Skip to content

Commit

Permalink
Merge pull request #73 from neighborhoods/feature/KOJO-112-api
Browse files Browse the repository at this point in the history
serialize and log the Data\JobInterface after passing through the foreman
  • Loading branch information
alexberryman committed Aug 27, 2019
2 parents 2e084cf + 79fb7ce commit 75e95e2
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/Data/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -324,4 +324,9 @@ public function getDeleteAfterDateTime(): \DateTime

return new \DateTime($deleteAfterDateTimeString);
}

public function jsonSerialize()
{
return $this->_persistentProperties;
}
}
2 changes: 2 additions & 0 deletions src/Data/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,6 @@ public function getCompletedAtDateTime(): \DateTime;
public function setDeleteAfterDateTime(\DateTime $deleteAfterDateTime): JobInterface;

public function getDeleteAfterDateTime(): \DateTime;

public function jsonSerialize();
}
1 change: 1 addition & 0 deletions src/Foreman.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
33 changes: 33 additions & 0 deletions src/Process/Pool/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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);
Expand All @@ -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]
Expand Down
41 changes: 21 additions & 20 deletions src/Process/Pool/Logger/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/Process/Pool/Logger/Formatter.yml
Original file line number Diff line number Diff line change
@@ -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%']]
2 changes: 0 additions & 2 deletions src/Process/Pool/Logger/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Process/Pool/Logger/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Neighborhoods\Kojo\Process\Pool\Logger;

use Neighborhoods\Kojo\Data\JobInterface;

class Message implements MessageInterface, \JsonSerializable
{
const KEY_TIME = 'time';
Expand All @@ -16,6 +18,7 @@ class Message implements MessageInterface, \JsonSerializable
protected $level;
protected $process_id;
protected $process_path;
protected $kojo_job;
protected $message;
protected $context;
protected $context_json_last_error;
Expand Down Expand Up @@ -164,4 +167,29 @@ 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) {
throw new \LogicException('Message kojo_job has not been set.');
}

return $this->kojo_job;
}

public function setKojoJob(JobInterface $kojo_job) : MessageInterface
{
if ($this->kojo_job !== null) {
throw new \LogicException('Message kojo_job is already set.');
}

$this->kojo_job = $kojo_job;

return $this;
}
}
8 changes: 8 additions & 0 deletions src/Process/Pool/Logger/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Neighborhoods\Kojo\Process\Pool\Logger;

use Neighborhoods\Kojo\Data\JobInterface;

interface MessageInterface
{
public function getTime(): string;
Expand Down Expand Up @@ -33,4 +35,10 @@ public function getContext(): array;
public function getContextJsonLastError(): int;

public function setContextJsonLastError(int $context_json_last_error): MessageInterface;

public function getKojoJob() : JobInterface;

public function setKojoJob(JobInterface $kojo_job) : MessageInterface;

public function hasKojoJob() : bool;
}
3 changes: 3 additions & 0 deletions src/Process/Pool/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

0 comments on commit 75e95e2

Please sign in to comment.