Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Resque often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Resque, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Resque implements EnqueueInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $kernelOptions; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $redisConfiguration; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $globalRetryStrategy = array(); |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $jobRetryStrategy = array(); |
||
28 | |||
29 | public function __construct(array $kernelOptions) |
||
33 | |||
34 | public function setPrefix($prefix) |
||
38 | |||
39 | public function setRedisConfiguration($host, $port, $database) |
||
50 | |||
51 | public function setGlobalRetryStrategy($strategy) |
||
55 | |||
56 | public function setJobRetryStrategy($strategy) |
||
60 | |||
61 | public function getRedisConfiguration() |
||
65 | |||
66 | public function enqueue(Job $job, $trackStatus = false) |
||
82 | |||
83 | public function enqueueOnce(Job $job, $trackStatus = false) |
||
98 | |||
99 | View Code Duplication | public function enqueueAt($at, Job $job) |
|
111 | |||
112 | View Code Duplication | public function enqueueIn($in, Job $job) |
|
124 | |||
125 | View Code Duplication | public function removedDelayed(Job $job) |
|
135 | |||
136 | View Code Duplication | public function removeFromTimestamp($at, Job $job) |
|
146 | |||
147 | public function getQueues() |
||
153 | |||
154 | /** |
||
155 | * @param $queue |
||
156 | * @return Queue |
||
157 | */ |
||
158 | public function getQueue($queue) |
||
162 | |||
163 | public function getWorkers() |
||
169 | |||
170 | public function getWorker($id) |
||
180 | |||
181 | public function pruneDeadWorkers() |
||
188 | |||
189 | public function getDelayedJobTimestamps() |
||
190 | { |
||
191 | $timestamps = \Resque::redis()->zrange('delayed_queue_schedule', 0, -1); |
||
192 | |||
193 | //TODO: find a more efficient way to do this |
||
194 | $out = array(); |
||
195 | foreach ($timestamps as $timestamp) { |
||
196 | $out[] = array($timestamp, \Resque::redis()->llen('delayed:'.$timestamp)); |
||
197 | } |
||
198 | |||
199 | return $out; |
||
200 | } |
||
201 | |||
202 | public function getFirstDelayedJobTimestamp() |
||
203 | { |
||
204 | $timestamps = $this->getDelayedJobTimestamps(); |
||
205 | if (count($timestamps) > 0) { |
||
206 | return $timestamps[0]; |
||
207 | } |
||
208 | |||
209 | return array(null, 0); |
||
210 | } |
||
211 | |||
212 | public function getNumberOfDelayedJobs() |
||
216 | |||
217 | View Code Duplication | public function getJobsForTimestamp($timestamp) |
|
218 | { |
||
219 | $jobs = \Resque::redis()->lrange('delayed:'.$timestamp, 0, -1); |
||
220 | $out = array(); |
||
221 | foreach ($jobs as $job) { |
||
222 | $out[] = json_decode($job, true); |
||
223 | } |
||
224 | |||
225 | return $out; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param $queue |
||
230 | * @return int |
||
231 | */ |
||
232 | public function clearQueue($queue) |
||
239 | |||
240 | View Code Duplication | public function getFailedJobs($start = -100, $count = 100) |
|
252 | |||
253 | /** |
||
254 | * Attach any applicable retry strategy to the job. |
||
255 | * |
||
256 | * @param Job $job |
||
257 | */ |
||
258 | protected function attachRetryStrategy($job) |
||
271 | } |
||
272 |