123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace crmeb\traits;
- use crmeb\utils\Queue;
- trait QueueTrait
- {
-
- protected static function queueName()
- {
- return null;
- }
-
- public static function dispatch($action = null, array $data = [], string $queueName = null)
- {
- $queue = Queue::instance()->job(__CLASS__);
- if (is_array($action)) {
- $queue->data(...$action);
- } else if (is_string($action)) {
- $queue->do($action)->data(...$data);
- }
- if ($queueName) {
- $queue->setQueueName($queueName);
- } else if (self::queueName()) {
- $queue->setQueueName(self::queueName());
- }
- return $queue->push();
- }
-
- public static function dispatchSece(int $secs, $action = null, array $data = [], string $queueName = null)
- {
- $queue = Queue::instance()->job(__CLASS__)->secs($secs);
- if (is_array($action)) {
- $queue->data(...$action);
- } else if (is_string($action)) {
- $queue->do($action)->data(...$data);
- }
- if ($queueName) {
- $queue->setQueueName($queueName);
- } else if (self::queueName()) {
- $queue->setQueueName(self::queueName());
- }
- return $queue->push();
- }
-
- public static function dispatchDo(string $do, array $data = [], int $secs = null, string $queueName = null)
- {
- $queue = Queue::instance()->job(__CLASS__)->do($do);
- if ($secs) {
- $queue->secs($secs);
- }
- if ($data) {
- $queue->data(...$data);
- }
- if ($queueName) {
- $queue->setQueueName($queueName);
- } else if (self::queueName()) {
- $queue->setQueueName(self::queueName());
- }
- return $queue->push();
- }
- }
|