Issues (35)

src/controllers/JobController.php (1 issue)

Labels
Severity
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\JobFilter;
16
use zhuravljov\yii\queue\monitor\Module;
17
use zhuravljov\yii\queue\monitor\records\PushRecord;
18
19
/**
20
 * Class JobController
21
 *
22
 * @author Roman Zhuravlev <[email protected]>
23
 */
24
class JobController extends Controller
25
{
26
    use FlashTrait;
0 ignored issues
show
The trait zhuravljov\yii\queue\monitor\base\FlashTrait requires the property $session which is not provided by zhuravljov\yii\queue\mon...ntrollers\JobController.
Loading history...
27
28
    /**
29
     * @var Module
30
     */
31
    public $module;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function behaviors()
37
    {
38
        return [
39
            'verbs' => [
40
                'class' => VerbFilter::class,
41
                'actions' => [
42
                    'push' => ['post'],
43
                    'stop' => ['post'],
44
                ],
45
            ],
46
        ];
47
    }
48
49
    /**
50
     * Pushed jobs
51
     *
52
     * @return mixed
53
     */
54
    public function actionIndex()
55
    {
56
        return $this->render('index', [
57
            'filter' => JobFilter::ensure(),
58
        ]);
59
    }
60
61
    /**
62
     * Job view
63
     *
64
     * @param int $id
65
     * @return mixed
66
     */
67
    public function actionView($id)
68
    {
69
        $record = $this->findRecord($id);
70
        if ($record->lastExec && $record->lastExec->isFailed()) {
71
            return $this->redirect(['view-attempts', 'id' => $record->id]);
72
        }
73
        return $this->redirect(['view-details', 'id' => $record->id]);
74
    }
75
76
    /**
77
     * Push details
78
     *
79
     * @param int $id
80
     * @return mixed
81
     */
82
    public function actionViewDetails($id)
83
    {
84
        return $this->render('view-details', [
85
            'record' => $this->findRecord($id),
86
        ]);
87
    }
88
89
    /**
90
     * Push environment
91
     *
92
     * @param int $id
93
     * @return mixed
94
     */
95
    public function actionViewContext($id)
96
    {
97
        return $this->render('view-context', [
98
            'record' => $this->findRecord($id),
99
        ]);
100
    }
101
102
    /**
103
     * Job object data
104
     *
105
     * @param int $id
106
     * @return mixed
107
     */
108
    public function actionViewData($id)
109
    {
110
        return $this->render('view-data', [
111
            'record' => $this->findRecord($id),
112
        ]);
113
    }
114
115
    /**
116
     * Attempts
117
     *
118
     * @param int $id
119
     * @return mixed
120
     */
121
    public function actionViewAttempts($id)
122
    {
123
        return $this->render('view-attempts', [
124
            'record' => $this->findRecord($id),
125
        ]);
126
    }
127
128
    /**
129
     * Pushes a job again
130
     *
131
     * @param int $id
132
     * @throws
133
     * @return mixed
134
     */
135
    public function actionPush($id)
136
    {
137
        if (!$this->module->canPushAgain) {
138
            throw new ForbiddenHttpException(Module::t('notice', 'Push is forbidden.'));
139
        }
140
141
        $record = $this->findRecord($id);
142
143
        if (!$record->isSenderValid()) {
144
            $error = Module::t(
145
                'notice',
146
                'The job isn\'t pushed because {sender} component isn\'t found.',
147
                ['sender'=>$record->sender_name]
148
            );
149
            return $this
150
                ->error($error)
151
                ->redirect(['view-details', 'id' => $record->id]);
152
        }
153
154
        if (!$record->isJobValid()) {
155
            $error = Module::t(
156
                'notice',
157
                'The job isn\'t pushed because it must be JobInterface instance.'
158
            );
159
            return $this
160
                ->error($error)
161
                ->redirect(['view-data', 'id' => $record->id]);
162
        }
163
164
        $uid = $record->getSender()->push($record->createJob());
165
        $newRecord = PushRecord::find()->byJob($record->sender_name, $uid)->one();
166
167
        return $this
168
            ->success(Module::t('notice', 'The job is pushed again.'))
169
            ->redirect(['view', 'id' => $newRecord->id]);
170
    }
171
172
    /**
173
     * Stop a job
174
     *
175
     * @param int $id
176
     * @throws
177
     * @return mixed
178
     */
179
    public function actionStop($id)
180
    {
181
        if (!$this->module->canExecStop) {
182
            throw new ForbiddenHttpException(Module::t('notice', 'Stop is forbidden.'));
183
        }
184
185
        $record = $this->findRecord($id);
186
187
        if ($record->isStopped()) {
188
            return $this
189
                ->error(Module::t('notice', 'The job is already stopped.'))
190
                ->redirect(['view-details', 'id' => $record->id]);
191
        }
192
193
        if (!$record->canStop()) {
194
            return $this
195
                ->error(Module::t('notice', 'The job is already done.'))
196
                ->redirect(['view-attempts', 'id' => $record->id]);
197
        }
198
199
        $record->stop();
200
201
        return $this
202
            ->success(Module::t('notice', 'The job will be stopped.'))
203
            ->redirect(['view-details', 'id' => $record->id]);
204
    }
205
206
    /**
207
     * @param int $id
208
     * @throws NotFoundHttpException
209
     * @return PushRecord
210
     */
211
    protected function findRecord($id)
212
    {
213
        if ($record = PushRecord::find()->byId($id)->one()) {
214
            return $record;
215
        }
216
        throw new NotFoundHttpException(Module::t('notice', 'Record not found.'));
217
    }
218
}
219