Skip to content

Commit f89636e

Browse files
committed
Timeout the workers
1 parent 77a8468 commit f89636e

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/ContinuousPipe/MessageBundle/Command/PullAndConsumeMessageCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
class PullAndConsumeMessageCommand extends ContainerAwareCommand
1919
{
20+
/**
21+
* Maximum runtime, in seconds. 30 minutes, in order to prevent any database-timeout related issue.
22+
*/
23+
const MAX_RUNTIME_IN_SECS = 1800;
24+
25+
/**
26+
* @var bool
27+
*/
2028
private $shouldStop = false;
2129

2230
/**
@@ -61,6 +69,7 @@ public function run(InputInterface $input, OutputInterface $output)
6169
pcntl_signal(SIGINT, [$this, 'stopCommand']);
6270

6371
$output->writeln('Waiting for messages...');
72+
$startTime = time();
6473

6574
while (!$this->shouldStop) {
6675
foreach ($this->messagePuller->pull() as $pulledMessage) {
@@ -91,7 +100,15 @@ public function run(InputInterface $input, OutputInterface $output)
91100
}
92101

93102
$output->writeln(sprintf('Finished consuming message "%s" (%s)', get_class($message), $pulledMessage->getIdentifier()));
103+
104+
// If an exception happened, break the loop to ensure to go back to the `shouldStop` condition
105+
if (isset($e)) {
106+
break;
107+
}
94108
}
109+
110+
$ranMoreThanRunTime = (time() - $startTime) > self::MAX_RUNTIME_IN_SECS;
111+
$this->shouldStop = $this->shouldStop || $ranMoreThanRunTime;
95112
}
96113

97114
$output->writeln('The worker has stopped (should have stopped: '.($this->shouldStop ? 'yes' : 'no').')');

0 commit comments

Comments
 (0)