Skip to content

Commit 8f28d30

Browse files
committed
Added some callbacks to the manager.
Changed pollInterval to milliseconds.
1 parent 4c23b51 commit 8f28d30

File tree

2 files changed

+305
-47
lines changed

2 files changed

+305
-47
lines changed

src/ProcessManager.php

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ProcessManager
2121
protected $numberOfParallelProcesses;
2222

2323
/**
24-
* The interval to wait between the polls of the processes, in microseconds.
24+
* The interval to wait between the polls of the processes, in milliseconds.
2525
* @var int
2626
*/
2727
protected $pollInterval;
@@ -38,26 +38,110 @@ class ProcessManager
3838
*/
3939
protected $runningProcesses = [];
4040

41+
/**
42+
* The callback for when a process is about to be started.
43+
* @var callable|null
44+
*/
45+
protected $processStartCallback;
46+
47+
/**
48+
* The callback for when a process has finished.
49+
* @var callable|null
50+
*/
51+
protected $processFinishCallback;
52+
53+
/**
54+
* The callback for when a process has successfully finished with exit code 0.
55+
* @var callable|null
56+
*/
57+
protected $processSuccessCallback;
58+
59+
/**
60+
* The callback for when a process has failed to finish with exit code 0.
61+
* @var callable|null
62+
*/
63+
protected $processFailCallback;
64+
4165
/**
4266
* ProcessManager constructor.
4367
* @param int $numberOfParallelProcesses The number of processes to run in parallel.
44-
* @param int $pollInterval The interval to wait between the polls of the processes, in microseconds.
68+
* @param int $pollInterval The interval to wait between the polls of the processes, in milliseconds.
4569
*/
46-
public function __construct(int $numberOfParallelProcesses = 1, int $pollInterval = 1000)
70+
public function __construct(int $numberOfParallelProcesses = 1, int $pollInterval = 100)
4771
{
4872
$this->numberOfParallelProcesses = $numberOfParallelProcesses;
4973
$this->pollInterval = $pollInterval;
5074
}
5175

76+
/**
77+
* Sets the callback for when a process is about to be started.
78+
* @param callable|null $processStartCallback The callback, accepting a Process as only argument.
79+
* @return $this
80+
*/
81+
public function setProcessStartCallback(?callable $processStartCallback)
82+
{
83+
$this->processStartCallback = $processStartCallback;
84+
return $this;
85+
}
86+
87+
/**
88+
* Sets the callback for when a process has finished.
89+
* @param callable|null $processFinishCallback The callback, accepting a Process as only argument.
90+
* @return $this
91+
*/
92+
public function setProcessFinishCallback(?callable $processFinishCallback)
93+
{
94+
$this->processFinishCallback = $processFinishCallback;
95+
return $this;
96+
}
97+
98+
/**
99+
* Sets the callback for when a process has failed to finish with exit code 0.
100+
* @param callable|null $processSuccessCallback The callback, accepting a Process as only argument.
101+
* @return $this
102+
*/
103+
public function setProcessSuccessCallback(?callable $processSuccessCallback)
104+
{
105+
$this->processSuccessCallback = $processSuccessCallback;
106+
return $this;
107+
}
108+
109+
/**
110+
* Sets the callback for when a process has successfully finished with exit code 0.
111+
* @param callable|null $processFailCallback The callback, accepting a Process as only argument.
112+
* @return $this
113+
*/
114+
public function setProcessFailCallback(?callable $processFailCallback)
115+
{
116+
$this->processFailCallback = $processFailCallback;
117+
return $this;
118+
}
119+
120+
/**
121+
* Invokes the callback if it is an callable.
122+
* @param callable|null $callback
123+
* @param Process $process
124+
*/
125+
protected function invokeCallback(?callable $callback, Process $process): void
126+
{
127+
if (is_callable($callback)) {
128+
$callback($process);
129+
}
130+
}
131+
52132
/**
53133
* Adds a process to the manager.
54134
* @param Process $process
135+
* @param callable|null $callback
136+
* @param array $env
137+
* @return $this
55138
*/
56-
public function addProcess(Process $process, callable $callback = null, array $env = []): void
139+
public function addProcess(Process $process, callable $callback = null, array $env = [])
57140
{
58141
$this->pendingProcessData[] = [$process, $callback, $env];
59142
$this->executeNextPendingProcess();
60143
$this->checkRunningProcesses();
144+
return $this;
61145
}
62146

63147
/**
@@ -68,6 +152,7 @@ protected function executeNextPendingProcess(): void
68152
if ($this->canExecuteNextPendingRequest()) {
69153
list($process, $callback, $env) = array_shift($this->pendingProcessData);
70154
/* @var Process $process */
155+
$this->invokeCallback($this->processStartCallback, $process);
71156
$process->start($callback, $env);
72157
$this->runningProcesses[$process->getPid()] = $process;
73158
}
@@ -88,32 +173,49 @@ protected function canExecuteNextPendingRequest(): bool
88173
*/
89174
protected function checkRunningProcesses(): void
90175
{
91-
foreach ($this->runningProcesses as $pid => $process) {
92-
$process->checkTimeout();
93-
if (!$process->isRunning()) {
94-
unset($this->runningProcesses[$pid]);
95-
$this->executeNextPendingProcess();
96-
}
176+
foreach ($this->runningProcesses as $process) {
177+
$this->checkRunningProcess($process);
178+
}
179+
}
180+
181+
/**
182+
* Checks the process whether it has finished.
183+
* @param Process $process
184+
*/
185+
protected function checkRunningProcess(Process $process): void
186+
{
187+
$process->checkTimeout();
188+
if (!$process->isRunning()) {
189+
$this->invokeCallback(
190+
$process->isSuccessful() ? $this->processSuccessCallback : $this->processFailCallback,
191+
$process
192+
);
193+
$this->invokeCallback($this->processFinishCallback, $process);
194+
195+
unset($this->runningProcesses[$process->getPid()]);
196+
$this->executeNextPendingProcess();
97197
}
98198
}
99199

100200
/**
101201
* Waits for all processes to be finished.
202+
* @return $this
102203
*/
103-
public function waitForAllProcesses(): void
204+
public function waitForAllProcesses()
104205
{
105206
while ($this->hasUnfinishedProcesses()) {
106207
$this->sleep();
107208
$this->checkRunningProcesses();
108209
}
210+
return $this;
109211
}
110212

111213
/**
112214
* Sleeps for the next poll.
113215
*/
114216
protected function sleep(): void
115217
{
116-
usleep($this->pollInterval);
218+
usleep($this->pollInterval * 1000);
117219
}
118220

119221
/**

0 commit comments

Comments
 (0)