queue.php
1.1 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
<?php
use Channel\Client;
use Channel\Server;
use Workerman\Worker;
use Workerman\Lib\Timer;
// composer autoload
include __DIR__ . '/../vendor/autoload.php';
$channel_server = new Server();
$worker = new Worker();
$worker->name = 'Event';
$worker->onWorkerStart = function()
{
Client::connect();
$count = 0;
$timerId = Timer::add(0.01, function() use (&$timerId, &$count) {
Client::publish('test event', 'some data');
$count++;
Client::enqueue('task-queue', time());
if ($count == 1000) {
Timer::del($timerId);
}
});
Timer::add(10, function() {
Client::enqueue('task-queue', 'hello every 10 seconds');
});
};
$mq = new Worker();
$mq->name = 'Queue';
$mq->count = 4;
$mq->onWorkerStart = function($worker) {
Client::connect();
$countDown = 20;
$id = 1;
Client::watch('task-queue', function($data) use ($worker, &$countDown, &$id) {
echo "[$id] Worker {$worker->id} get queue: $data\n";
sleep(0.2);
$countDown--;
$id++;
if ($worker->id > 1 && $countDown == 0) {
Client::unwatch('task-queue');
}
Timer::add(1, [Client::class, 'reserve'], [], false);
});
};
Worker::runAll();