@@ -42,9 +42,10 @@ public function createJob($taskName, $data, $notBefore = null) {
4242 * Looks for a new job that can be processed with the current abilities
4343 *
4444 * @param array $capabilities Available queue worker tasks.
45+ * @param array $types Request a job from these types (or exclude certain types), or any otherwise.
4546 * @return mixed Job data or false.
4647 */
47- public function requestJob ($ capabilities ) {
48+ public function requestJob ($ capabilities, array $ types = [] ) {
4849 $ idlist = [];
4950 $ wasFetched = [];
5051
@@ -64,6 +65,10 @@ public function requestJob($capabilities) {
6465 ];
6566 $ limit = Configure::read ('Queue.workers ' );
6667
68+ if ($ types ) {
69+ $ conditions = $ this ->_addFilter ($ conditions , 'task ' , $ types );
70+ }
71+
6772 // Generate the job specific conditions.
6873 foreach ($ capabilities as $ task ) {
6974 list ($ plugin , $ name ) = pluginSplit ($ task ['name ' ]);
@@ -179,7 +184,7 @@ public function getLength($taskName = null) {
179184 * @return array A list of task names
180185 */
181186 public function getTypes () {
182- $ fields = ['task ' ];
187+ $ fields = ['task ' , ' task ' ];
183188 $ group = ['task ' ];
184189
185190 return $ this ->find ('list ' , compact ('fields ' , 'group ' ));
@@ -246,4 +251,33 @@ public function cleanFailedJobs($capabilities) {
246251 return $ this ->deleteAll ($ conditions , false );
247252 }
248253
254+ /**
255+ * Filters field `key` based on the provided values. Values prefixed with '-' are excluded.
256+ *
257+ * @param array $conditions Conditions
258+ * @param string $key Key
259+ * @param array $values Values
260+ * @return array the conditions
261+ */
262+ protected function _addFilter (array $ conditions , $ key , array $ values ) : array {
263+ $ include = [];
264+ $ exclude = [];
265+ foreach ($ values as $ value ) {
266+ if (substr ($ value , 0 , 1 ) === '- ' ) {
267+ $ exclude [] = substr ($ value , 1 );
268+ } else {
269+ $ include [] = $ value ;
270+ }
271+ }
272+
273+ if ($ include ) {
274+ $ conditions [$ key . ' IN ' ] = $ include ;
275+ }
276+ if ($ exclude ) {
277+ $ conditions [$ key . ' NOT IN ' ] = $ exclude ;
278+ }
279+
280+ return $ conditions ;
281+ }
282+
249283}
0 commit comments