|
10 | 10 |
|
11 | 11 | class RabbitMQJob extends Job |
12 | 12 | { |
13 | | - |
14 | | - protected $channel; |
15 | | - protected $queue; |
16 | | - protected $connection; |
17 | | - protected $message; |
18 | | - |
19 | | - public function __construct($container, RabbitMQQueue $connection, AMQPChannel $channel, $queue, AMQPMessage $message) |
20 | | - { |
21 | | - $this->container = $container; |
22 | | - $this->connection = $connection; |
23 | | - $this->channel = $channel; |
24 | | - $this->queue = $queue; |
25 | | - $this->message = $message; |
26 | | - } |
27 | | - |
28 | | - /** |
29 | | - * Fire the job. |
30 | | - * |
31 | | - * @return void |
32 | | - */ |
33 | | - public function fire() |
34 | | - { |
35 | | - $this->resolveAndFire(json_decode($this->message->body, true)); |
36 | | - } |
37 | | - |
38 | | - /** |
39 | | - * Get the raw body string for the job. |
40 | | - * |
41 | | - * @return string |
42 | | - */ |
43 | | - public function getRawBody() |
44 | | - { |
45 | | - return $this->message->body; |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Delete the job from the queue. |
50 | | - * |
51 | | - * @return void |
52 | | - */ |
53 | | - public function delete() |
54 | | - { |
55 | | - parent::delete(); |
56 | | - |
57 | | - $this->channel->basic_ack($this->message->delivery_info['delivery_tag']); |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Get queue name |
62 | | - * |
63 | | - * @return string |
64 | | - */ |
65 | | - public function getQueue() |
66 | | - { |
67 | | - return $this->queue; |
68 | | - } |
69 | | - |
70 | | - /** |
71 | | - * Release the job back into the queue. |
72 | | - * |
73 | | - * @param int $delay |
74 | | - * |
75 | | - * @return void |
76 | | - */ |
77 | | - public function release($delay = 0) |
78 | | - { |
79 | | - $this->delete(); |
80 | | - |
81 | | - $body = $this->message->body; |
82 | | - $body = json_decode($body, true); |
83 | | - |
84 | | - $attempts = $this->attempts(); |
85 | | - |
86 | | - // write attempts to body |
87 | | - $body['attempts'] = $attempts + 1; |
88 | | - |
89 | | - $this->connection->pushRaw(json_encode($body), $this->getQueue(), |
90 | | - $delay > 0 ? [ 'delay' => $delay ] : []); |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Get the number of times the job has been attempted. |
95 | | - * |
96 | | - * @return int |
97 | | - */ |
98 | | - public function attempts() |
99 | | - { |
100 | | - $body = json_decode($this->message->body, true); |
101 | | - |
102 | | - return isset($body['attempts']) ? $body['attempts'] : 0; |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Get the job identifier. |
107 | | - * |
108 | | - * @return string |
109 | | - */ |
110 | | - public function getJobId() |
111 | | - { |
112 | | - return $this->message->body; |
113 | | - } |
114 | | - |
| 13 | + protected $channel; |
| 14 | + protected $queue; |
| 15 | + protected $connection; |
| 16 | + protected $message; |
| 17 | + |
| 18 | + public function __construct($container, RabbitMQQueue $connection, AMQPChannel $channel, $queue, AMQPMessage $message) |
| 19 | + { |
| 20 | + $this->container = $container; |
| 21 | + $this->connection = $connection; |
| 22 | + $this->channel = $channel; |
| 23 | + $this->queue = $queue; |
| 24 | + $this->message = $message; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Fire the job. |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function fire() |
| 33 | + { |
| 34 | + $this->resolveAndFire(json_decode($this->message->body, true)); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the raw body string for the job. |
| 39 | + * |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + public function getRawBody() |
| 43 | + { |
| 44 | + return $this->message->body; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Delete the job from the queue. |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function delete() |
| 53 | + { |
| 54 | + parent::delete(); |
| 55 | + |
| 56 | + $this->channel->basic_ack($this->message->delivery_info['delivery_tag']); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Get queue name |
| 61 | + * |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + public function getQueue() |
| 65 | + { |
| 66 | + return $this->queue; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Release the job back into the queue. |
| 71 | + * |
| 72 | + * @param int $delay |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function release($delay = 0) |
| 77 | + { |
| 78 | + $this->delete(); |
| 79 | + |
| 80 | + $body = $this->message->body; |
| 81 | + $body = json_decode($body, true); |
| 82 | + |
| 83 | + $attempts = $this->attempts(); |
| 84 | + |
| 85 | + // write attempts to body |
| 86 | + $body['attempts'] = $attempts + 1; |
| 87 | + |
| 88 | + $this->connection->pushRaw(json_encode($body), $this->getQueue(), |
| 89 | + $delay > 0 ? [ 'delay' => $delay ] : []); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the number of times the job has been attempted. |
| 94 | + * |
| 95 | + * @return int |
| 96 | + */ |
| 97 | + public function attempts() |
| 98 | + { |
| 99 | + $body = json_decode($this->message->body, true); |
| 100 | + |
| 101 | + return isset($body['attempts']) ? $body['attempts'] : 0; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get the job identifier. |
| 106 | + * |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + public function getJobId() |
| 110 | + { |
| 111 | + return $this->message->body; |
| 112 | + } |
115 | 113 | } |
0 commit comments