-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPHPResqueBundle.php
More file actions
87 lines (72 loc) · 2.46 KB
/
PHPResqueBundle.php
File metadata and controls
87 lines (72 loc) · 2.46 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
<?php
namespace PHPResqueBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
require_once dirname(__FILE__) . '/vendor/php-resque/lib/Resque.php';
require_once dirname(__FILE__) . '/vendor/php-resque/lib/Resque/Worker.php';
class PHPResqueBundle extends Bundle {
private $queue = '*';
private $logging = 'normal';
private $checker_interval = 5;
private $fork_count = 1;
public function defineQueue($name) {
$this->queue = $name;
}
public function verbose($mode) {
$this->logging = $mode;
}
public function setInterval($interval) {
$this->checker_interval = (int)$interval;
}
public function forkInstances($count) {
settype($count, 'int');
if ($count > 1) {
if (function_exists('pcntl_fork')) {
$this->fork_count = $count;
} else {
fwrite(STDOUT, "*** Fork could not initialized. PHP function pcntl_fork() does NOT exists \n");
$this->fork_count = 1;
}
} else {
$this->fork_count = 1;
}
}
public function getForkInstances() {
return $this->fork_count;
}
private function loglevel() {
switch ($this->logging) {
case 'verbose' :
return \Resque_Worker::LOG_VERBOSE;
case 'normal' :
return \Resque_Worker::LOG_NONE;
default :
return \Resque_Worker::LOG_NONE;
}
}
private function work() {
$worker = new \Resque_Worker(explode(',', $this->queue));
$worker->logLevel = $this->loglevel();
$worker->work($this->checker_interval);
fwrite(STDOUT, '*** Starting worker ' . $worker . "\n");
}
public function daemon() {
\Resque::setBackend('127.0.0.1:6379');
if (strpos($this->queue, ':') !== false) {
list($namespace, $queue) = explode(':', $this->queue);
\Resque_Redis::prefix($namespace);
$this->queue = $queue;
}
if ($this->getForkInstances() > 1) {
for ($i = 0; $i < $this->getForkInstances(); ++$i) {
$pid = pcntl_fork();
if ($pid == -1) {
throw new \RuntimeException("Could not fork worker {$i}");
}
$this->work();
break;
}
} else {
$this->work();
}
}
}