Skip to content

Commit

Permalink
Merge pull request #91 from neighborhoods/KOJO-96-no-more-exception-c…
Browse files Browse the repository at this point in the history
…rash-looping

KOJO-96 | Panic job instead of crashing on userspace exceptions
  • Loading branch information
mucha55 committed Dec 26, 2019
2 parents 87fe80c + 2818ccf commit 595adf3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
30 changes: 8 additions & 22 deletions src/Foreman.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Foreman implements ForemanInterface
use Locator\AwareTrait;
use Update\Work\Factory\AwareTrait;
use Update\Panic\Factory\AwareTrait;
use Update\Crash\Factory\AwareTrait;
use Update\Complete\Success\Factory\AwareTrait;
use Defensive\AwareTrait;
use Logger\AwareTrait;
Expand All @@ -46,11 +45,15 @@ protected function _workWorker(): ForemanInterface
try {
$this->getProcessPoolLoggerMessageMetadataBuilder()->setJob($this->_getJob());
$this->_updateJobAsWorking();
restore_error_handler();
$this->_runWorker();
set_error_handler(new ErrorHandler());
$this->_updateJobAfterWork();
} catch (Locator\Exception | \Error $throwable) {
} catch (\Throwable $throwable) {
set_error_handler(new ErrorHandler());
$this->_panicJob();
$jobId = $this->_getJob()->getId();
// exiting with nonzero code will give the Root's SIGCHLD handler some info
throw new \RuntimeException("Panicking job with ID[$jobId].", 0, $throwable);
}
$this->_getSemaphore()->releaseLock($this->_getNewJobOwnerResource($this->_getJob()));
Expand Down Expand Up @@ -84,17 +87,9 @@ protected function _injectRDBMSConnectionService(): ForemanInterface

protected function _runWorker(): ForemanInterface
{
try {
restore_error_handler();
$this->_injectWorkerService();
$this->_injectRDBMSConnectionService();
call_user_func($this->_getLocator()->getCallable());
set_error_handler(new ErrorHandler());
} catch (\Exception $exception) {
set_error_handler(new ErrorHandler());
$this->_crashJob();
throw $exception;
}
$this->_injectWorkerService();
$this->_injectRDBMSConnectionService();
call_user_func($this->_getLocator()->getCallable());

return $this;
}
Expand Down Expand Up @@ -152,13 +147,4 @@ protected function _panicJob(): ForemanInterface

return $this;
}

protected function _crashJob(): ForemanInterface
{
$updateCrash = $this->_getServiceUpdateCrashFactory()->create();
$updateCrash->setJob($this->_getJob());
$updateCrash->save();

return $this;
}
}
1 change: 0 additions & 1 deletion src/Foreman.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
- [addSemaphoreResourceFactory, ['@semaphore.resource.factory-job']]
- [setServiceUpdateWorkFactory, ['@service.update.work.factory']]
- [setServiceUpdatePanicFactory, ['@service.update.panic.factory']]
- [setServiceUpdateCrashFactory, ['@service.update.crash.factory']]
- [setServiceUpdateCompleteSuccessFactory, ['@service.update.complete.success.factory']]
- [setLogger, ['@process.pool.logger']]
- [setProcessPoolLoggerMessageMetadataBuilder, ['@neighborhoods.kojo.process.pool.logger.message.metadata.builder']]
Expand Down

0 comments on commit 595adf3

Please sign in to comment.