Skip to content

Commit

Permalink
feat: pid locking + max duration/execution tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfaassen committed May 3, 2020
1 parent e53e1dd commit daaf2d0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -25,7 +25,8 @@
"symfony/mime": "^5.0",
"gnello/php-mattermost-driver": "^2.12",
"thibaud-dauce/mattermost-php": "^1.2",
"linkorb/config": "^1.0"
"linkorb/config": "^1.0",
"andrewfenn/pid-helper": "^0.1.0"
},
"require-dev": {
"symfony/dotenv": "^3.0|^4.0",
Expand Down
42 changes: 41 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion src/Console/Command/WorkerCommand.php
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use RuntimeException;
use Exo\Core\Utils\ArrayUtils;
use PidHelper\PidHelper;

class WorkerCommand extends AbstractCommand
{
Expand All @@ -29,6 +30,13 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$lock = new PidHelper('/run/user/' . posix_getuid() . '/', 'exo-worker.pid');
if (!$lock->lock()) {
$output->writeln('<error>Other worker process running, quiting.</error>');

return;
}

$exo = $this->getExo();

$workerName = 'camunda';
Expand All @@ -39,9 +47,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$className = 'Exo\\Worker\\' . $options['TYPE'] . 'Worker';
$adapter = new $className($exo, $options);

$startAt = time();
$maxRuntime = 60*30; // seconds
$executionCount = 0;
$maxExecutionCount = 100;

$running = true;
while ($running) {
echo "Running" . PHP_EOL;
echo "Running [executions: $executionCount]" . PHP_EOL;
$request = $adapter->popRequest();
if ($request) {

Expand All @@ -51,9 +64,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$response = $exo->handle($request);
echo (json_encode($response, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) . PHP_EOL);
$adapter->pushResponse($response);
$executionCount++;
} else {
sleep(3);
}

if (time() > ($startAt + $maxRuntime)) {
$running = false;
}
if ($executionCount>= $maxExecutionCount) {
$running = false;
}
}
$lock->unlock();
}
}

0 comments on commit daaf2d0

Please sign in to comment.