/
ProcessManager.php
67 lines (60 loc) · 2.82 KB
/
ProcessManager.php
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
66
67
<?php
namespace Symbiote\QueuedJobs\Services;
use AsyncPHP\Doorman\Manager\ProcessManager as BaseProcessManager;
use SilverStripe\Core\Config\Configurable;
/**
* Class ProcessManager
*
* customise shell command to allow child tasks to persist even after manager process is terminated
* this lets the started jobs to finish properly in case the management process terminates
* fore example there are no more jobs to start or queue is paused
*
* @package Symbiote\QueuedJobs\Services
*/
class ProcessManager extends BaseProcessManager
{
use Configurable;
/**
* Enable / disable persistent child process
* If this is enabled the child processes can outlive the parent (manager process)
* This is what is needed for most cases as the manager process only starts new child processes
* Child processes on the other hand, actually execute queued jobs so they may take longer to execute
* Disabling this may cause the child processes to terminate prematurely which may result it multiple job
* execution retries / resumes
* Disable this option only if you are debugging a situation when a child process
* keeps hanging around for way too long (hours)
* Adding execution timeout to individual queued jobs is recommended to avoid such situation
*
* @config
* @var bool
*/
private static $persistent_child_process = true;
/**
* @param string $binary
* @param string $worker
* @param string $stdout
* @param string $stderr
* @return string
*/
protected function getCommand($binary, $worker, $stdout, $stderr) // phpcs:ignore SlevomatCodingStandard.TypeHints
{
// Nohup is short for “No Hangups.” It’s not a command that you run by itself.
// Nohup is a supplemental command that tells the Linux system not to stop another command once it has started.
// That means it’ll keep running until it’s done, even if the user that started it logs out
// Nohup is used here because the queue management process runs in a loop and it's starting new child processes
// which are running jobs
// It's possible to have the situation when queue management process terminates as it's no longer needed
// to create any new child processes but we still want the existing child processes to continue their work
// and finish the jbos which are already running
return sprintf('nohup %s %s %%s %s %s & echo $!', $binary, $worker, $stdout, $stderr);
}
public function __destruct()
{
if ($this->config()->get('persistent_child_process')) {
// Prevent background tasks from being killed when this script finishes
// this is an override for the default behaviour of killing background tasks
return;
}
parent::__destruct();
}
}