Skip to content

Commit

Permalink
Merge pull request #177 from igormukhingmailcom/pcntl
Browse files Browse the repository at this point in the history
Proper reaction for SIGTERM and SIGINT
  • Loading branch information
ddeboer committed May 4, 2015
2 parents 91fad5d + 577a1f8 commit a07d322
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 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,20 @@ public function process()
$writer->prepare();
}

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

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

if (is_callable('pcntl_signal_dispatch')) {
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 +168,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 a07d322

Please sign in to comment.