Conditions | 8 |
Paths | 16 |
Total Lines | 83 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
48 | public function requestJob($capabilities, array $types = []) { |
||
49 | $idlist = []; |
||
50 | $wasFetched = []; |
||
51 | |||
52 | $this->virtualFields['age'] = 'IFNULL(TIMESTAMPDIFF(SECOND, NOW(), not_before), 0)'; |
||
53 | $conditions = [ |
||
54 | 'completed' => null, |
||
55 | 'OR' => [] |
||
56 | ]; |
||
57 | $fields = [ |
||
58 | 'id', |
||
59 | 'fetched', |
||
60 | 'age' |
||
61 | ]; |
||
62 | $order = [ |
||
63 | 'age' => 'ASC', |
||
64 | 'id' => 'ASC' |
||
65 | ]; |
||
66 | $limit = Configure::read('Queue.workers'); |
||
67 | |||
68 | if ($types) { |
||
69 | $conditions = $this->_addFilter($conditions, 'task', $types); |
||
70 | } |
||
71 | |||
72 | // Generate the job specific conditions. |
||
73 | foreach ($capabilities as $task) { |
||
74 | list($plugin, $name) = pluginSplit($task['name']); |
||
75 | $tmp = [ |
||
76 | 'task' => $name, |
||
77 | 'AND' => [ |
||
78 | 'not_before <=' => date('Y-m-d H:i:s'), |
||
79 | [ |
||
80 | 'OR' => [ |
||
81 | 'fetched <' => date('Y-m-d H:i:s', time() - $task['timeout']), |
||
82 | 'fetched' => null |
||
83 | ] |
||
84 | ] |
||
85 | ], |
||
86 | 'failed_count <' => ($task['retries'] + 1) |
||
87 | ]; |
||
88 | $conditions['OR'][] = $tmp; |
||
89 | } |
||
90 | |||
91 | // First, find a list of a few of the oldest unfinished jobs. |
||
92 | $data = $this->find('all', compact('conditions', 'fields', 'order', 'limit')); |
||
93 | |||
94 | if (!empty($data)) { |
||
95 | // Generate a list of their ids |
||
96 | foreach ($data as $item) { |
||
97 | $idlist[] = $item[$this->name]['id']; |
||
98 | if (!empty($item[$this->name]['fetched'])) { |
||
99 | $wasFetched[] = $item[$this->name]['id']; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // Generate a unique identifier for the current worker thread |
||
104 | $key = sha1(microtime()); |
||
105 | |||
106 | // Try to update one of the found jobs with the key of this worker. |
||
107 | $this->query( |
||
108 | 'UPDATE ' . $this->tablePrefix . $this->table . ' SET worker_key = "' . $key . |
||
109 | '", fetched = "' . date('Y-m-d H:i:s') . '" WHERE ' . |
||
110 | 'id IN(' . implode(',', $idlist) . ') AND ' . |
||
111 | '(worker_key IS NULL OR fetched <= "' . date('Y-m-d H:i:s', time() - $task['timeout']) . '") ' . |
||
112 | 'ORDER BY ' . $this->virtualFields['age'] . ' ASC LIMIT 1' |
||
113 | ); |
||
114 | |||
115 | // Read which one actually got updated, which is the job we are supposed to execute. |
||
116 | $conditions = ['worker_key' => $key]; |
||
117 | $data = $this->find('first', compact('conditions')); |
||
118 | if (!empty($data)) { |
||
119 | // If the job had an existing fetched timestamp, increment the failure counter. |
||
120 | if (in_array($data[$this->name]['id'], $wasFetched)) { |
||
121 | $data[$this->name]['failed_count'] += 1; |
||
122 | $data[$this->name]['failure_message'] = 'Restart after timeout'; |
||
123 | $this->save($data); |
||
124 | } |
||
125 | |||
126 | return $data[$this->name]; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return false; |
||
131 | } |
||
288 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths