1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gewaer\Cli\Tasks; |
4
|
|
|
|
5
|
|
|
use Phalcon\Cli\Task as PhTask; |
6
|
|
|
use Gewaer\Models\UserLinkedSources; |
7
|
|
|
use Gewaer\Models\Users; |
8
|
|
|
use Throwable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* CLI To send push ontification and pusher msg |
12
|
|
|
* |
13
|
|
|
* @package Gewaer\Cli\Tasks |
14
|
|
|
* |
15
|
|
|
* @property Config $config |
16
|
|
|
* @property \Pusher\Pusher $pusher |
17
|
|
|
* @property \Monolog\Logger $log |
18
|
|
|
* @property Channel $channel |
19
|
|
|
* @property Queue $queue |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class QueueTask extends PhTask |
23
|
|
|
{ |
24
|
|
|
public function notificationAction() |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Every handler needs the below code |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The job itself |
33
|
|
|
*/ |
34
|
|
|
// $jobArray = [ |
35
|
|
|
// 'id' => $job_id++, |
36
|
|
|
// 'notification' => 'hello you need to pay your account', |
37
|
|
|
// 'sleep_period' => rand(0, 3) |
38
|
|
|
// ]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Need to convert it to rabbitmq msg |
42
|
|
|
*/ |
43
|
|
|
$msg = new \PhpAmqpLib\Message\AMQPMessage( |
|
|
|
|
44
|
|
|
json_encode($jobArray, JSON_UNESCAPED_SLASHES), |
|
|
|
|
45
|
|
|
['delivery_mode' => 2] // make message persistent |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Actual way to send jobs to queue |
50
|
|
|
*/ |
51
|
|
|
// $channel->basic_publish($msg, '', RABBITMQ_QUEUE_NAME); |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/////////////////////////////////////////////////////////////////////// |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
$channel = $this->queue->channel(); |
58
|
|
|
|
59
|
|
|
// Create the queue if it doesnt already exist. |
60
|
|
|
$channel->queue_declare( |
61
|
|
|
$queue = "notifications", |
62
|
|
|
$passive = false, |
63
|
|
|
$durable = true, |
64
|
|
|
$exclusive = false, |
65
|
|
|
$auto_delete = false, |
66
|
|
|
$nowait = false, |
67
|
|
|
$arguments = null, |
68
|
|
|
$ticket = null |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
echo ' [*] Waiting for notifications. To exit press CTRL+C', "\n"; |
72
|
|
|
|
73
|
|
|
$callback = function ($msg) { |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Assign message body as an assoc array to job |
77
|
|
|
*/ |
78
|
|
|
$job = json_decode($msg->body, $assocForm = true); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Custom actions here on jobs |
82
|
|
|
*/ |
83
|
|
|
echo($job['notification']); |
84
|
|
|
sleep($job['sleep_period']); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Log the delivery info |
88
|
|
|
*/ |
89
|
|
|
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); |
90
|
|
|
}; |
91
|
|
|
|
92
|
|
|
$channel->basic_qos(null, 1, null); |
93
|
|
|
|
94
|
|
|
$channel->basic_consume( |
95
|
|
|
$queue = "notifications", |
96
|
|
|
$consumer_tag = '', |
97
|
|
|
$no_local = false, |
98
|
|
|
$no_ack = false, |
99
|
|
|
$exclusive = false, |
100
|
|
|
$nowait = false, |
101
|
|
|
$callback |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
while (count($channel->callbacks)) { |
105
|
|
|
$channel->wait(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$channel->close(); |
109
|
|
|
$this->queue->close(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|