1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/zhuravljov/yii2-queue-monitor |
4
|
|
|
* @copyright Copyright (c) 2017 Roman Zhuravlev |
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace zhuravljov\yii\queue\monitor\controllers; |
9
|
|
|
|
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
use yii\web\Controller; |
12
|
|
|
use yii\web\ForbiddenHttpException; |
13
|
|
|
use yii\web\NotFoundHttpException; |
14
|
|
|
use zhuravljov\yii\queue\monitor\base\FlashTrait; |
15
|
|
|
use zhuravljov\yii\queue\monitor\filters\WorkerFilter; |
16
|
|
|
use zhuravljov\yii\queue\monitor\Module; |
17
|
|
|
use zhuravljov\yii\queue\monitor\records\WorkerRecord; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Worker Controller |
21
|
|
|
* |
22
|
|
|
* @author Roman Zhuravlev <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class WorkerController extends Controller |
25
|
|
|
{ |
26
|
|
|
use FlashTrait; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Module |
30
|
|
|
*/ |
31
|
|
|
public $module; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function behaviors() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
'verb' => [ |
40
|
|
|
'class' => VerbFilter::class, |
41
|
|
|
'actions' => [ |
42
|
|
|
'stop' => ['post'], |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Worker List |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function actionIndex() |
54
|
|
|
{ |
55
|
|
|
return $this->render('index', [ |
56
|
|
|
'filter' => WorkerFilter::ensure(), |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Stops a worker |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @throws ForbiddenHttpException |
65
|
|
|
* @return \yii\web\Response |
66
|
|
|
*/ |
67
|
|
|
public function actionStop($id) |
68
|
|
|
{ |
69
|
|
|
if (!$this->module->canWorkerStop) { |
70
|
|
|
throw new ForbiddenHttpException('Stop is forbidden.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->findRecord($id)->stop(); |
74
|
|
|
return $this->success('Worker was stopped.') |
75
|
|
|
->redirect(['index']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $id |
80
|
|
|
* @throws NotFoundHttpException |
81
|
|
|
* @return WorkerRecord |
82
|
|
|
*/ |
83
|
|
|
protected function findRecord($id) |
84
|
|
|
{ |
85
|
|
|
if ($record = WorkerRecord::findOne($id)) { |
86
|
|
|
return $record; |
87
|
|
|
} |
88
|
|
|
throw new NotFoundHttpException('Record not found.'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|