Skip to content

Commit

Permalink
refactor: Remove hardcoded namespace. (#1026)
Browse files Browse the repository at this point in the history
* refactor: Remove hardcoded namespace.

* refactor: Move lines into constructor.

* refactor: Add a static variable.
  • Loading branch information
drupol authored Mar 25, 2021
1 parent 7f4d1fa commit 531ef3c
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Task/Base/Exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Robo\Task\Base;

use Robo\Common\ExecTrait;
use Closure;
use Robo\Contract\CommandInterface;
use Robo\Contract\PrintedInterface;
use Robo\Contract\SimulatedInterface;
Expand Down Expand Up @@ -45,12 +45,33 @@ class Exec extends BaseTask implements CommandInterface, PrintedInterface, Simul
*/
protected $command;

private static $isSetupStopRunningJob = false;

/**
* @param string|\Robo\Contract\CommandInterface $command
*/
public function __construct($command)
{
$this->command = $this->receiveCommand($command);

$this->setupStopRunningJobs();
}

private function setupStopRunningJobs()
{
if (self::$isSetupStopRunningJob === true) {
return;
}

$stopRunningJobs = Closure::fromCallable(['self', 'stopRunningJobs']);

if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, $stopRunningJobs);
}

register_shutdown_function($stopRunningJobs);

self::$isSetupStopRunningJob = true;
}

public function __destruct()
Expand Down Expand Up @@ -122,9 +143,3 @@ public function run()
return $result;
}
}

if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, ['Robo\Task\Base\Exec', 'stopRunningJobs']);
}

register_shutdown_function(['Robo\Task\Base\Exec', 'stopRunningJobs']);

0 comments on commit 531ef3c

Please sign in to comment.