Issues (186)

Branch: oauth-creation-featureflag

includes/DataObjects/JobQueue.php (1 issue)

Labels
Severity
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\DataObjects;
11
12
use Exception;
13
use Waca\Background\Task\BotCreationTask;
14
use Waca\Background\Task\UserCreationTask;
15
use Waca\Background\Task\WelcomeUserTask;
16
use Waca\DataObject;
17
use Waca\Exceptions\OptimisticLockFailedException;
18
19
class JobQueue extends DataObject
20
{
21
    /*
22
     * Status workflow is this:
23
     *
24
     * 1) Queued. The job has been added to the queue.
25
     * 2) Ready. The job is ready to be run in the next queue run.
26
     * 3) Waiting. The job has been picked up by the worker
27
     * 4) Running. The job is actively being processed.
28
     * 5) Complete / Failed. The job has been processed
29
     *
30
     * A job can move to Cancelled at any point, and will be cancelled automatically.
31
     *
32
     * 'held' is not used by the system, and is intended for manual pauses.
33
     *
34
     */
35
36
    const STATUS_QUEUED = 'queued';
37
    const STATUS_READY = 'ready';
38
    const STATUS_WAITING = 'waiting';
39
    const STATUS_RUNNING = 'running';
40
    const STATUS_COMPLETE = 'complete';
41
    const STATUS_FAILED = 'failed';
42
    const STATUS_CANCELLED = 'cancelled';
43
    const STATUS_HELD = 'held';
44
45
    /** @var string */
46
    private $task;
47
    /** @var int */
48
    private $user;
49
    /** @var int */
50
    private $request;
51
    /** @var int */
52
    private $emailtemplate;
53
    /** @var string */
54
    private $status;
55
    /** @var string */
56
    private $enqueue;
57
    /** @var string */
58
    private $parameters;
59
    /** @var string */
60
    private $error;
61
    /** @var int */
62
    private $acknowledged;
63
    /** @var int */
64
    private $parent;
65
    /** @var int */
66
    private $domain;
67
68
    /**
69
     * This feels like the least bad place to put this method.
70
     */
71
    public static function getTaskDescriptions()
72
    {
73
        return array(
74
            BotCreationTask::class  => 'Create account (via bot)',
75
            UserCreationTask::class => 'Create account (via OAuth)',
76
            WelcomeUserTask::class  => 'Welcome user',
77
        );
78
    }
79
80
    /**
81
     * Saves a data object to the database, either updating or inserting a record.
82
     * @return void
83
     * @throws Exception
84
     * @throws OptimisticLockFailedException
85
     */
86
    public function save()
87
    {
88
        if ($this->isNew()) {
89
            // insert
90
            $statement = $this->dbObject->prepare(<<<SQL
91
                INSERT INTO jobqueue (task, user, request, emailtemplate, parameters, parent, status, domain) 
92
                VALUES (:task, :user, :request, :emailtemplate, :parameters, :parent, 'queued', :domain)
93
SQL
94
            );
95
            $statement->bindValue(":task", $this->task);
96
            $statement->bindValue(":user", $this->user);
97
            $statement->bindValue(":request", $this->request);
98
            $statement->bindValue(":emailtemplate", $this->emailtemplate);
99
            $statement->bindValue(":parameters", $this->parameters);
100
            $statement->bindValue(":parent", $this->parent);
101
            $statement->bindValue(":domain", $this->domain);
102
103
            if ($statement->execute()) {
104
                $this->id = (int)$this->dbObject->lastInsertId();
105
            }
106
            else {
107
                throw new Exception($statement->errorInfo());
0 ignored issues
show
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
108
            }
109
        }
110
        else {
111
            // update
112
            $statement = $this->dbObject->prepare(<<<SQL
113
                UPDATE jobqueue SET 
114
                      status = :status
115
                    , error = :error
116
                    , acknowledged = :ack
117
                    , updateversion = updateversion + 1
118
                WHERE id = :id AND updateversion = :updateversion;
119
SQL
120
            );
121
122
            $statement->bindValue(":id", $this->id);
123
            $statement->bindValue(":updateversion", $this->updateversion);
124
125
            $statement->bindValue(":status", $this->status);
126
            $statement->bindValue(":error", $this->error);
127
            $statement->bindValue(":ack", $this->acknowledged);
128
129
            if (!$statement->execute()) {
130
                throw new Exception($statement->errorInfo());
131
            }
132
133
            if ($statement->rowCount() !== 1) {
134
                throw new OptimisticLockFailedException();
135
            }
136
137
            $this->updateversion++;
138
        }
139
    }
140
141
    #region Properties
142
143
    /**
144
     * @return string
145
     */
146
    public function getTask()
147
    {
148
        return $this->task;
149
    }
150
151
    /**
152
     * @param string $task
153
     */
154
    public function setTask($task)
155
    {
156
        $this->task = $task;
157
    }
158
159
    /**
160
     * @return int
161
     */
162
    public function getTriggerUserId()
163
    {
164
        return $this->user;
165
    }
166
167
    /**
168
     * @param int $user
169
     */
170
    public function setTriggerUserId($user)
171
    {
172
        $this->user = $user;
173
    }
174
175
    /**
176
     * @return int
177
     */
178
    public function getRequest()
179
    {
180
        return $this->request;
181
    }
182
183
    /**
184
     * @param int $request
185
     */
186
    public function setRequest($request)
187
    {
188
        $this->request = $request;
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function getStatus()
195
    {
196
        return $this->status;
197
    }
198
199
    /**
200
     * @param string $status
201
     */
202
    public function setStatus($status)
203
    {
204
        $this->status = $status;
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getEnqueue()
211
    {
212
        return $this->enqueue;
213
    }
214
215
    /**
216
     * @param string $enqueue
217
     */
218
    public function setEnqueue($enqueue)
219
    {
220
        $this->enqueue = $enqueue;
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getParameters()
227
    {
228
        return $this->parameters;
229
    }
230
231
    /**
232
     * @param string $parameters
233
     */
234
    public function setParameters($parameters)
235
    {
236
        $this->parameters = $parameters;
237
    }
238
239
    /**
240
     * @return mixed
241
     */
242
    public function getError()
243
    {
244
        return $this->error;
245
    }
246
247
    /**
248
     * @param mixed $error
249
     */
250
    public function setError($error)
251
    {
252
        $this->error = $error;
253
    }
254
255
    /**
256
     * @return int
257
     */
258
    public function getAcknowledged()
259
    {
260
        return $this->acknowledged;
261
    }
262
263
    /**
264
     * @param int $acknowledged
265
     */
266
    public function setAcknowledged($acknowledged)
267
    {
268
        $this->acknowledged = $acknowledged;
269
    }
270
271
    /**
272
     * @return int
273
     */
274
    public function getParent()
275
    {
276
        return $this->parent;
277
    }
278
279
    /**
280
     * @param int $parent
281
     */
282
    public function setParent($parent)
283
    {
284
        $this->parent = $parent;
285
    }
286
287
    /**
288
     * @return int
289
     */
290
    public function getEmailTemplate()
291
    {
292
        return $this->emailtemplate;
293
    }
294
295
    /**
296
     * @param int $emailTemplate
297
     */
298
    public function setEmailTemplate($emailTemplate)
299
    {
300
        $this->emailtemplate = $emailTemplate;
301
    }
302
    #endregion
303
    public function getDomain(): int
304
    {
305
        return $this->domain;
306
    }
307
308
    public function setDomain(int $domain): void
309
    {
310
        $this->domain = $domain;
311
    }
312
}