caseyamcl/sortable-tasks 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

caseyamcl/sortable-tasks

Composer 安装命令:

composer require caseyamcl/sortable-tasks

包简介

Abstraction to compile and sort tasks on the fly

README 文档

README

Latest Version on Packagist Software License Build Coverage Status Quality Score Total Downloads

A simple, un-opinionated PHP 7.4+ abstraction library to allow for the ordering of tasks. Features:

  • Tasks are service class instances that can define other tasks as dependencies
  • Useful for libraries like setup routines, ensuring that HTTP middleware runs in-order, and other libraries where the ordering of tasks is determined in an arbitrary order, but need to be run deterministically
  • Builds on the marcj/topsort library to enable sorting of tasks, each defined in their own class
  • Un-opinionated about how tasks actually run; just concerned with sorting them based on their dependency and running them in-order
  • PSR-4 and PSR-12 compliant

Structure

This library only contains two files:

| SortableTask.php | Interface for a task; contains two methods to define dependencies, depeondsOn and mustRunBefore | | SortableTasksIterator.php | Iterator class that does the sorting and other work; implements Iterable and Countable |

Install

Via Composer

$ composer require caseyamcl/sortable-tasks

Usage

Refer to the tests/Fixture directory for a fully functional example.

The best way to demonstrate this library is with an example, so we'll use a setup application. In our hypothetical application, setup tasks can be registered in any order, but they will run in a particular order based on a set of explicitly defined dependencies.

First, we must define a class that implements the SortableTask interface:

use SortableTasks\SortableTask;

abstract class SetupStep implements SortableTask
{
    abstract public function __invoke(): bool;
}
 

Notice that we do not implement the dependsOn() and mustRunBefore() methods in the abstract SetupStep class. This means that each concrete implementation step must define its dependencies. This is optional, and is up to the library that implements SortableTasks.

If most steps do not require ordering, then we could do the following:

use SortableTasks\SortableTask;

abstract class SetupStep implements SortableTask
{
    abstract public function __invoke(): bool;

    // Provide default implementations of `dependsOn` and `mustRunBefore` that return empty arrays
    public static function dependsOn() : iterable
    {
        return [];
    }
    public static function mustRunBefore() : iterable
    {
        return [];
    }
}

Let's create some setup steps now:

class CheckConfigStep extends SetupStep
{
    public static function dependsOn(): iterable
    {
       return []; // depends on nothing; can run anytime in the order of operations
    }
    
    public function __invoke(): bool
    {
        // do stuff, then..
        return true;
    }
}

class CheckDbConnectionStep extends SetupStep
{
    private DbConnector $dbConnector;
    
    public function __construct(DbConnector $dbConnector)
    {    
        $this->dbConnector = $dbConnector;
    }

    public static function dependsOn(): iterable
    {
        return [CheckConfigStep::class];
    }

    public static function mustRunBefore(): iterable
    {
        return [BuildContainerStep::class];
    }

    public function __invoke(): bool
    {
        // do stuff, then..
        return $this->dbConnector->checkConnection();
    }
}

class BuildContainerStep extends SetupStep
{
    private ContainerBuilder $containerBuilder;

    public function __construct(ContainerBuilder $containerBuilder)
    {
        $this->containerBuilder = $containerBuilder;
    }

    public static function dependsOn(): iterable
    {
        return [CheckConfigStep::class, CheckDbConnection::class];
    }

    public function __invoke(): bool
    {
        // do stuff, then..
        return $this->containerBuilder->buildContainer();
    }
}

Now that we have some concrete classes, let's add them to a SortableTasksIterator:

use SortableTasks\SortableTasksIterator;

$iterator = new SortableTasksIterator();

// Notice that it doesn't matter in what order we add the steps; they will get sorted at runtime
$iterator->add(new BuildContainerStep());
$iterator->add(new CheckDbConnectionStep());
$iterator->add(new CheckConfigStep());

// Tasks are sorted upon calling the iterator
// Class names are the keys
foreach ($iterator as $setupStepClassName => $setupStep) {
    if (! $setupStep()->__invoke()) {
        throw new SetupFailedException('Setup failed on step: ' . $setupStepClassName);
    }
}

Handling errors

There are two issues that are likely to occur during task execution, both of them throw excpetions:

  1. Circular dependencies; e.g., Task "A" depends on Task "B", which depends on Task "A". In this situation, a MJS\TopSort\CircularDependecyException is thrown.
  2. Non-existent dependencies; e.g., Task "A" depends on Task "B", but task "B" is not defined. In this situation, a MJS\TopSort\ElementNotFoundException is thrown.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email me@caseymclaughlin.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

caseyamcl/sortable-tasks 适用场景与选型建议

caseyamcl/sortable-tasks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「caseyamcl」 「sortable-tasks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 caseyamcl/sortable-tasks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 caseyamcl/sortable-tasks 我们能提供哪些服务?
定制开发 / 二次开发

基于 caseyamcl/sortable-tasks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-31