-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.php
41 lines (38 loc) · 972 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
39
40
41
<?php
require_once './includes/init.php';
// -- Connect to PubSub --
use Google\Cloud\PubSub\PubSubClient;
$keyFile = 'pubsub_secret.json' ;
$pubSub = null;
$subscription = null;
$pubSub = new PubSubClient([
'keyFilePath' => 'includes/' . $keyFile
]);
$subscription = $pubSub->subscription('workers_test');
if(!$subscription->exists())
{
trigger_error('Missing subscription "workers_test"', E_USER_ERROR);
die;
}
// -- Main loop --
$task_counter = 1;
echo "Ready" . PHP_EOL;
echo "Waiting for tasks..." . PHP_EOL;
while(true)
{
// -- Waiting for message --
$message = false;
foreach ($subscription->pull(['maxMessages' => 1]) as $pullMessage)
{
$message = $pullMessage;
}
// -- Message received --
if ($message)
{
echo "New task #" . $task_counter . PHP_EOL;
sleep(10);
$subscription->acknowledge($message);
echo "Finished #" . $task_counter . PHP_EOL;
$task_counter++;
}
}