|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tomzx\Job; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
class Awaiter |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var int |
|
11
|
|
|
*/ |
|
12
|
|
|
private $resolutionMicroSeconds; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param int $resolutionMicroSeconds |
|
16
|
|
|
*/ |
|
17
|
14 |
|
public function __construct(int $resolutionMicroSeconds = 100000) |
|
18
|
|
|
{ |
|
19
|
14 |
|
$this->resolutionMicroSeconds = $resolutionMicroSeconds; |
|
20
|
14 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param \tomzx\Job\JobQueue $queue |
|
24
|
|
|
* @return \tomzx\Job\JobQueue |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function all(JobQueue $queue): JobQueue |
|
27
|
|
|
{ |
|
28
|
2 |
|
if ($queue->isEmpty()) { |
|
29
|
1 |
|
return $queue; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
$jobs = $queue; |
|
33
|
1 |
|
while (true) { |
|
34
|
1 |
|
$jobsLeft = new JobQueue(); |
|
35
|
1 |
|
foreach ($jobs->all() as $job) { |
|
36
|
1 |
|
if ( ! $job->isResolved()) { |
|
37
|
1 |
|
$jobsLeft->push($job); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
if ($jobsLeft->isEmpty()) { |
|
42
|
1 |
|
return $queue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$jobs = $jobsLeft; |
|
46
|
|
|
|
|
47
|
1 |
|
usleep($this->resolutionMicroSeconds); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \tomzx\Job\JobQueue $queue |
|
53
|
|
|
* @return \tomzx\Job\Job |
|
54
|
|
|
* @throws \InvalidArgumentException |
|
55
|
|
|
*/ |
|
56
|
7 |
|
public function any(JobQueue $queue): Job |
|
57
|
|
|
{ |
|
58
|
7 |
|
if ($queue->isEmpty()) { |
|
59
|
1 |
|
throw new InvalidArgumentException('Cannot await any job when there are none.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
while (true) { |
|
63
|
6 |
|
foreach ($queue->all() as $job) { |
|
64
|
6 |
|
if ($job->isResolved()) { |
|
65
|
6 |
|
return $job; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
usleep($this->resolutionMicroSeconds); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param \tomzx\Job\JobQueue $queue |
|
75
|
|
|
* @param int $count |
|
76
|
|
|
* @return \tomzx\Job\JobQueue |
|
77
|
|
|
* @throws \InvalidArgumentException |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function some(JobQueue $queue, int $count): JobQueue |
|
80
|
|
|
{ |
|
81
|
2 |
|
$jobs = $queue; |
|
82
|
2 |
|
$countJobsLeft = $jobs->count(); |
|
83
|
|
|
|
|
84
|
2 |
|
if ($count > $countJobsLeft) { |
|
85
|
1 |
|
throw new InvalidArgumentException('Awaiting more jobs than available.'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$jobsCompleted = new JobQueue(); |
|
89
|
1 |
|
while (true) { |
|
90
|
1 |
|
$jobsLeft = new JobQueue(); |
|
91
|
1 |
|
foreach ($jobs->all() as $job) { |
|
92
|
1 |
|
if ($job->isResolved()) { |
|
93
|
1 |
|
$jobsCompleted->push($job); |
|
94
|
1 |
|
if ($jobsCompleted->count() === $count) { |
|
95
|
1 |
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
1 |
|
$jobsLeft->push($job); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
if ($jobsCompleted->count() === $count) { |
|
103
|
1 |
|
return $jobsCompleted; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
$jobs = $jobsLeft; |
|
107
|
|
|
|
|
108
|
1 |
|
usleep($this->resolutionMicroSeconds); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|