-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #3: Refactored background processing into autoloaded worker class.
- Loading branch information
1 parent
d3210e9
commit 51d7c35
Showing
3 changed files
with
115 additions
and
42 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
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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omnipedia\BackgroundTasks; | ||
|
||
use React\ChildProcess\Process; | ||
|
||
/** | ||
* Omnipedia background tasks worker process wrapper. | ||
*/ | ||
class WorkerProcess { | ||
|
||
/** | ||
* The ReactPHP Process instance for this command. | ||
* | ||
* @var \React\ChildProcess\Process | ||
*/ | ||
protected Process $process; | ||
|
||
/** | ||
* Constructor; instantiates a new ReactPHP Process. | ||
* | ||
* @param string $command | ||
* Command to run. | ||
*/ | ||
public function __construct(string $command) { | ||
|
||
$this->process = new Process($command); | ||
|
||
} | ||
|
||
/** | ||
* Start the configured process. | ||
*/ | ||
public function start(): void { | ||
|
||
print 'Running: ' . $this->process->getCommand() . \PHP_EOL; | ||
|
||
$this->process->start(); | ||
|
||
$this->process->on('exit', [$this, 'onProcessExit']); | ||
|
||
$this->process->stdout->on('data', [$this, 'onStdOutData']); | ||
|
||
$this->process->stderr->on('data', [$this, 'onStdErrData']); | ||
|
||
} | ||
|
||
/** | ||
* Process exit event handler. | ||
* | ||
* @param int|null $exitCode | ||
* | ||
* @param int|null $termSignal | ||
*/ | ||
public function onProcessExit(?int $exitCode, ?int $termSignal): void { | ||
|
||
if ($exitCode !== null) { | ||
return; | ||
} | ||
|
||
print \sprintf( | ||
'Command %s terminated with signal: %s', | ||
$this->process->getCommand(), $termSignal | ||
); | ||
|
||
} | ||
|
||
/** | ||
* stdout data event handler; prints output. | ||
* | ||
* @param string $chunk | ||
* Output data string/chunk. In other contexts this can also be binary data, | ||
* but in our case we're only expecting a string. | ||
*/ | ||
public function onStdOutData(string $chunk): void { | ||
|
||
print $chunk; | ||
|
||
} | ||
|
||
/** | ||
* stderr data event handler; prints output. | ||
* | ||
* @param string $chunk | ||
* Output data string/chunk. In other contexts this can also be binary data, | ||
* but in our case we're only expecting a string. | ||
*/ | ||
public function onStdErrData(string $chunk): void { | ||
|
||
if (empty(trim($chunk))) { | ||
return; | ||
} | ||
|
||
print $chunk; | ||
|
||
} | ||
|
||
} |