Skip to content

fix: don't store a serialized exception on error #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,28 @@
/**
* Adds the performance log to the
*/
protected function updateLogs(TaskLog $taskLog)

Check warning on line 135 in src/TaskRunner.php

View workflow job for this annotation

GitHub Actions / infection / Mutation Testing

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ /** * Adds the performance log to the */ - protected function updateLogs(TaskLog $taskLog) + private function updateLogs(TaskLog $taskLog) { if (setting('Tasks.logPerformance') === false) { return;
{
if (setting('Tasks.logPerformance') === false) {
return;
}

// "unique" name will be returned if one wasn't set
$name = $taskLog->task->name;
$name = $taskLog->task->name;
$error = null;

if ($taskLog->error instanceof Throwable) {
$error = "Exception: {$taskLog->error->getCode()} - {$taskLog->error->getMessage()}" . PHP_EOL .
"file: {$taskLog->error->getFile()}:{$taskLog->error->getLine()}";
}

$data = [
'task' => $name,
'type' => $taskLog->task->getType(),
'start' => $taskLog->runStart->format('Y-m-d H:i:s'),
'duration' => $taskLog->duration(),
'output' => $taskLog->output ?? null,
'error' => serialize($taskLog->error ?? null),
'error' => $error,
];

// Get existing logs
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/TaskRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testRunWithSuccess()
'start' => date('Y-m-d H:i:s'),
'duration' => '0.00',
'output' => null,
'error' => serialize(null),
'error' => null,
],
];
$this->seeInDatabase('settings', [
Expand All @@ -79,6 +79,53 @@ public function testRunWithSuccess()
]);
}

/**
* @throws ReflectionException
*/
public function testRunWithError()
{
$task1 = (new Task('closure', static function () {
echo 'Task 1';
}))->daily('12:05am')->named('task1');
$task3 = (new Task('closure', static function () {
throw new Exception('Example exception in Task 3');
}))->daily('12:00am')->named('task3');

$runner = $this->getRunner([$task1, $task3]);

ob_start();
$runner->withTestTime('12:00am')->run();
$output = ob_get_clean();

// Only task 3 should have run
$this->assertSame('', $output);

// Get info about the exception
$reflection = new ReflectionFunction($task3->getAction());
$file = $reflection->getFileName();
$line = $reflection->getStartLine() + 1;

// Should have logged the stats
$expected = [
[
'task' => 'task3',
'type' => 'closure',
'start' => date('Y-m-d H:i:s'),
'duration' => '0.00',
'output' => null,
'error' => "Exception: 0 - Example exception in Task 3\nfile: {$file}:{$line}",
],
];
$this->seeInDatabase('settings', [
'class' => 'CodeIgniter\Tasks\Config\Tasks',
'key' => 'log-task3',
'value' => serialize($expected),
]);
$this->dontSeeInDatabase('settings', [
'key' => 'log-task1',
]);
}

protected function getRunner(array $tasks = [])
{
$scheduler = service('scheduler');
Expand Down