Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Batching #556

Merged
merged 13 commits into from
Jun 30, 2017
Prev Previous commit
Next Next commit
Dynamically change the number of children (#489)
* Dynamically change the number of children
Takashi Matsuo authored and dwsupplee committed Jun 30, 2017
commit 6f1b120c7545d5f7016bea67b109440e1fc5f90a
Empty file modified dev/sh/clear-ipc
100644 → 100755
Empty file.
32 changes: 28 additions & 4 deletions src/Core/Batch/BatchDaemon.php
Original file line number Diff line number Diff line change
@@ -104,17 +104,42 @@ public function runParent()
$jobs = $this->runner->getJobs();
foreach ($jobs as $job) {
if (! array_key_exists($job->getIdentifier(), $procs)) {
echo 'Spawning children' . PHP_EOL;
$procs[$job->getIdentifier()] = [];
for ($i = 0; $i < $job->getWorkerNum(); $i++) {
$procs[$job->getIdentifier()][] = proc_open(
}
while (count($procs[$job->getIdentifier()]) > $job->getWorkerNum()) {
// Stopping an excessive child.
echo 'Stopping an excessive child.' . PHP_EOL;
$proc = array_pop($procs[$job->getIdentifier()]);
$status = proc_get_status($proc);
// Keep sending SIGTERM until the child exits.
while ($status['running'] === true) {
@proc_terminate($proc);
usleep(50000);
$status = proc_get_status($proc);
}
@proc_close($proc);
}
for ($i = 0; $i < $job->getWorkerNum(); $i++) {
$needStart = false;
if (array_key_exists($i, $procs[$job->getIdentifier()])) {
$status = proc_get_status($procs[$job->getIdentifier()][$i]);
if ($status['running'] !== true) {
$needStart = true;
}
} else {
$needStart = true;
}
if ($needStart) {
echo 'Starting a child.' . PHP_EOL;
$procs[$job->getIdentifier()][$i] = proc_open(
sprintf('%s %d', $this->command, $job->getIdNum()),
$this->descriptorSpec,
$pipes
);
}
}
}
usleep(1000000); // Reload the config after 1 second
pcntl_signal_dispatch();
if ($this->shutdown) {
echo 'Shutting down, waiting for the children' . PHP_EOL;
@@ -133,7 +158,6 @@ public function runParent()
echo 'BatchDaemon exiting' . PHP_EOL;
exit;
}
usleep(1000000); // Reload the config after 1 second
// Reload the config
$this->runner->loadConfig();
}