stwalkerster /
waca
| 1 | <?php |
||
| 2 | /****************************************************************************** |
||
| 3 | * Wikipedia Account Creation Assistance tool * |
||
| 4 | * * |
||
| 5 | * All code in this file is released into the public domain by the ACC * |
||
| 6 | * Development Team. Please see team.json for a list of contributors. * |
||
| 7 | ******************************************************************************/ |
||
| 8 | |||
| 9 | namespace Waca\DataObjects; |
||
| 10 | |||
| 11 | use Exception; |
||
| 12 | use Waca\Background\Task\BotCreationTask; |
||
| 13 | use Waca\Background\Task\UserCreationTask; |
||
| 14 | use Waca\Background\Task\WelcomeUserTask; |
||
| 15 | use Waca\DataObject; |
||
| 16 | use Waca\Exceptions\OptimisticLockFailedException; |
||
| 17 | |||
| 18 | class JobQueue extends DataObject |
||
| 19 | { |
||
| 20 | /* |
||
| 21 | * Status workflow is this: |
||
| 22 | * |
||
| 23 | * 1) Queued. The job has been added to the queue. |
||
| 24 | * 2) Ready. The job is ready to be run in the next queue run. |
||
| 25 | * 3) Waiting. The job has been picked up by the worker |
||
| 26 | * 4) Running. The job is actively being processed. |
||
| 27 | * 5) Complete / Failed. The job has been processed |
||
| 28 | * |
||
| 29 | * A job can move to Cancelled at any point, and will be cancelled automatically. |
||
| 30 | * |
||
| 31 | * 'held' is not used by the system, and is intended for manual pauses. |
||
| 32 | * |
||
| 33 | */ |
||
| 34 | |||
| 35 | const STATUS_QUEUED = 'queued'; |
||
| 36 | const STATUS_READY = 'ready'; |
||
| 37 | const STATUS_WAITING = 'waiting'; |
||
| 38 | const STATUS_RUNNING = 'running'; |
||
| 39 | const STATUS_COMPLETE = 'complete'; |
||
| 40 | const STATUS_FAILED = 'failed'; |
||
| 41 | const STATUS_CANCELLED = 'cancelled'; |
||
| 42 | const STATUS_HELD = 'held'; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $task; |
||
| 46 | /** @var int */ |
||
| 47 | private $user; |
||
| 48 | /** @var int */ |
||
| 49 | private $request; |
||
| 50 | /** @var int */ |
||
| 51 | private $emailtemplate; |
||
| 52 | /** @var string */ |
||
| 53 | private $status; |
||
| 54 | /** @var string */ |
||
| 55 | private $enqueue; |
||
| 56 | /** @var string */ |
||
| 57 | private $parameters; |
||
| 58 | /** @var string */ |
||
| 59 | private $error; |
||
| 60 | /** @var int */ |
||
| 61 | private $acknowledged; |
||
| 62 | /** @var int */ |
||
| 63 | private $parent; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * This feels like the least bad place to put this method. |
||
| 67 | */ |
||
| 68 | public static function getTaskDescriptions() |
||
| 69 | { |
||
| 70 | return array( |
||
| 71 | BotCreationTask::class => 'Create account (via bot)', |
||
| 72 | UserCreationTask::class => 'Create account (via OAuth)', |
||
| 73 | WelcomeUserTask::class => 'Welcome user', |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Saves a data object to the database, either updating or inserting a record. |
||
| 79 | * @return void |
||
| 80 | * @throws Exception |
||
| 81 | * @throws OptimisticLockFailedException |
||
| 82 | */ |
||
| 83 | public function save() |
||
| 84 | { |
||
| 85 | if ($this->isNew()) { |
||
| 86 | // insert |
||
| 87 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 88 | INSERT INTO jobqueue (task, user, request, emailtemplate, parameters, parent, status) |
||
| 89 | VALUES (:task, :user, :request, :emailtemplate, :parameters, :parent, 'queued') |
||
| 90 | SQL |
||
| 91 | ); |
||
| 92 | $statement->bindValue(":task", $this->task); |
||
| 93 | $statement->bindValue(":user", $this->user); |
||
| 94 | $statement->bindValue(":request", $this->request); |
||
| 95 | $statement->bindValue(":emailtemplate", $this->emailtemplate); |
||
| 96 | $statement->bindValue(":parameters", $this->parameters); |
||
| 97 | $statement->bindValue(":parent", $this->parent); |
||
| 98 | |||
| 99 | if ($statement->execute()) { |
||
| 100 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 101 | } |
||
| 102 | else { |
||
| 103 | throw new Exception($statement->errorInfo()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 104 | } |
||
| 105 | } |
||
| 106 | else { |
||
| 107 | // update |
||
| 108 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 109 | UPDATE jobqueue SET |
||
| 110 | status = :status |
||
| 111 | , error = :error |
||
| 112 | , acknowledged = :ack |
||
| 113 | , updateversion = updateversion + 1 |
||
| 114 | WHERE id = :id AND updateversion = :updateversion; |
||
| 115 | SQL |
||
| 116 | ); |
||
| 117 | |||
| 118 | $statement->bindValue(":id", $this->id); |
||
| 119 | $statement->bindValue(":updateversion", $this->updateversion); |
||
| 120 | |||
| 121 | $statement->bindValue(":status", $this->status); |
||
| 122 | $statement->bindValue(":error", $this->error); |
||
| 123 | $statement->bindValue(":ack", $this->acknowledged); |
||
| 124 | |||
| 125 | if (!$statement->execute()) { |
||
| 126 | throw new Exception($statement->errorInfo()); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($statement->rowCount() !== 1) { |
||
| 130 | throw new OptimisticLockFailedException(); |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->updateversion++; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | #region Properties |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getTask() |
||
| 143 | { |
||
| 144 | return $this->task; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $task |
||
| 149 | */ |
||
| 150 | public function setTask($task) |
||
| 151 | { |
||
| 152 | $this->task = $task; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | public function getTriggerUserId() |
||
| 159 | { |
||
| 160 | return $this->user; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param int $user |
||
| 165 | */ |
||
| 166 | public function setTriggerUserId($user) |
||
| 167 | { |
||
| 168 | $this->user = $user; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | public function getRequest() |
||
| 175 | { |
||
| 176 | return $this->request; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param int $request |
||
| 181 | */ |
||
| 182 | public function setRequest($request) |
||
| 183 | { |
||
| 184 | $this->request = $request; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getStatus() |
||
| 191 | { |
||
| 192 | return $this->status; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $status |
||
| 197 | */ |
||
| 198 | public function setStatus($status) |
||
| 199 | { |
||
| 200 | $this->status = $status; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getEnqueue() |
||
| 207 | { |
||
| 208 | return $this->enqueue; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $enqueue |
||
| 213 | */ |
||
| 214 | public function setEnqueue($enqueue) |
||
| 215 | { |
||
| 216 | $this->enqueue = $enqueue; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getParameters() |
||
| 223 | { |
||
| 224 | return $this->parameters; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $parameters |
||
| 229 | */ |
||
| 230 | public function setParameters($parameters) |
||
| 231 | { |
||
| 232 | $this->parameters = $parameters; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | public function getError() |
||
| 239 | { |
||
| 240 | return $this->error; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param mixed $error |
||
| 245 | */ |
||
| 246 | public function setError($error) |
||
| 247 | { |
||
| 248 | $this->error = $error; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function getAcknowledged() |
||
| 255 | { |
||
| 256 | return $this->acknowledged; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param int $acknowledged |
||
| 261 | */ |
||
| 262 | public function setAcknowledged($acknowledged) |
||
| 263 | { |
||
| 264 | $this->acknowledged = $acknowledged; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return int |
||
| 269 | */ |
||
| 270 | public function getParent() |
||
| 271 | { |
||
| 272 | return $this->parent; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param int $parent |
||
| 277 | */ |
||
| 278 | public function setParent($parent) |
||
| 279 | { |
||
| 280 | $this->parent = $parent; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return int |
||
| 285 | */ |
||
| 286 | public function getEmailTemplate() |
||
| 287 | { |
||
| 288 | return $this->emailtemplate; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param int $emailTemplate |
||
| 293 | */ |
||
| 294 | public function setEmailTemplate($emailTemplate) |
||
| 295 | { |
||
| 296 | $this->emailtemplate = $emailTemplate; |
||
| 297 | } |
||
| 298 | #endregion |
||
| 299 | } |