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