-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG: Doorman manager child processing fix. (#305)
* BUG: Doorman manager child processing fix. * PR fixes * Configurable behaviour (PR fixes).
- Loading branch information
1 parent
e259183
commit b1ce613
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters