RunJobQueueTask::execute()   D
last analyzed

Complexity

Conditions 9
Paths 2052

Size

Total Lines 101
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 101
ccs 0
cts 52
cp 0
rs 4.1147
c 1
b 0
f 0
cc 9
nc 2052
nop 0
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ConsoleTasks;
11
12
use Exception;
13
use PDO;
14
use Waca\Background\BackgroundTaskBase;
15
use Waca\Background\Task\BotCreationTask;
16
use Waca\Background\Task\UserCreationTask;
17
use Waca\Background\Task\WelcomeUserTask;
18
use Waca\DataObjects\JobQueue;
19
use Waca\ExceptionHandler;
20
use Waca\Exceptions\ApplicationLogicException;
21
use Waca\Helpers\Logger;
22
use Waca\PdoDatabase;
23
use Waca\Tasks\ConsoleTaskBase;
24
25
class RunJobQueueTask extends ConsoleTaskBase
26
{
27
    private $taskList = array(
28
        WelcomeUserTask::class,
29
        BotCreationTask::class,
30
        UserCreationTask::class
31
    );
32
33
    public function execute()
34
    {
35
        $database = $this->getDatabase();
36
37
        // ensure we're running inside a tx here.
38
        if (!$database->hasActiveTransaction()) {
39
            $database->beginTransaction();
40
        }
41
42
        $sql = 'SELECT * FROM jobqueue WHERE status = :status ORDER BY enqueue LIMIT :lim';
43
        $statement = $database->prepare($sql);
44
        $statement->execute(array(
45
            ':status' => JobQueue::STATUS_READY,
46
            ':lim' => $this->getSiteConfiguration()->getJobQueueBatchSize()
47
        ));
48
49
        /** @var JobQueue[] $queuedJobs */
50
        $queuedJobs = $statement->fetchAll(PDO::FETCH_CLASS, JobQueue::class);
51
52
        // mark all the jobs as running, and commit the txn so we're not holding onto long-running transactions.
53
        // We'll re-lock the row when we get to it.
54
        foreach ($queuedJobs as $job) {
55
            $job->setDatabase($database);
56
            $job->setStatus(JobQueue::STATUS_WAITING);
57
            $job->setError(null);
58
            $job->setAcknowledged(null);
59
            $job->save();
60
        }
61
62
        $database->commit();
63
64
        set_error_handler(array(RunJobQueueTask::class, 'errorHandler'), E_ALL);
65
66
        foreach ($queuedJobs as $job) {
67
            try {
68
                // refresh from the database
69
                /** @var JobQueue $job */
70
                $job = JobQueue::getById($job->getId(), $database);
71
72
                if ($job->getStatus() !== JobQueue::STATUS_WAITING) {
73
                    continue;
74
                }
75
76
                $database->beginTransaction();
77
                $job->setStatus(JobQueue::STATUS_RUNNING);
78
                $job->save();
79
                $database->commit();
80
81
                $database->beginTransaction();
82
83
                // re-lock the job
84
                $job->setStatus(JobQueue::STATUS_RUNNING);
85
                $job->save();
86
87
                // validate we're allowed to run the requested task (whitelist)
88
                if (!in_array($job->getTask(), $this->taskList)) {
89
                    throw new ApplicationLogicException('Job task not registered');
90
                }
91
92
                // Create a task.
93
                $taskName = $job->getTask();
94
95
                if (!class_exists($taskName)) {
96
                    throw new ApplicationLogicException('Job task does not exist');
97
                }
98
99
                /** @var BackgroundTaskBase $task */
100
                $task = new $taskName;
101
102
                $this->setupTask($task, $job);
103
                $task->run();
104
            }
105
            catch (Exception $ex) {
106
                $database->rollBack();
107
                try {
108
                    $database->beginTransaction();
109
110
                    /** @var JobQueue $job */
111
                    $job = JobQueue::getById($job->getId(), $database);
112
                    $job->setDatabase($database);
113
                    $job->setStatus(JobQueue::STATUS_FAILED);
114
                    $job->setError($ex->getMessage());
115
                    $job->setAcknowledged(0);
116
                    $job->save();
117
118
                    Logger::backgroundJobIssue($this->getDatabase(), $job);
119
120
                    $database->commit();
121
                }
122
                catch (Exception $ex) {
123
                    // oops, something went horribly wrong trying to handle this in a nice way; let's just fall back to
124
                    // logging this to disk for a tool root to investigate.
125
                    ExceptionHandler::logExceptionToDisk($ex, $this->getSiteConfiguration());
126
                }
127
            }
128
            finally {
129
                $database->commit();
130
            }
131
        }
132
133
        $this->stageQueuedTasks($database);
134
    }
135
136
    /**
137
     * @param BackgroundTaskBase $task
138
     * @param JobQueue           $job
139
     */
140
    private function setupTask(BackgroundTaskBase $task, JobQueue $job)
141
    {
142
        $task->setJob($job);
143
        $task->setDatabase($this->getDatabase());
144
        $task->setHttpHelper($this->getHttpHelper());
145
        $task->setOauthProtocolHelper($this->getOAuthProtocolHelper());
146
        $task->setEmailHelper($this->getEmailHelper());
147
        $task->setSiteConfiguration($this->getSiteConfiguration());
148
        $task->setNotificationHelper($this->getNotificationHelper());
149
    }
150
151
    /** @noinspection PhpUnusedParameterInspection */
152
    public static function errorHandler($errno, $errstr, $errfile, $errline)
153
    {
154
        throw new Exception($errfile . "@" . $errline . ": " . $errstr);
155
    }
156
157
    /**
158
     * Stages tasks for execution during the *next* jobqueue run.
159
     *
160
     * This is to build in some delay between enqueue and execution to allow for accidentally-triggered tasks to be
161
     * cancelled.
162
     *
163
     * @param PdoDatabase $database
164
     */
165
    protected function stageQueuedTasks(PdoDatabase $database): void
166
    {
167
        try {
168
            $database->beginTransaction();
169
170
            $sql = 'SELECT * FROM jobqueue WHERE status = :status ORDER BY enqueue LIMIT :lim';
171
            $statement = $database->prepare($sql);
172
173
            // use a larger batch size than the main runner, but still keep it limited in case things go crazy.
174
            $statement->execute(array(
175
                ':status' => JobQueue::STATUS_QUEUED,
176
                ':lim' => $this->getSiteConfiguration()->getJobQueueBatchSize() * 2
177
            ));
178
179
            /** @var JobQueue[] $queuedJobs */
180
            $queuedJobs = $statement->fetchAll(PDO::FETCH_CLASS, JobQueue::class);
181
182
            foreach ($queuedJobs as $job) {
183
                $job->setDatabase($database);
184
                $job->setStatus(JobQueue::STATUS_READY);
185
                $job->save();
186
            }
187
188
            $database->commit();
189
        }
190
        catch (Exception $ex) {
191
            $database->rollBack();
192
            ExceptionHandler::logExceptionToDisk($ex, $this->getSiteConfiguration());
193
        }
194
    }
195
}
196