-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
worker.php
38 lines (29 loc) · 905 Bytes
/
worker.php
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
<?php
//Establish connection AMQP
$connection = new AMQPConnection();
$connection->setHost('127.0.0.1');
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
//Create and declare channel
$channel = new AMQPChannel($connection);
$routing_key = 'task_queue';
$callback_func = function(AMQPEnvelope $message, AMQPQueue $q) use (&$max_jobs) {
echo " [x] Received: ", $message->getBody(), PHP_EOL;
sleep(sleep(substr_count($message->getBody(), '.')));
echo " [X] Done", PHP_EOL;
$q->ack($message->getDeliveryTag());
};
try{
$queue = new AMQPQueue($channel);
$queue->setName($routing_key);
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
echo ' [*] Waiting for logs. To exit press CTRL+C', PHP_EOL;
$queue->consume($callback_func);
}catch(AMQPQueueException $ex){
print_r($ex);
}catch(Exception $ex){
print_r($ex);
}
$connection->disconnect();