-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDaemon.php
More file actions
108 lines (101 loc) · 2.6 KB
/
Daemon.php
File metadata and controls
108 lines (101 loc) · 2.6 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
<?php
namespace PHPQueue;
use Clio\Console;
use Clio\Daemon as D;
abstract class Daemon
{
public $queue_name = null;
public $pid_file;
public $log_root;
public $stdout = '/tmp/stdout.log';
public $stderr = '/tmp/stderr.log';
/**
* @param string $pid_file
* @param string $log_root
*/
public function __construct($pid_file, $log_root)
{
$this->pid_file = $pid_file;
$this->log_root = $log_root;
}
public function run()
{
global $argv;
if (empty($argv[1]))
{
Console::output("Unknown action.");
die();
}
if (empty($this->queue_name))
{
Console::output("Queue is not set.");
die();
}
switch($argv[1])
{
case 'start':
$this->start();
break;
case 'stop':
$this->stop();
break;
case 'restart':
$this->restart();
break;
default:
Console::output("Unknown action.");
break;
}
}
protected function start()
{
Console::stdout('Starting... ');
try
{
if (D::isRunning($this->pid_file)) {
Console::output('%y[Already Running]%n');
} else {
$queue = $this->queue_name;
$log_path = $this->log_root;
D::work(array(
'pid' => $this->pid_file
, 'stdout' => $this->stdout
, 'stderr' => $this->stderr
),
function($stdin, $stdout, $sterr) use ($queue, $log_path)
{
$runner = new Runner($queue, array('logPath'=>$log_path));
$runner->run();
}
);
Console::output('%g[OK]%n');
}
}
catch (\Exception $ex)
{
Console::output('%r[FAILED]%n');
}
}
protected function stop()
{
Console::stdout('Stopping... ');
try
{
if (!D::isRunning($this->pid_file)) {
Console::output('%y[Daemon not running]%n');
} else {
D::kill($this->pid_file, true);
Console::output('%g[OK]%n');
}
}
catch (\Exception $ex)
{
Console::output('%r[FAILED]%n');
}
}
protected function restart()
{
$this->stop();
$this->start();
}
}