Skip to content

Commit

Permalink
Proper reaction for SIGTERM and SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
igormukhingmailcom committed May 3, 2015
1 parent 91fad5d commit 56aabc7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Workflow implements WorkflowInterface
*/
private $writers = [];

/**
* @var boolean For internal use
*/
protected $shouldStop = false;

/**
* Construct a workflow
*
Expand Down Expand Up @@ -116,8 +121,16 @@ public function process()
$writer->prepare();
}

$this->shouldStop = false;
pcntl_signal(SIGTERM, array($this, 'stop'));
pcntl_signal(SIGINT, array($this, 'stop'));

// Read all items
foreach ($this->reader as $index => $item) {

pcntl_signal_dispatch();
if ( $this->shouldStop ) break;

try {
foreach (clone $this->steps as $step) {
if (false === $step->process($item)) {
Expand Down Expand Up @@ -151,6 +164,14 @@ public function process()
return new Result($this->name, $startTime, new \DateTime, $count, $exceptions);
}

/**
* Stops processing and force return Result from process() function
*/
public function stop()
{
$this->shouldStop = true;
}

/**
* Set skipItemOnFailure.
*
Expand Down

0 comments on commit 56aabc7

Please sign in to comment.