Dispatch.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\job;
  12. use core\util\Queue;
  13. /**
  14. * 任务派遣队列
  15. */
  16. class Dispatch
  17. {
  18. /**
  19. * 加入队列
  20. * @param $action
  21. * @param array $data
  22. * @param int $secs
  23. * @param string|null $queue_name
  24. * @param bool $is_async
  25. * @return mixed
  26. */
  27. public static function dispatch($action, array $data = [], int $secs = 0, string $queue_name = null, bool $is_async = true)
  28. {
  29. $class = static::class;//调用主调类
  30. if (env('queue.state', false) && $is_async) {
  31. $queue = Queue::instance()->job($class)->secs($secs);
  32. if (is_array($action)) {
  33. $queue->data(...$action);
  34. } else if (is_string($action)) {
  35. $queue->method($action)->data(...$data);
  36. }
  37. if ($queue_name) {
  38. $queue->setQueueName($queue_name);
  39. }
  40. return $queue->push();
  41. } else {
  42. if($secs == 0){
  43. $class_name = '\\' . $class;
  44. $res = new $class_name();
  45. if (is_array($action)) {
  46. return $res->doJob(...$action);
  47. } else {
  48. return $res->$action(...$data);
  49. }
  50. }
  51. }
  52. }
  53. }