From d116e30ca237b6530f60924eca32e79dc42e7c2f Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Tue, 6 Aug 2019 14:30:23 -0500 Subject: [PATCH 1/4] serialize and log the Data\JobInterface after passing through the foreman --- src/Data/Job.php | 7 ++++- src/Data/JobInterface.php | 2 ++ src/Foreman.php | 1 + src/Process/Pool/Logger.php | 33 ++++++++++++++++++++ src/Process/Pool/Logger/Message.php | 24 ++++++++++++++ src/Process/Pool/Logger/MessageInterface.php | 4 +++ src/Process/Pool/LoggerInterface.php | 3 ++ 7 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/Data/Job.php b/src/Data/Job.php index ad533981..c7a7556c 100644 --- a/src/Data/Job.php +++ b/src/Data/Job.php @@ -6,7 +6,7 @@ use Neighborhoods\Kojo\Db\Model; use Neighborhoods\Pylon\TimeInterface; -class Job extends Model implements JobInterface +class Job extends Model implements JobInterface, \JsonSerializable { public function __construct() { @@ -324,4 +324,9 @@ public function getDeleteAfterDateTime(): \DateTime return new \DateTime($deleteAfterDateTimeString); } + + public function jsonSerialize() + { + return $this->_persistentProperties; + } } diff --git a/src/Data/JobInterface.php b/src/Data/JobInterface.php index 8481db14..171a59f6 100644 --- a/src/Data/JobInterface.php +++ b/src/Data/JobInterface.php @@ -119,4 +119,6 @@ public function getCompletedAtDateTime(): \DateTime; public function setDeleteAfterDateTime(\DateTime $deleteAfterDateTime): JobInterface; public function getDeleteAfterDateTime(): \DateTime; + + public function jsonSerialize(); } diff --git a/src/Foreman.php b/src/Foreman.php index 04a4dea4..75dba47a 100644 --- a/src/Foreman.php +++ b/src/Foreman.php @@ -43,6 +43,7 @@ protected function _workWorker(): ForemanInterface $this->setJob($this->_getSelector()->getWorkableJob()); $this->_getLocator()->setJob($this->_getJob()); try { + $this->_getLogger()->setJob($this->_getJob()); $this->_updateJobAsWorking(); $this->_runWorker(); $this->_updateJobAfterWork(); diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index ff152b67..57545ac8 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -4,6 +4,7 @@ namespace Neighborhoods\Kojo\Process\Pool; use Monolog\Formatter\NormalizerFormatter; +use Neighborhoods\Kojo\Data\JobInterface; use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface; use Neighborhoods\Kojo\ProcessInterface; use Neighborhoods\Pylon\Data\Property\Defensive; @@ -24,6 +25,8 @@ class Logger extends Log\AbstractLogger implements LoggerInterface protected $log_formatter; protected $level_filter_mask; + /** @var JobInterface */ + protected $job; public function setProcess(ProcessInterface $process): LoggerInterface { @@ -32,6 +35,31 @@ public function setProcess(ProcessInterface $process): LoggerInterface return $this; } + public function hasJob(): bool + { + return isset($this->job); + } + + public function getJob() : JobInterface + { + if ($this->job === null) { + throw new \LogicException('Logger job has not been set.'); + } + + return $this->job; + } + + public function setJob(JobInterface $job) : LoggerInterface + { + if ($this->job !== null) { + throw new \LogicException('Logger job is already set.'); + } + + $this->job = $job; + + return $this; + } + protected function _getProcess(): ProcessInterface { return $this->_read(ProcessInterface::class); @@ -53,6 +81,11 @@ public function log($level, $message, array $context = []) $logMessage->setLevel($level); $logMessage->setProcessId($processId); $logMessage->setProcessPath($this->_getProcess()->getPath()); + + if ($this->hasJob()){ + $logMessage->setKojoJob($this->getJob()); + } + $logMessage->setMessage($message); if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] diff --git a/src/Process/Pool/Logger/Message.php b/src/Process/Pool/Logger/Message.php index 1fb0dcf4..752a048e 100644 --- a/src/Process/Pool/Logger/Message.php +++ b/src/Process/Pool/Logger/Message.php @@ -4,6 +4,8 @@ namespace Neighborhoods\Kojo\Process\Pool\Logger; +use Neighborhoods\Kojo\Data\JobInterface; + class Message implements MessageInterface, \JsonSerializable { const KEY_TIME = 'time'; @@ -11,11 +13,13 @@ class Message implements MessageInterface, \JsonSerializable const KEY_PROCESS_ID = 'process_id'; const KEY_PROCESS_PATH = 'process_path'; const KEY_MESSAGE = 'message'; + const KEY_JOB_METADATA = 'kojo_job'; protected $time; protected $level; protected $process_id; protected $process_path; + protected $kojo_job = []; protected $message; protected $context; protected $context_json_last_error; @@ -164,4 +168,24 @@ public function setContextJsonLastError(int $context_json_last_error): MessageIn return $this; } + + public function getKojoJob() : JobInterface + { + if ($this->kojo_job === null) { + throw new \LogicException('Message job_metadata has not been set.'); + } + + return $this->kojo_job; + } + + public function setKojoJob(JobInterface $kojo_job) : MessageInterface + { + if ($this->kojo_job !== []) { + throw new \LogicException('Message job_metadata is already set.'); + } + + $this->kojo_job = $kojo_job; + + return $this; + } } diff --git a/src/Process/Pool/Logger/MessageInterface.php b/src/Process/Pool/Logger/MessageInterface.php index 56dcc81b..e8acc548 100644 --- a/src/Process/Pool/Logger/MessageInterface.php +++ b/src/Process/Pool/Logger/MessageInterface.php @@ -4,6 +4,8 @@ namespace Neighborhoods\Kojo\Process\Pool\Logger; +use Neighborhoods\Kojo\Data\JobInterface; + interface MessageInterface { public function getTime(): string; @@ -33,4 +35,6 @@ public function getContext(): array; public function getContextJsonLastError(): int; public function setContextJsonLastError(int $context_json_last_error): MessageInterface; + + public function setKojoJob(JobInterface $job_metadata) : MessageInterface; } diff --git a/src/Process/Pool/LoggerInterface.php b/src/Process/Pool/LoggerInterface.php index 6b768ec3..5067b3b0 100644 --- a/src/Process/Pool/LoggerInterface.php +++ b/src/Process/Pool/LoggerInterface.php @@ -3,6 +3,7 @@ namespace Neighborhoods\Kojo\Process\Pool; +use Neighborhoods\Kojo\Data\JobInterface; use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface; use Neighborhoods\Kojo\ProcessInterface; use Psr\Log; @@ -18,4 +19,6 @@ public function getLogFormatter() : FormatterInterface; public function setLogFormatter(FormatterInterface $log_formatter) : LoggerInterface; public function setLevelFilterMask(array $level_filter_mask): LoggerInterface; + + public function setJob(JobInterface $job) : LoggerInterface; } From 8ebd46180a468776f92e5748d8548bb8f49ecc9d Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Thu, 8 Aug 2019 10:41:00 -0500 Subject: [PATCH 2/4] remove array initialization since ElasticSearch will work with 'null' --- src/Process/Pool/Logger/Message.php | 9 ++++----- src/Process/Pool/Logger/MessageInterface.php | 2 ++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Process/Pool/Logger/Message.php b/src/Process/Pool/Logger/Message.php index 752a048e..efcd0430 100644 --- a/src/Process/Pool/Logger/Message.php +++ b/src/Process/Pool/Logger/Message.php @@ -13,13 +13,12 @@ class Message implements MessageInterface, \JsonSerializable const KEY_PROCESS_ID = 'process_id'; const KEY_PROCESS_PATH = 'process_path'; const KEY_MESSAGE = 'message'; - const KEY_JOB_METADATA = 'kojo_job'; protected $time; protected $level; protected $process_id; protected $process_path; - protected $kojo_job = []; + protected $kojo_job; protected $message; protected $context; protected $context_json_last_error; @@ -172,7 +171,7 @@ public function setContextJsonLastError(int $context_json_last_error): MessageIn public function getKojoJob() : JobInterface { if ($this->kojo_job === null) { - throw new \LogicException('Message job_metadata has not been set.'); + throw new \LogicException('Message kojo_job has not been set.'); } return $this->kojo_job; @@ -180,8 +179,8 @@ public function getKojoJob() : JobInterface public function setKojoJob(JobInterface $kojo_job) : MessageInterface { - if ($this->kojo_job !== []) { - throw new \LogicException('Message job_metadata is already set.'); + if ($this->kojo_job !== null) { + throw new \LogicException('Message kojo_job is already set.'); } $this->kojo_job = $kojo_job; diff --git a/src/Process/Pool/Logger/MessageInterface.php b/src/Process/Pool/Logger/MessageInterface.php index e8acc548..225dfe6e 100644 --- a/src/Process/Pool/Logger/MessageInterface.php +++ b/src/Process/Pool/Logger/MessageInterface.php @@ -36,5 +36,7 @@ public function getContextJsonLastError(): int; public function setContextJsonLastError(int $context_json_last_error): MessageInterface; + public function getKojoJob() : JobInterface; + public function setKojoJob(JobInterface $job_metadata) : MessageInterface; } From 9d4982cc22f41502ed8050ce57610234649e7b88 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Thu, 8 Aug 2019 11:55:11 -0500 Subject: [PATCH 3/4] shorten timestamp, remove PID since it's in the process path, add type_code and assigned_state --- src/Process/Pool/Logger/Formatter.php | 41 ++++++++++--------- src/Process/Pool/Logger/Formatter.yml | 4 +- .../Pool/Logger/FormatterInterface.php | 2 - src/Process/Pool/Logger/Message.php | 5 +++ src/Process/Pool/Logger/MessageInterface.php | 2 + 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/Process/Pool/Logger/Formatter.php b/src/Process/Pool/Logger/Formatter.php index 91711587..b579fe42 100644 --- a/src/Process/Pool/Logger/Formatter.php +++ b/src/Process/Pool/Logger/Formatter.php @@ -3,16 +3,14 @@ namespace Neighborhoods\Kojo\Process\Pool\Logger; +use DateTime; use Neighborhoods\Pylon\Data\Property\Defensive; class Formatter implements FormatterInterface { use Defensive\AwareTrait; - const PAD_PID = 6; - const PAD_PATH = 50; const PROP_PROCESS_PATH_PADDING = 'process_path_padding'; - const PROP_PROCESS_ID_PADDING = 'process_id_padding'; const PROP_LOG_FORMAT = 'log_format'; const LOG_FORMAT_PIPES = 'pipes'; @@ -46,14 +44,29 @@ public function getFormattedThrowableMessage(\Throwable $throwable): string protected function formatPipes(MessageInterface $message) : string { - $processIdPaddingLength = $this->getProcessIdPadding(); $processPathPaddingLength = $this->getProcessPathPadding(); - $processID = str_pad($message->getProcessId(), $processIdPaddingLength, ' ', STR_PAD_LEFT); $processPath = str_pad($message->getProcessPath(), $processPathPaddingLength, ' '); - $level = str_pad($message->getLevel(), 12, ' '); - - return implode(' | ', [$message->getTime(), $level, $processID, $processPath, $message->getMessage()]); + $level = str_pad($message->getLevel(), 8, ' '); + $kojoJobTypeCode = str_pad($message->hasKojoJob() ? $message->getKojoJob()->getTypeCode() : '', 26, ' '); + $kojoJobAssignedState = str_pad( + $message->hasKojoJob() ? $message->getKojoJob()->getAssignedState() : '', + 20, + ' ' + ); + + + return implode( + ' | ', + [ + DateTime::createFromFormat('D, d M y H:i:s.u T', $message->getTime())->format('H:i:s.u'), + $level, + $processPath, + $kojoJobTypeCode, + $kojoJobAssignedState, + $message->getMessage(), + ] + ); } protected function formatJson(MessageInterface $message) : string @@ -78,18 +91,6 @@ protected function getProcessPathPadding() : int return $this->_read(self::PROP_PROCESS_PATH_PADDING); } - public function setProcessIdPadding(int $processIdPadding) : FormatterInterface - { - $this->_create(self::PROP_PROCESS_ID_PADDING, $processIdPadding); - - return $this; - } - - protected function getProcessIdPadding() : int - { - return $this->_read(self::PROP_PROCESS_ID_PADDING); - } - public function setLogFormat(string $logFormat) { $this->_create(self::PROP_LOG_FORMAT, $logFormat); diff --git a/src/Process/Pool/Logger/Formatter.yml b/src/Process/Pool/Logger/Formatter.yml index ffcc8b5b..db59737b 100644 --- a/src/Process/Pool/Logger/Formatter.yml +++ b/src/Process/Pool/Logger/Formatter.yml @@ -1,11 +1,9 @@ parameters: - process.pool.logger.formatter.process_id_padding: 6 - process.pool.logger.formatter.path_padding: 80 + process.pool.logger.formatter.path_padding: 65 process.pool.logger.formatter.log_format: !php/const \Neighborhoods\Kojo\Process\Pool\Logger\Formatter::LOG_FORMAT_JSON services: neighborhoods.kojo.process.log_formatter: class: Neighborhoods\Kojo\Process\Pool\Logger\Formatter calls: - - [setProcessIdPadding, ['%process.pool.logger.formatter.process_id_padding%']] - [setProcessPathPadding, ['%process.pool.logger.formatter.path_padding%']] - [setLogFormat, ['%process.pool.logger.formatter.log_format%']] diff --git a/src/Process/Pool/Logger/FormatterInterface.php b/src/Process/Pool/Logger/FormatterInterface.php index a6760fcb..e1752f2f 100644 --- a/src/Process/Pool/Logger/FormatterInterface.php +++ b/src/Process/Pool/Logger/FormatterInterface.php @@ -11,8 +11,6 @@ public function getFormattedMessage(MessageInterface $message): string; public function setProcessPathPadding(int $processPathPadding): FormatterInterface; - public function setProcessIdPadding(int $processIdPadding): FormatterInterface; - public function setLogFormat(string $logFormat); /** diff --git a/src/Process/Pool/Logger/Message.php b/src/Process/Pool/Logger/Message.php index efcd0430..1baf4d90 100644 --- a/src/Process/Pool/Logger/Message.php +++ b/src/Process/Pool/Logger/Message.php @@ -168,6 +168,11 @@ public function setContextJsonLastError(int $context_json_last_error): MessageIn return $this; } + public function hasKojoJob(): bool + { + return isset($this->kojo_job); + } + public function getKojoJob() : JobInterface { if ($this->kojo_job === null) { diff --git a/src/Process/Pool/Logger/MessageInterface.php b/src/Process/Pool/Logger/MessageInterface.php index 225dfe6e..ce2646c5 100644 --- a/src/Process/Pool/Logger/MessageInterface.php +++ b/src/Process/Pool/Logger/MessageInterface.php @@ -39,4 +39,6 @@ public function setContextJsonLastError(int $context_json_last_error): MessageIn public function getKojoJob() : JobInterface; public function setKojoJob(JobInterface $job_metadata) : MessageInterface; + + public function hasKojoJob() : bool; } From 79fb7ce73a758126ab0557fa221f9117cb31c1f7 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Thu, 8 Aug 2019 14:20:22 -0500 Subject: [PATCH 4/4] $job_metadata -> $kojo_job to match method name --- src/Process/Pool/Logger/MessageInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Process/Pool/Logger/MessageInterface.php b/src/Process/Pool/Logger/MessageInterface.php index ce2646c5..6b4137b9 100644 --- a/src/Process/Pool/Logger/MessageInterface.php +++ b/src/Process/Pool/Logger/MessageInterface.php @@ -38,7 +38,7 @@ public function setContextJsonLastError(int $context_json_last_error): MessageIn public function getKojoJob() : JobInterface; - public function setKojoJob(JobInterface $job_metadata) : MessageInterface; + public function setKojoJob(JobInterface $kojo_job) : MessageInterface; public function hasKojoJob() : bool; }