-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWorker.php
More file actions
65 lines (53 loc) · 1.43 KB
/
Worker.php
File metadata and controls
65 lines (53 loc) · 1.43 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
<?php
namespace ShonM\ResqueBundle;
use Resque\Worker as BaseWorker;
class Worker extends BaseWorker
{
private $forkCount = 1;
private $job;
public function forkInstances($count)
{
settype($count, 'int');
$this->forkCount = 1;
if ($count > 1) {
if (function_exists('pcntl_fork')) {
$this->forkCount = $count;
} else {
fwrite(STDOUT, "*** Fork could not be initialized. PHP function pcntl_fork() does exist\n");
}
}
}
public function getForkInstances()
{
return $this->forkCount;
}
public function daemonize()
{
if (function_exists('pcntl_fork')) {
for ($i = 0; $i < $this->getForkInstances(); ++$i) {
$pid = pcntl_fork();
if ($pid == -1) {
throw new \RuntimeException("Could not fork worker {$i}");
} elseif (! $pid) {
$this->work($this->getInterval());
die;
}
}
} else {
$this->work($this->getInterval());
}
}
public function job()
{
if ($this->job === null) {
$this->job = $this->job();
}
return $this->job;
}
public function getQueues()
{
return array_map(function ($queue) {
return new Queue($queue);
}, $this->queues());
}
}