Skip to content

Commit 3821b32

Browse files
committed
Refactoring
1 parent eef6bf3 commit 3821b32

File tree

4 files changed

+42
-29
lines changed

4 files changed

+42
-29
lines changed

src/JobMonitor.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function events()
8282
Queue::EVENT_AFTER_PUSH => 'afterPush',
8383
Queue::EVENT_BEFORE_EXEC => 'beforeExec',
8484
Queue::EVENT_AFTER_EXEC => 'afterExec',
85-
Queue::EVENT_AFTER_ERROR => 'afterError',
85+
Queue::EVENT_AFTER_ERROR => 'afterExec',
8686
];
8787
}
8888

@@ -162,33 +162,6 @@ public function afterExec(ExecEvent $event)
162162
if (!$this->isActive($event->job)) {
163163
return;
164164
}
165-
166-
$push = static::$startedPush ?: $this->getPushRecord($event);
167-
if (!$push) {
168-
return;
169-
}
170-
if ($push->last_exec_id) {
171-
ExecRecord::updateAll([
172-
'finished_at' => time(),
173-
'memory_usage' => memory_get_peak_usage(),
174-
'error' => null,
175-
'result_data' => serialize($event->result),
176-
'retry' => false,
177-
], [
178-
'id' => $push->last_exec_id
179-
]);
180-
}
181-
}
182-
183-
/**
184-
* @param ExecEvent $event
185-
*/
186-
public function afterError(ExecEvent $event)
187-
{
188-
if (!$this->isActive($event->job)) {
189-
return;
190-
}
191-
192165
$push = static::$startedPush ?: $this->getPushRecord($event);
193166
if (!$push) {
194167
return;
@@ -202,7 +175,7 @@ public function afterError(ExecEvent $event)
202175
'finished_at' => time(),
203176
'memory_usage' => static::$startedPush ? memory_get_peak_usage() : null,
204177
'error' => $event->error,
205-
'result_data' => null,
178+
'result_data' => $event->result !== null ? serialize($event->result) : null,
206179
'retry' => $event->retry,
207180
], [
208181
'id' => $push->last_exec_id

tests/app/controllers/TestController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use tests\app\jobs\NotVoidJob;
66
use tests\app\jobs\RecursionJob;
77
use tests\app\jobs\SimpleJob;
8+
use tests\app\jobs\WrongJob;
89
use Yii;
910
use yii\web\Controller;
1011
use zhuravljov\yii\queue\monitor\base\FlashTrait;
@@ -47,6 +48,12 @@ public function actionPushNotVoidJob()
4748
return $this->success('Not void job has been pushed.')->goBackward();
4849
}
4950

51+
public function actionPushWrongJob()
52+
{
53+
$this->getQueue()->push(new WrongJob());
54+
return $this->success('Wrong job has been pushed.')->goBackward();
55+
}
56+
5057
/**
5158
* @return \yii\queue\Queue
5259
*/

tests/app/jobs/WrongJob.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace tests\app\jobs;
4+
5+
use Exception;
6+
use yii\queue\JobInterface;
7+
use yii\queue\RetryableJobInterface;
8+
9+
/**
10+
* Wrong Job
11+
*/
12+
class WrongJob implements JobInterface, RetryableJobInterface
13+
{
14+
public function execute($queue)
15+
{
16+
throw new Exception('Test exception.');
17+
}
18+
19+
public function getTtr()
20+
{
21+
return 10;
22+
}
23+
24+
public function canRetry($attempt, $error)
25+
{
26+
return $attempt < 2;
27+
}
28+
}

tests/app/views/layouts/test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
'url' => ['/test/push-not-void-job'],
7272
'linkOptions' => ['data-method' => 'post'],
7373
],
74+
[
75+
'label' => 'Push Wrong Job',
76+
'url' => ['/test/push-wrong-job'],
77+
'linkOptions' => ['data-method' => 'post'],
78+
],
7479
],
7580
],
7681
],

0 commit comments

Comments
 (0)