Add step type, step key, and updated timestamp to run logs

[MAILPOET-5568]
This commit is contained in:
Jan Jakes
2023-09-04 13:06:31 +02:00
committed by Aschepikov
parent c2027932c0
commit 370f4ee3ae
7 changed files with 77 additions and 34 deletions

View File

@ -5,6 +5,7 @@ namespace MailPoet\Automation\Engine\Control;
use Exception;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Data\AutomationRun;
use MailPoet\Automation\Engine\Data\AutomationRunLog;
use MailPoet\Automation\Engine\Data\StepRunArgs;
use MailPoet\Automation\Engine\Data\StepValidationArgs;
use MailPoet\Automation\Engine\Data\SubjectEntry;
@ -86,7 +87,7 @@ class StepHandler {
return;
}
$logger = $this->stepRunLoggerFactory->createLogger($runId, $stepId);
$logger = $this->stepRunLoggerFactory->createLogger($runId, $stepId, AutomationRunLog::TYPE_ACTION);
$logger->logStart();
try {
$this->handleStep($runId, $stepId, $runNumber);

View File

@ -25,16 +25,21 @@ class StepRunLogger {
/** @var AutomationRunLog|null */
private $log;
/** @var string */
private $stepType;
public function __construct(
AutomationRunLogStorage $automationRunLogStorage,
Hooks $hooks,
int $runId,
string $stepId
string $stepId,
string $stepType
) {
$this->automationRunLogStorage = $automationRunLogStorage;
$this->hooks = $hooks;
$this->runId = $runId;
$this->stepId = $stepId;
$this->stepType = $stepType;
}
public function logStart(): void {
@ -44,7 +49,7 @@ class StepRunLogger {
public function logSuccess(): void {
$log = $this->getLog();
$log->setStatus(AutomationRunLog::STATUS_COMPLETE);
$log->setCompletedAt(new DateTimeImmutable());
$log->setUpdatedAt(new DateTimeImmutable());
$this->triggerAfterRunHook($log);
$this->automationRunLogStorage->updateAutomationRunLog($log);
}
@ -53,7 +58,7 @@ class StepRunLogger {
$log = $this->getLog();
$log->setStatus(AutomationRunLog::STATUS_FAILED);
$log->setError($error);
$log->setCompletedAt(new DateTimeImmutable());
$log->setUpdatedAt(new DateTimeImmutable());
$this->triggerAfterRunHook($log);
$this->automationRunLogStorage->updateAutomationRunLog($log);
}
@ -64,7 +69,7 @@ class StepRunLogger {
}
if (!$this->log) {
$log = new AutomationRunLog($this->runId, $this->stepId);
$log = new AutomationRunLog($this->runId, $this->stepId, $this->stepType);
$id = $this->automationRunLogStorage->createAutomationRunLog($log);
$this->log = $this->automationRunLogStorage->getAutomationRunLog($id);
}

View File

@ -20,7 +20,7 @@ class StepRunLoggerFactory {
$this->hooks = $hooks;
}
public function createLogger(int $runId, string $stepId): StepRunLogger {
return new StepRunLogger($this->automationRunLogStorage, $this->hooks, $runId, $stepId);
public function createLogger(int $runId, string $stepId, string $stepType): StepRunLogger {
return new StepRunLogger($this->automationRunLogStorage, $this->hooks, $runId, $stepId, $stepType);
}
}

View File

@ -18,6 +18,11 @@ class AutomationRunLog {
self::STATUS_FAILED,
];
public const TYPE_ACTION = 'action';
public const TYPE_TRIGGER = 'trigger';
public const KEY_UNKNOWN = 'unknown';
/** @var int */
private $id;
@ -27,14 +32,20 @@ class AutomationRunLog {
/** @var string */
private $stepId;
/** @var string */
private $stepType;
/** @var string */
private $stepKey;
/** @var string */
private $status;
/** @var DateTimeImmutable */
private $startedAt;
/** @var DateTimeImmutable|null */
private $completedAt;
/** @var DateTimeImmutable */
private $updatedAt;
/** @var array */
private $data = [];
@ -45,12 +56,18 @@ class AutomationRunLog {
public function __construct(
int $automationRunId,
string $stepId,
string $stepType,
int $id = null
) {
$this->automationRunId = $automationRunId;
$this->stepId = $stepId;
$this->stepType = $stepType;
$this->stepKey = self::KEY_UNKNOWN;
$this->status = self::STATUS_RUNNING;
$this->startedAt = new DateTimeImmutable();
$now = new DateTimeImmutable();
$this->startedAt = $now;
$this->updatedAt = $now;
if ($id) {
$this->id = $id;
@ -69,6 +86,19 @@ class AutomationRunLog {
return $this->stepId;
}
public function getStepType(): string {
return $this->stepType;
}
public function getStepKey(): string {
return $this->stepKey;
}
public function setStepKey(string $stepKey): void {
$this->stepKey = $stepKey;
$this->updatedAt = new DateTimeImmutable();
}
public function getStatus(): string {
return $this->status;
}
@ -78,18 +108,19 @@ class AutomationRunLog {
throw new InvalidArgumentException("Invalid status '$status'.");
}
$this->status = $status;
$this->updatedAt = new DateTimeImmutable();
}
public function getStartedAt(): DateTimeImmutable {
return $this->startedAt;
}
public function getCompletedAt(): ?DateTimeImmutable {
return $this->completedAt;
public function getUpdatedAt(): DateTimeImmutable {
return $this->updatedAt;
}
public function setCompletedAt(DateTimeImmutable $completedAt): void {
$this->completedAt = $completedAt;
public function setUpdatedAt(DateTimeImmutable $updatedAt): void {
$this->updatedAt = $updatedAt;
}
public function getData(): array {
@ -102,6 +133,7 @@ class AutomationRunLog {
throw new InvalidArgumentException("Invalid data provided for key '$key'. Only scalar values and arrays of scalar values are allowed.");
}
$this->data[$key] = $value;
$this->updatedAt = new DateTimeImmutable();
}
public function getError(): array {
@ -110,13 +142,16 @@ class AutomationRunLog {
public function toArray(): array {
return [
'id' => $this->id,
'automation_run_id' => $this->automationRunId,
'step_id' => $this->stepId,
'step_type' => $this->stepType,
'step_key' => $this->stepKey,
'status' => $this->status,
'started_at' => $this->startedAt->format(DateTimeImmutable::W3C),
'completed_at' => $this->completedAt ? $this->completedAt->format(DateTimeImmutable::W3C) : null,
'error' => Json::encode($this->error),
'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C),
'data' => Json::encode($this->data),
'error' => Json::encode($this->error),
];
}
@ -127,14 +162,16 @@ class AutomationRunLog {
'code' => $error->getCode(),
'trace' => $error->getTrace(),
];
$this->updatedAt = new DateTimeImmutable();
}
public static function fromArray(array $data): self {
$log = new AutomationRunLog((int)$data['automation_run_id'], $data['step_id']);
$log = new AutomationRunLog((int)$data['automation_run_id'], $data['step_id'], $data['step_type']);
$log->id = (int)$data['id'];
$log->stepKey = $data['step_key'];
$log->status = $data['status'];
$log->startedAt = new DateTimeImmutable($data['started_at']);
$log->completedAt = $data['completed_at'] ? new DateTimeImmutable($data['completed_at']) : null;
$log->updatedAt = new DateTimeImmutable($data['updated_at']);
$log->data = Json::decode($data['data']);
$log->error = Json::decode($data['error']);
return $log;

View File

@ -51,7 +51,7 @@ class AutomationRunLogTest extends \MailPoetTest {
}
public function testItAllowsSettingSimpleData(): void {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$this->assertSame([], $log->getData());
$log->setData('key', 'value');
$data = $log->getData();
@ -60,7 +60,7 @@ class AutomationRunLogTest extends \MailPoetTest {
}
public function testItAllowsSettingArraysOfScalarValues(): void {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$data = [
'string',
11.1,
@ -75,7 +75,7 @@ class AutomationRunLogTest extends \MailPoetTest {
}
public function testItAllowsSettingMultidimensionalArraysOfScalarValues(): void {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$data = [
'values' => [
'string',
@ -92,7 +92,7 @@ class AutomationRunLogTest extends \MailPoetTest {
}
public function testItDoesNotAllowSettingDataThatIncludesClosures(): void {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$badData = [
function() {
echo 'closures cannot be serialized';
@ -104,7 +104,7 @@ class AutomationRunLogTest extends \MailPoetTest {
}
public function testItDoesNotAllowSettingObjectsForData(): void {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$object = new stdClass();
$object->key = 'value';
$this->expectException(\InvalidArgumentException::class);
@ -113,7 +113,7 @@ class AutomationRunLogTest extends \MailPoetTest {
}
public function testItDoesNotAllowSettingMultidimensionalArrayThatContainsNonScalarValue(): void {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$data = [
'test' => [
'multidimensional' => [
@ -175,26 +175,26 @@ class AutomationRunLogTest extends \MailPoetTest {
$automationRunLogs = $this->getLogsForAction();
expect($automationRunLogs)->count(1);
$log = $automationRunLogs[0];
expect($log->getStatus())->equals('completed');
expect($log->getStatus())->equals('complete');
}
public function testItAddsCompletedAtTimestampAfterRunningSuccessfully(): void {
public function testItSetsUpdatedAtTimestampAfterRunningSuccessfully(): void {
$this->wp->addAction(Hooks::AUTOMATION_RUN_LOG_AFTER_STEP_RUN, function(AutomationRunLog $log) {
expect($log->getCompletedAt())->null();
expect($log->getUpdatedAt())->null();
});
$automationRunLogs = $this->getLogsForAction();
expect($automationRunLogs)->count(1);
$log = $automationRunLogs[0];
expect($log->getCompletedAt())->isInstanceOf(\DateTimeImmutable::class);
expect($log->getUpdatedAt())->isInstanceOf(\DateTimeImmutable::class);
}
public function testItAddsCompletedAtTimestampAfterFailing(): void {
public function testItSetsUpdatedAtTimestampAfterFailing(): void {
$automationRunLogs = $this->getLogsForAction(function() {
throw new \Exception('error');
});
expect($automationRunLogs)->count(1);
$log = $automationRunLogs[0];
expect($log->getCompletedAt())->isInstanceOf(\DateTimeImmutable::class);
expect($log->getUpdatedAt())->isInstanceOf(\DateTimeImmutable::class);
}
public function testItLogsFailedStatusCorrectly(): void {

View File

@ -20,18 +20,18 @@ class AutomationRunLogStorageTest extends \MailPoetTest {
}
public function testItSavesAndRetrievesAsExpected() {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$log->setData('key', 'value');
$log->setData('key2', ['arrayData']);
$preSave = $log->toArray();
$id = $this->storage->createAutomationRunLog($log);
$fromDatabase = $this->storage->getAutomationRunLog($id);
$this->assertInstanceOf(AutomationRunLog::class, $fromDatabase);
expect($preSave)->equals($fromDatabase->toArray());
expect(['id' => $fromDatabase->getId()] + $preSave)->equals($fromDatabase->toArray());
}
public function testItCanStoreAnError() {
$log = new AutomationRunLog(1, 'step-id');
$log = new AutomationRunLog(1, 'step-id', AutomationRunLog::TYPE_ACTION);
$log->setError(new \Exception('test'));
$id = $this->storage->createAutomationRunLog($log);
$log = $this->storage->getAutomationRunLog($id);

View File

@ -180,7 +180,7 @@ class AutomationStorageTest extends \MailPoetTest {
$runId = $automationRunStorage->createAutomationRun($automationRun);
$runs[$type][] = $runId;
for ($logI = 0; $logI < 2; $logI++) {
$log = new AutomationRunLog($runId, "step-{$logI}");
$log = new AutomationRunLog($runId, "step-{$logI}", AutomationRunLog::TYPE_ACTION);
$logId = $automationRunLogStorage->createAutomationRunLog($log);
$runLogs[$type][] = $logId;
}