forked from walkor/workerman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.php
75 lines (62 loc) · 1.7 KB
/
redis.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
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
<?php
//examples on how to use WorkerClient
require_once __DIR__ . '/WorkerClient/Autoloader.php';
use WorkerClient\Worker;
$redis_password = '';
// Create a Worker Process Manager server
$worker = new Worker("redis://127.0.0.1:6379");
// number of worker processes (master process excluded)
$worker->count = 10;
$channel = pathinfo($argv[0])['filename'];
$onMessage = function ($connection, $data)
{
if (!is_null($data)) {
//do something
file_put_contents('test.txt', $data . "\n", FILE_APPEND);
}
else {
echo "brPop timeout, retry...\n";
}
if (Worker::getStatus() == Worker::STATUS_RUNNING) {
global $channel;
$connection->send("brPop {$channel} 3\n", true); #timeout = 3 seconds
} else {
$connection->close();
}
};
$onLogin = function ($connection, $login)
{
if ($login !== true)
{
echo "auth failed\n";
$connection->close();
return;
}
echo "auth ok\n";
global $onMessage;
$connection->onMessage = $onMessage;
$onMessage($connection, null); //ugly but just working
};
// Emitted when new connection come
$worker->onConnect = function($connection)
{
echo "connected to redis\n";
global $redis_password;
if ($redis_password) {
$connection->send("AUTH {$redis_password}\n", true);
}
else {
global $onLogin;
$onLogin($connection, true); //no auth = success
}
};
$worker->onMessage = $onLogin;
// Emitted when connection closed
$worker->onClose = function($connection)
{
echo "Server Closed Connection, stop this worker.\n";
//Master process will start a new worker process
$connection->worker->stop();
};
// Run worker
Worker::runAll();