forked from yiisoft/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanel.php
More file actions
139 lines (125 loc) · 3.56 KB
/
Copy pathPanel.php
File metadata and controls
139 lines (125 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\queue\debug;
use Exception;
use Yii;
use yii\base\NotSupportedException;
use yii\base\ViewContextInterface;
use yii\debug\Panel as BasePanel;
use yii\helpers\VarDumper;
use yii\queue\JobInterface;
use yii\queue\PushEvent;
use yii\queue\Queue;
/**
* Debug Panel.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class Panel extends BasePanel implements ViewContextInterface
{
private array $jobs = [];
/**
* @inheritdoc
*/
public function getName(): string
{
return 'Queue';
}
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
PushEvent::on(Queue::class, Queue::EVENT_AFTER_PUSH, function (PushEvent $event) {
$this->jobs[] = $this->getPushData($event);
});
}
/**
* @param PushEvent $event
* @return array
*/
protected function getPushData(PushEvent $event): array
{
$data = [];
foreach (Yii::$app->getComponents(false) as $id => $component) {
if ($component === $event->sender) {
$data['sender'] = $id;
break;
}
}
$data['id'] = $event->id;
$data['ttr'] = $event->ttr;
$data['delay'] = $event->delay;
$data['priority'] = $event->priority;
if ($event->job instanceof JobInterface) {
$data['class'] = get_class($event->job);
$data['properties'] = [];
foreach (get_object_vars($event->job) as $property => $value) {
$data['properties'][$property] = VarDumper::dumpAsString($value);
}
} else {
$data['data'] = VarDumper::dumpAsString($event->job);
}
return $data;
}
/**
* @inheritdoc
*/
public function save()
{
return ['jobs' => $this->jobs];
}
/**
* @inheritdoc
*/
public function getViewPath(): string
{
return __DIR__ . '/views';
}
/**
* @inheritdoc
*/
public function getSummary(): string
{
/** @psalm-var array{jobs: array} $this->data */
return Yii::$app->view->render('summary', [
'url' => $this->getUrl(),
'count' => isset($this->data['jobs']) ? count($this->data['jobs']) : 0,
], $this);
}
/**
* @inheritdoc
*/
public function getDetail(): string
{
/** @psalm-var array{jobs: array} $this->data */
$jobs = $this->data['jobs'] ?? [];
foreach ($jobs as &$job) {
/** @psalm-var array{sender: string, id: string|int} $job */
$job['status'] = 'unknown';
/** @var Queue $queue */
if ($queue = Yii::$app->get($job['sender'], false)) {
try {
/** @psalm-var Queue $queue */
if ($queue->isWaiting($job['id'])) {
$job['status'] = 'waiting';
} elseif ($queue->isReserved($job['id'])) {
$job['status'] = 'reserved';
} elseif ($queue->isDone($job['id'])) {
$job['status'] = 'done';
}
} catch (NotSupportedException|Exception $e) {
$job['status'] = $e->getMessage();
}
}
}
unset($job);
return Yii::$app->view->render('detail', compact('jobs'), $this);
}
}