Conditions | 9 |
Paths | 2052 |
Total Lines | 101 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
196 |