-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.php
More file actions
139 lines (113 loc) · 4.25 KB
/
worker.php
File metadata and controls
139 lines (113 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
use Laminas\Mvc\Service\ServiceManagerConfig;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ArrayUtils;
use Laminas\Stdlib\Glob;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Wire\AMQPTable;
require_once __DIR__ . '/vendor/autoload.php';
class RabbitWorker
{
private $db;
private $serviceManager;
private $rabbitMQConfig;
private $workerId;
public function __construct($workerName)
{
$configuration = [];
foreach (Glob::glob('config/{{*}}{{,*.local}}.php', Glob::GLOB_BRACE) as $file) {
$configuration = ArrayUtils::merge($configuration, include $file);
}
// Prepare the service manager
$smConfig = isset($config['service_manager']) ? $configuration['service_manager'] : [];
$smConfig = new ServiceManagerConfig($smConfig);
$this->serviceManager = new ServiceManager();
$smConfig->configureServiceManager($this->serviceManager);
$this->serviceManager->setService('ApplicationConfig', $configuration);
// Load modules
$this->serviceManager->get('ModuleManager')->loadModules();
$this->db = $this->serviceManager->get('DBJobQueue');
$this->rabbitMQConfig = $this->serviceManager->get('Config')['rabbitmq'];
$this->workerId = null;
$stmt = $this->db->query("SELECT id FROM processors WHERE name = ?");
$processorResults = $stmt->execute([$workerName]);
foreach ($processorResults as $processorResult) {
$this->workerId = $processorResult['id'];
}
if (!isset($this->workerId)) {
$stmt = $this->db->query("INSERT INTO processors "
. "(name) VALUES (?)");
$stmt->execute([$workerName]);
$this->workerId = $this->db->getDriver()->getLastGeneratedValue();
}
}
public function work()
{
$connection = new AMQPStreamConnection(
$this->rabbitMQConfig["RABBITMQ_HOST"],
$this->rabbitMQConfig["RABBITMQ_PORT"],
$this->rabbitMQConfig["RABBITMQ_USERNAME"],
$this->rabbitMQConfig["RABBITMQ_PASSWORD"]
);
$channel = $connection->channel();
# Create the queue if it doesnt already exist.
$channel->queue_declare(
$queue = $this->rabbitMQConfig["RABBITMQ_QUEUE_NAME"],
$passive = false,
$durable = true,
$exclusive = false,
$auto_delete = false,
$nowait = false,
// $arguments = null,
new AMQPTable(array(
"x-max-priority" => 10
)),
$ticket = null
);
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$callback = function($msg) {
echo " [x] Received ", $msg->body, "\n";
$job = json_decode($msg->body, $assocForm = true);
$stmt = $this->db->query("SELECT command FROM job_list "
. "WHERE id = ?");
$results = $stmt->execute([$job['id']]);
foreach ($results as $result) {
$command = $result['command'];
}
$output = shell_exec($command);
echo " Output: $output \n";
$stmt = $this->db->query("UPDATE job_list "
. "SET processor_id=?, executed_at=CURRENT_TIMESTAMP WHERE id = ?");
$results = $stmt->execute([
$this->workerId,
$job['id']
]);
echo " [x] Done", "\n";
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
$channel->basic_qos(null, 1, null);
$channel->basic_consume(
$queue = $this->rabbitMQConfig["RABBITMQ_QUEUE_NAME"],
$consumer_tag = '',
$no_local = false,
$no_ack = false,
$exclusive = false,
$nowait = false,
$callback
);
while (count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
}
}
if (isset($argv[1])) {
echo ' Worker name is: '.$argv[1], "\n";
$rabbitWorker = new RabbitWorker($argv[1]);
$rabbitWorker->work();
}
else {
echo "Please provide a processor name \n";
}
die;