Skip to content

Commit

Permalink
Merge pull request #18 from neighborhoods/feature/KOJO-36
Browse files Browse the repository at this point in the history
Create a formatter for the \Throwable type and use when jobs Panic
  • Loading branch information
alexberryman authored Oct 11, 2018
2 parents 89d6aa1 + 68c304b commit 8c4bf04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Process/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Neighborhoods\Kojo\Process;

use Neighborhoods\Kojo\Foreman;
use Neighborhoods\Kojo\Scheduler;
use Neighborhoods\Kojo\Maintainer;
use Neighborhoods\Kojo\Selector;
use Neighborhoods\Kojo\Process;
use Neighborhoods\Kojo\Scheduler;
use Neighborhoods\Kojo\Selector;

class Job extends Forked implements JobInterface
{
Expand All @@ -17,20 +17,21 @@ class Job extends Forked implements JobInterface
use Selector\AwareTrait;
use Process\Pool\Factory\AwareTrait;

protected function _run(): Forked
protected function _run() : Forked
{
try{
try {
$this->_getSelector()->setProcess($this);
$this->_getMaintainer()->rescheduleCrashedJobs();
$this->_getScheduler()->scheduleStaticJobs();
$this->_getMaintainer()->updatePendingJobs();
$this->_getMaintainer()->deleteCompletedJobs();
$this->_getForeman()->workWorker();
}catch(\Throwable $throwable){
$this->_getLogger()->critical($throwable->getMessage());
} catch (\Throwable $throwable) {
$message = $this->_getLogger()->getLogFormatter()->getFormattedThrowableMessage($throwable);
$this->_getLogger()->critical($message);
$this->_setOrReplaceExitCode(255);
}

return $this;
}
}
}
14 changes: 14 additions & 0 deletions src/Process/Pool/Logger/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public function getFormattedMessage(MessageInterface $message) : string
}
}

public function getFormattedThrowableMessage(\Throwable $throwable): string
{
if ($throwable->getPrevious()) {
$previousType = get_class($throwable->getPrevious());
$previousMessage = $throwable->getPrevious()->getMessage();
$previousMessage = "{$previousType}: {$previousMessage}";
$message = implode(' ', [$throwable->getMessage(), $previousMessage]);
} else {
$message = $throwable->getMessage();
}

return $message;
}

protected function formatPipes(MessageInterface $message) : string
{
$processIdPaddingLength = $this->getProcessIdPadding();
Expand Down
2 changes: 2 additions & 0 deletions src/Process/Pool/Logger/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public function setProcessPathPadding(int $processPathPadding) : FormatterInterf
public function setProcessIdPadding(int $processIdPadding) : FormatterInterface;

public function setLogFormat(string $logFormat);

public function getFormattedThrowableMessage(\Throwable $throwable) : string;
}

0 comments on commit 8c4bf04

Please sign in to comment.