Skip to content

Commit

Permalink
Merge pull request #67 from neighborhoods/feature/KOJO-99
Browse files Browse the repository at this point in the history
Pass exceptions as an object under the 'exception' key, and normalize that key if it's a throwable
  • Loading branch information
mucha55 committed Apr 8, 2019
2 parents 5b3b8e3 + 38b19bb commit 9ba59ed
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"symfony/finder": "^4.0",
"zendframework/zend-db": "^2.8",
"dragonmantank/cron-expression": "^2.0",
"doctrine/dbal": "^2.7"
"doctrine/dbal": "^2.7",
"monolog/monolog": "^1.24.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
Expand Down
12 changes: 6 additions & 6 deletions src/Message/Broker/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function waitForNewMessage(): BrokerInterface
0
);
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
throw $throwable;
}

Expand All @@ -43,7 +43,7 @@ public function hasMessage(): bool
$publishChannelLength = $this->getPublishChannelLength();
$subscriptionChannelLength = $this->getSubscriptionChannelLength();
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
throw $throwable;
}

Expand All @@ -58,7 +58,7 @@ public function getNextMessage(): string
$message = $this->_getRedisClient()->rPop($this->_getPublishChannelName());
}
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
throw $throwable;
}

Expand All @@ -70,7 +70,7 @@ public function getPublishChannelLength(): int
try {
$publishChannelLength = $this->_getRedisClient()->lLen($this->_getPublishChannelName());
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
throw $throwable;
}

Expand All @@ -82,7 +82,7 @@ public function getSubscriptionChannelLength(): int
try {
$subscriptionChannelLength = $this->_getRedisClient()->lLen($this->_getSubscriptionChannelName());
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
throw $throwable;
}

Expand All @@ -94,7 +94,7 @@ public function publishMessage($message): BrokerInterface
try {
$this->_getRedisClient()->lPush($this->_getPublishChannelName(), $message);
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
throw $throwable;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Process/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function _run(): Forked
$this->_getMaintainer()->deleteCompletedJobs();
$this->_getForeman()->workWorker();
} catch (\Throwable $throwable) {
$this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]);
$this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]);
$this->_setOrReplaceExitCode(255);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Process/Listener/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function addProcess(string $processTypeCode): Command
try {
$this->_getProcessPool()->addChildProcess($process);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}

return $this;
Expand Down
14 changes: 14 additions & 0 deletions src/Process/Pool/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Neighborhoods\Kojo\Process\Pool;

use Monolog\Formatter\NormalizerFormatter;
use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface;
use Neighborhoods\Kojo\ProcessInterface;
use Neighborhoods\Pylon\Data\Property\Defensive;
Expand All @@ -14,7 +15,11 @@ class Logger extends Log\AbstractLogger implements LoggerInterface
use Time\AwareTrait;
use Logger\Message\Factory\AwareTrait;
use Defensive\AwareTrait;

public const PROP_IS_ENABLED = 'is_enabled';
public const CONTEXT_KEY_EXCEPTION = 'exception';
public const CONTEXT_KEY_EXCEPTION_STRING = 'exception_string';

protected const LOG_DATE_TIME_FORMAT = 'D, d M y H:i:s.u T';

protected $log_formatter;
Expand Down Expand Up @@ -49,6 +54,15 @@ public function log($level, $message, array $context = [])
$logMessage->setProcessId($processId);
$logMessage->setProcessPath($this->_getProcess()->getPath());
$logMessage->setMessage($message);

if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION]
instanceof \Throwable){
$context[self::CONTEXT_KEY_EXCEPTION_STRING] = (string)$context[self::CONTEXT_KEY_EXCEPTION];
$normalizedException = (new NormalizerFormatter())->format([$context[self::CONTEXT_KEY_EXCEPTION]]);
unset($context[self::CONTEXT_KEY_EXCEPTION]);
$context[self::CONTEXT_KEY_EXCEPTION] = $normalizedException[0];
}

if (json_encode($context) === false) {
$logMessage->setContext([]);
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/Process/Pool/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function _listenerProcessExited(ListenerInterface $listenerProcess): S
$this->_getProcessPool()->addChildProcess($replacementListenerProcess);
} catch (Exception $forkedException) {
$this->_pauseListenerProcess($listenerProcess);
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ protected function _jobProcessExited(JobInterface $jobProcess): Strategy
try {
$this->_getProcessPool()->addChildProcess($replacementProcess);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}
if (!$this->_getProcessPool()->hasAlarm()) {
$this->_getProcessPool()->setAlarm($this->getMaxAlarmTime());
Expand All @@ -106,7 +106,7 @@ public function receivedAlarm(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($alarmProcess);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}
}
}
Expand All @@ -125,7 +125,7 @@ public function initializePool(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($process);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
if ($process instanceof CommandInterface) {
$this->_getProcessPool()->getProcess()->exit();
}
Expand All @@ -138,7 +138,7 @@ public function initializePool(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($fillProcess);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ protected function _unPauseListenerProcesses(): Strategy
$this->_getProcessPool()->addChildProcess($newListenerProcess);
unset($this->_pausedListenerProcesses[$processId]);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/Process/Pool/Strategy/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function childProcessExited(ProcessInterface $process): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($rootProcess);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
}
} else {
$className = get_class($process);
Expand All @@ -48,7 +48,7 @@ public function initializePool(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($process);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
$this->_getProcessPool()->getProcess()->exit();
}
}
Expand All @@ -59,7 +59,7 @@ public function initializePool(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($fillProcess);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
$this->_getProcessPool()->getProcess()->exit();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Process/Pool/Strategy/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function initializePool(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($process);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
$this->_getProcessPool()->getProcess()->exit();
}
}
Expand All @@ -63,7 +63,7 @@ public function initializePool(): StrategyInterface
try {
$this->_getProcessPool()->addChildProcess($fillProcess);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]);
$this->_getProcessPool()->getProcess()->exit();
}
}
Expand Down

0 comments on commit 9ba59ed

Please sign in to comment.