项目作者: Hyperf-Glory

项目描述 :
基于Hyperf开发的任务调度系统
高级语言: PHP
项目地址: git://github.com/Hyperf-Glory/Task-Schedule.git
创建时间: 2021-02-07T10:49:00Z
项目社区:https://github.com/Hyperf-Glory/Task-Schedule

开源协议:MIT License

下载


Task-Schedule

基于Hyperf开发的任务调度系统

基于 Hyperf + Nsq 的一个异步队列库.支持投递任务,DAG任务编排.多个任务使用同一个事务。

特性

  • 默认 Nsq 驱动
  • 秒级延时任务
  • 自定义重试次数和时间
  • 自定义错误回调
  • 支持任务执行中间件
  • 自定义队列快照事件
  • 弹性多进程消费
  • 协程支持
  • 漂亮的仪表盘
  • 任务编排协程安全的单连接模式(事务保持、多路复用等条件下,有时必须使用一个连接)
  • dag任务编排

环境

  • PHP 7.4+
  • Swoole 4.6+
  • Redis 5.0+ (redis 驱动)
  • Nsq 1.2.0

TODO

  • RestApi支持投递,查看任务状态
  • web界面
  • 分布式支持
  • 接入报警

案例

1.投递任务

  1. use App\Model\Task;
  2. use App\Job\SimpleJob;
  3. use App\Kernel\Nsq\Queue;
  4. class Example{
  5. /**
  6. * @desc 测试job队列功能
  7. */
  8. public function queue() : void
  9. {
  10. $task = Task::find(1);
  11. $job = new SimpleJob($task);
  12. $queue = new Queue('queue');
  13. $queue->push($job);
  14. }
  15. }

2.任务编排协程安全的单连接模式(事务保持、多路复用等条件下,有时必须使用一个连接)

  1. use App\Kernel\Concurrent\ConcurrentMySQLPattern;
  2. use App\Dag\Task\Task1;
  3. use App\Dag\Task\Task2;
  4. use App\Dag\Task\Task3;
  5. class Example{
  6. public function conCurrentMySQL() : void
  7. {
  8. $dsn = 'DSN';
  9. $user = 'USER';
  10. $password = 'PWD';
  11. try {
  12. $pdo = new \PDO($dsn, $user, $password);
  13. $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
  14. $c = new ConcurrentMySQLPattern($pdo, $this->logger);
  15. $c->beginTransaction();
  16. $dag = new \Hyperf\Dag\Dag();
  17. $a = \Hyperf\Dag\Vertex::make(function () use ($c)
  18. {
  19. $task = new Task1();
  20. return $task->Run($c);
  21. }, 'a');
  22. $b = \Hyperf\Dag\Vertex::make(function ($results) use ($c)
  23. {
  24. $task = new Task2();
  25. return $task->Run($c);
  26. }, 'b');
  27. $d = \Hyperf\Dag\Vertex::make(function ($results) use ($c, $a, $b)
  28. {
  29. if ($results[$a->key] && $results[$b->key]) {
  30. return $c->commit();
  31. }
  32. return $c->rollback();
  33. }, 'd');
  34. $e = \Hyperf\Dag\Vertex::make(function ($results) use ($c)
  35. {
  36. $c->close();
  37. }, 'e');
  38. $results = $dag
  39. ->addVertex($a)
  40. ->addVertex($b)
  41. ->addVertex($d)
  42. ->addVertex($e)
  43. ->addEdge($a, $b)
  44. ->addEdge($b, $d)
  45. ->addEdge($d, $e)
  46. ->run();
  47. } catch (\PDOException $exception) {
  48. echo 'Connection failed: ' . $exception->getMessage();
  49. }
  50. }
  51. }

3.仪表盘
img.png

4.TODO:
task-schedule.png