1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Examples; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* All listeners will be called after async calling "foo", "bar", "baz" jobs. |
6
|
|
|
* |
7
|
|
|
* @package Examples |
8
|
|
|
* @subpackage Base |
9
|
|
|
* |
10
|
|
|
* @author Vasily Zorin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ExampleComplexJob extends \PHPDaemon\Core\AppInstance |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Called when the worker is ready to go |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function onReady() |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
// Adding listener |
24
|
|
|
// ComplexJob - STATE_WAITING |
25
|
|
|
$job = new \PHPDaemon\Core\ComplexJob(function ($job) { |
26
|
|
|
// ComplexJob - STATE_DONE |
27
|
|
|
/*array ( |
28
|
|
|
'bar' => |
29
|
|
|
array ( |
30
|
|
|
'job' => 'bar', |
31
|
|
|
'success' => false, |
32
|
|
|
'line' => 63, |
33
|
|
|
), |
34
|
|
|
'foo' => |
35
|
|
|
array ( |
36
|
|
|
'job' => 'foo', |
37
|
|
|
'success' => true, |
38
|
|
|
'line' => 84, |
39
|
|
|
'arg' => |
40
|
|
|
array ( |
41
|
|
|
'param' => 'value', |
42
|
|
|
), |
43
|
|
|
), |
44
|
|
|
'baz' => |
45
|
|
|
array ( |
46
|
|
|
'job' => 'baz', |
47
|
|
|
'success' => false, |
48
|
|
|
'line' => 94, |
49
|
|
|
), |
50
|
|
|
)*/ |
51
|
|
|
\PHPDaemon\Core\Daemon::log($job->results); |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
// Adding listener |
55
|
|
|
// ComplexJob - STATE_WAITING |
56
|
|
|
$job->addListener(function ($job) { |
|
|
|
|
57
|
|
|
// ComplexJob - STATE_DONE |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
// Adding async job foo |
61
|
|
|
$job('foo', $this->foo(['param' => 'value'])); |
62
|
|
|
|
63
|
|
|
// Adding with 1 sec delay |
64
|
|
|
\PHPDaemon\Core\Timer::add(function ($event) use ($job) { |
65
|
|
|
|
66
|
|
|
// Adding async job bar |
67
|
|
View Code Duplication |
$job('bar', function ($jobname, $job) { |
|
|
|
|
68
|
|
|
\PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job) { |
69
|
|
|
// Job done |
70
|
|
|
$job->setResult($jobname, ['job' => 'bar', 'success' => false, 'line' => __LINE__]); |
71
|
|
|
$event->finish(); |
72
|
|
|
}, 1e3 * 50); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
// Adding async job baz. Equal $job('baz', $this->baz()); |
76
|
|
|
$job->addJob('baz', $this->baz()); |
77
|
|
|
|
78
|
|
|
// Run jobs. All listeners will be called when the jobs done |
79
|
|
|
// ComplexJob - STATE_RUNNING |
80
|
|
|
$job(); |
81
|
|
|
|
82
|
|
|
$event->finish(); |
83
|
|
|
}, 1e6 * 1); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
final public function foo($arg) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return function ($jobname, $job) use ($arg) { |
89
|
|
|
\PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job, $arg) { |
90
|
|
|
// Job done |
91
|
|
|
$job->setResult($jobname, ['job' => 'foo', 'success' => true, 'line' => __LINE__, 'arg' => $arg]); |
92
|
|
|
$event->finish(); |
93
|
|
|
}, 1e3 * 100); |
94
|
|
|
}; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
final public function baz() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return function ($jobname, $job) { |
100
|
|
|
\PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job) { |
101
|
|
|
// Job done |
102
|
|
|
$job->setResult($jobname, ['job' => 'baz', 'success' => false, 'line' => __LINE__]); |
103
|
|
|
$event->finish(); |
104
|
|
|
}, 1e3 * 300); |
105
|
|
|
}; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.