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