| 1 | <?php |
||
| 2 | |||
| 3 | namespace Freyo\LaravelQueueCMQ\Queue\Jobs; |
||
| 4 | |||
| 5 | use Freyo\LaravelQueueCMQ\Queue\CMQQueue; |
||
| 6 | use Freyo\LaravelQueueCMQ\Queue\Driver\Message; |
||
| 7 | use Freyo\LaravelQueueCMQ\Queue\Driver\Queue; |
||
| 8 | use Illuminate\Container\Container; |
||
| 9 | use Illuminate\Contracts\Queue\Job as JobContract; |
||
| 10 | use Illuminate\Queue\Jobs\Job; |
||
| 11 | |||
| 12 | class CMQJob extends Job implements JobContract |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var \Freyo\LaravelQueueCMQ\Queue\CMQQueue |
||
| 16 | */ |
||
| 17 | protected $connection; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var \Freyo\LaravelQueueCMQ\Queue\Driver\Message |
||
| 21 | */ |
||
| 22 | protected $message; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new job instance. |
||
| 26 | * |
||
| 27 | * @param \Illuminate\Container\Container $container |
||
| 28 | * @param \Freyo\LaravelQueueCMQ\Queue\CMQQueue $connection |
||
| 29 | * @param \Freyo\LaravelQueueCMQ\Queue\Driver\Message $message |
||
| 30 | * @param \Freyo\LaravelQueueCMQ\Queue\Driver\Queue $queue |
||
| 31 | * @param string $connectionName |
||
| 32 | */ |
||
| 33 | public function __construct(Container $container, CMQQueue $connection, Message $message, Queue $queue, $connectionName) |
||
| 34 | { |
||
| 35 | $this->container = $container; |
||
| 36 | $this->connection = $connection; |
||
| 37 | $this->message = $message; |
||
| 38 | $this->queue = $queue->getQueueName(); |
||
| 39 | $this->connectionName = $connectionName; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the job identifier. |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function getJobId() |
||
| 48 | { |
||
| 49 | return $this->message->msgId; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the raw body of the job. |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function getRawBody() |
||
| 58 | { |
||
| 59 | return $this->message->msgBody; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get the number of times the job has been attempted. |
||
| 64 | * |
||
| 65 | * @return int |
||
| 66 | */ |
||
| 67 | public function attempts() |
||
| 68 | { |
||
| 69 | return $this->message->dequeueCount; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Fire the job. |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public function fire() |
||
| 78 | { |
||
| 79 | method_exists($this, 'resolveAndFire') |
||
| 80 | ? $this->resolveAndFire($this->payload()) |
||
| 81 | : parent::fire(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the decoded body of the job. |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function payload() |
||
| 90 | { |
||
| 91 | if ($this->connection->isPlain()) { |
||
| 92 | $job = $this->connection->getPlainJob(); |
||
| 93 | |||
| 94 | return [ |
||
| 95 | 'displayName' => is_string($job) ? explode('@', $job)[0] : null, |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 96 | 'job' => $job, |
||
| 97 | 'maxTries' => null, |
||
| 98 | 'timeout' => null, |
||
| 99 | 'data' => $this->getRawBody(), |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | |||
| 103 | return json_decode($this->getRawBody(), true); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Delete the job from the queue. |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function delete() |
||
| 112 | { |
||
| 113 | parent::delete(); |
||
| 114 | |||
| 115 | $this->connection->getQueue($this->getQueue())->delete_message($this->message->receiptHandle); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Release the job back into the queue. |
||
| 120 | * |
||
| 121 | * @param int $delay |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function release($delay = 0) |
||
| 126 | { |
||
| 127 | parent::release($delay); |
||
| 128 | } |
||
| 129 | } |
||
| 130 |