Skip to content

Commit 8ff0197

Browse files
authored
fix: don't store a serialized exception on error (#188)
1 parent a4db0d1 commit 8ff0197

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/TaskRunner.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,21 @@ protected function updateLogs(TaskLog $taskLog)
139139
}
140140

141141
// "unique" name will be returned if one wasn't set
142-
$name = $taskLog->task->name;
142+
$name = $taskLog->task->name;
143+
$error = null;
144+
145+
if ($taskLog->error instanceof Throwable) {
146+
$error = "Exception: {$taskLog->error->getCode()} - {$taskLog->error->getMessage()}" . PHP_EOL .
147+
"file: {$taskLog->error->getFile()}:{$taskLog->error->getLine()}";
148+
}
143149

144150
$data = [
145151
'task' => $name,
146152
'type' => $taskLog->task->getType(),
147153
'start' => $taskLog->runStart->format('Y-m-d H:i:s'),
148154
'duration' => $taskLog->duration(),
149155
'output' => $taskLog->output ?? null,
150-
'error' => serialize($taskLog->error ?? null),
156+
'error' => $error,
151157
];
152158

153159
// Get existing logs

tests/unit/TaskRunnerTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testRunWithSuccess()
6666
'start' => date('Y-m-d H:i:s'),
6767
'duration' => '0.00',
6868
'output' => null,
69-
'error' => serialize(null),
69+
'error' => null,
7070
],
7171
];
7272
$this->seeInDatabase('settings', [
@@ -79,6 +79,53 @@ public function testRunWithSuccess()
7979
]);
8080
}
8181

82+
/**
83+
* @throws ReflectionException
84+
*/
85+
public function testRunWithError()
86+
{
87+
$task1 = (new Task('closure', static function () {
88+
echo 'Task 1';
89+
}))->daily('12:05am')->named('task1');
90+
$task3 = (new Task('closure', static function () {
91+
throw new Exception('Example exception in Task 3');
92+
}))->daily('12:00am')->named('task3');
93+
94+
$runner = $this->getRunner([$task1, $task3]);
95+
96+
ob_start();
97+
$runner->withTestTime('12:00am')->run();
98+
$output = ob_get_clean();
99+
100+
// Only task 3 should have run
101+
$this->assertSame('', $output);
102+
103+
// Get info about the exception
104+
$reflection = new ReflectionFunction($task3->getAction());
105+
$file = $reflection->getFileName();
106+
$line = $reflection->getStartLine() + 1;
107+
108+
// Should have logged the stats
109+
$expected = [
110+
[
111+
'task' => 'task3',
112+
'type' => 'closure',
113+
'start' => date('Y-m-d H:i:s'),
114+
'duration' => '0.00',
115+
'output' => null,
116+
'error' => "Exception: 0 - Example exception in Task 3\nfile: {$file}:{$line}",
117+
],
118+
];
119+
$this->seeInDatabase('settings', [
120+
'class' => 'CodeIgniter\Tasks\Config\Tasks',
121+
'key' => 'log-task3',
122+
'value' => serialize($expected),
123+
]);
124+
$this->dontSeeInDatabase('settings', [
125+
'key' => 'log-task1',
126+
]);
127+
}
128+
82129
protected function getRunner(array $tasks = [])
83130
{
84131
$scheduler = service('scheduler');

0 commit comments

Comments
 (0)