QueueTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\traits;
  12. use crmeb\utils\Queue;
  13. /**
  14. * 快捷加入消息队列
  15. * Trait QueueTrait
  16. * @package crmeb\traits
  17. */
  18. trait QueueTrait
  19. {
  20. /**
  21. * 列名
  22. * @return string
  23. */
  24. protected static function queueName()
  25. {
  26. return null;
  27. }
  28. /**
  29. * 加入队列
  30. * @param $action
  31. * @param array $data
  32. * @param string|null $queueName
  33. * @return mixed
  34. */
  35. public static function dispatch($action = null, array $data = [], string $queueName = null)
  36. {
  37. $queue = Queue::instance()->job(__CLASS__);
  38. if (is_array($action)) {
  39. $queue->data(...$action);
  40. } else if (is_string($action)) {
  41. $queue->do($action)->data(...$data);
  42. }
  43. if ($queueName) {
  44. $queue->setQueueName($queueName);
  45. } else if (self::queueName()) {
  46. $queue->setQueueName(self::queueName());
  47. }
  48. return $queue->push();
  49. }
  50. /**
  51. * 延迟加入消息队列
  52. * @param int $secs
  53. * @param $action
  54. * @param array $data
  55. * @param string|null $queueName
  56. * @return mixed
  57. */
  58. public static function dispatchSece(int $secs, $action = null, array $data = [], string $queueName = null)
  59. {
  60. $queue = Queue::instance()->job(__CLASS__)->secs($secs);
  61. if (is_array($action)) {
  62. $queue->data(...$action);
  63. } else if (is_string($action)) {
  64. $queue->do($action)->data(...$data);
  65. }
  66. if ($queueName) {
  67. $queue->setQueueName($queueName);
  68. } else if (self::queueName()) {
  69. $queue->setQueueName(self::queueName());
  70. }
  71. return $queue->push();
  72. }
  73. /**
  74. * 加入小队列
  75. * @param string $do
  76. * @param array $data
  77. * @param int|null $secs
  78. * @param string|null $queueName
  79. * @return mixed
  80. */
  81. public static function dispatchDo(string $do, array $data = [], int $secs = null, string $queueName = null)
  82. {
  83. $queue = Queue::instance()->job(__CLASS__)->do($do);
  84. if ($secs) {
  85. $queue->secs($secs);
  86. }
  87. if ($data) {
  88. $queue->data(...$data);
  89. }
  90. if ($queueName) {
  91. $queue->setQueueName($queueName);
  92. } else if (self::queueName()) {
  93. $queue->setQueueName(self::queueName());
  94. }
  95. return $queue->push();
  96. }
  97. }