Updated \TamerLib\Objects > WorkerInstance to pass through the parent process arguments to the sub-process
This commit is contained in:
parent
ee70a733db
commit
dc02826079
1 changed files with 30 additions and 51 deletions
|
@ -6,9 +6,10 @@
|
|||
|
||||
use Exception;
|
||||
use LogLib\Log;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Process\PhpExecutableFinder;
|
||||
use Symfony\Component\Process\Process;
|
||||
use TamerLib\Classes\Utilities;
|
||||
use TamerLib\Exceptions\WorkerFailedException;
|
||||
|
||||
class WorkerInstance
|
||||
{
|
||||
|
@ -27,7 +28,6 @@
|
|||
*/
|
||||
private $process;
|
||||
|
||||
|
||||
/**
|
||||
* WorkerInstance constructor.
|
||||
*
|
||||
|
@ -73,12 +73,12 @@
|
|||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
Log::warning('net.nosial.tamerlib', sprintf('Failed to stop worker %s', $this->configuration->getWorkerId()), $e);
|
||||
Log::warning(Utilities::getName(), sprintf('Failed to stop worker %s', $this->configuration->getWorkerId()), $e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
$this->process = null;
|
||||
Log::debug('net.nosial.tamerlib', sprintf('Stopped worker %s', $this->configuration->getWorkerId()));
|
||||
Log::debug(Utilities::getName(), sprintf('Stopped worker %s', $this->configuration->getWorkerId()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,6 +86,7 @@
|
|||
* Starts the process for the worker.
|
||||
*
|
||||
* @return void
|
||||
* @throws WorkerFailedException
|
||||
*/
|
||||
public function start(): void
|
||||
{
|
||||
|
@ -94,71 +95,57 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// Get the current processes arguments and pass them to the new process.
|
||||
$pass_args = $_SERVER['argv'];
|
||||
if(count($pass_args) > 1)
|
||||
{
|
||||
array_shift($pass_args);
|
||||
}
|
||||
|
||||
$php_bin = (new PhpExecutableFinder())->find();
|
||||
$process = new Process([$php_bin, $this->path]);
|
||||
$process = new Process(array_merge([$php_bin, $this->path], $pass_args));
|
||||
$process->setEnv($this->configuration->toEnvironment());
|
||||
|
||||
try
|
||||
{
|
||||
Log::debug(Utilities::getName(), sprintf('Executing %s', $process->getCommandLine()));
|
||||
$process->start();
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
Log::warning('net.nosial.tamerlib', sprintf('Failed to start worker %s', $this->configuration->getWorkerId()), $e);
|
||||
return;
|
||||
throw new WorkerFailedException(sprintf('Failed to start worker %s', $this->configuration->getWorkerId()), $e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
$this->process = $process;
|
||||
Log::debug('net.nosial.tamerlib', sprintf('Started worker %s', $this->configuration->getWorkerId()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts the worker.
|
||||
*
|
||||
* @return void
|
||||
* @throws WorkerFailedException
|
||||
*/
|
||||
public function restart(): void
|
||||
{
|
||||
$this->stop();
|
||||
$this->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last output from the worker.
|
||||
*
|
||||
* @return string|null
|
||||
* @return string
|
||||
*/
|
||||
public function getOutput(): ?string
|
||||
public function getOutput(): string
|
||||
{
|
||||
if(is_null($this->process))
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
$output = $this->process->getIncrementalOutput();
|
||||
return empty($output) ? null : $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Monitors the worker for a given amount of time, or indefinitely if no timeout is given.
|
||||
* Throws an exception if the worker is not running.
|
||||
* Outputs the worker's output to the console.
|
||||
*
|
||||
* @param int $timeout
|
||||
* @return void
|
||||
*/
|
||||
public function monitor(int $timeout=0): void
|
||||
{
|
||||
$time_start = time();
|
||||
while(true)
|
||||
{
|
||||
if(!$this->isRunning())
|
||||
{
|
||||
throw new RuntimeException(sprintf('Worker %s is not running', $this->configuration->getWorkerId()));
|
||||
}
|
||||
|
||||
$output = $this->getOutput();
|
||||
if(!is_null($output))
|
||||
{
|
||||
print($output);
|
||||
}
|
||||
|
||||
if($timeout > 0 && (time() - $time_start) > $timeout)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Utilities::getLatestOutput($this->process);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -176,12 +163,4 @@
|
|||
{
|
||||
return $this->configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPath(): ?string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue