commit cb398764c09982191a7139bbc681a4ad9a3be679 Author: darthsteven Date: Thu Dec 1 20:50:54 2016 +0100 Issue #1895580 by Steven Jones: Task priorities diff --git a/hosting.api.php b/hosting.api.php index 97008f4..31fc154 100644 --- a/hosting.api.php +++ b/hosting.api.php @@ -357,7 +357,7 @@ function hosting_QUEUE_TYPE_queue($count = 5) { global $provision_errors; drush_log(dt("Running tasks queue")); - $tasks = _hosting_get_new_tasks($count); + $tasks = hosting_get_new_tasks($count); foreach ($tasks as $task) { drush_invoke_process('@self', "hosting-task", array($task->nid), array(), array('fork' => TRUE)); } diff --git a/queued/hosting_queued.drush.inc b/queued/hosting_queued.drush.inc index 983c45f..faeb12a 100644 --- a/queued/hosting_queued.drush.inc +++ b/queued/hosting_queued.drush.inc @@ -73,7 +73,7 @@ function drush_hosting_queued() { // Get some tasks to run - if ($tasks = @_hosting_get_new_tasks()) { + if ($tasks = @hosting_get_new_tasks()) { if (lock_acquire('hosting_queue_tasks_running', HOSTING_QUEUE_LOCK_TIMEOUT)) { drush_log('Acquired lock on task queue.'); foreach ($tasks as $task) { diff --git a/task/hosting_task.api.php b/task/hosting_task.api.php index fa34820..4233d13 100644 --- a/task/hosting_task.api.php +++ b/task/hosting_task.api.php @@ -103,6 +103,24 @@ function hosting_task_TASK_TYPE_form_validate($form, &$form_state) { } +/** + * Alter the query that selects the next tasks to run. + * + * You may use this hook to prioritise one type of task over another, or to + * prefer one client over another etc. + * + * @see hosting_get_new_tasks + * + * @param QueryAlterableInterface $query + * The structured query that will select the next tasks to run. + */ +function hook_query_hosting_get_new_tasks_alter(QueryAlterableInterface $query) { + // Change the sort ordering so that newer tasks are preferred to older ones. + $order_by = &$query->getOrderBy(); + $order_by['n.changed'] = 'DESC'; + $order_by['n.nid'] = 'DESC'; +} + /** * @} End of "addtogroup hooks". diff --git a/task/hosting_task.module b/task/hosting_task.module index 598ae49..34e03d5 100644 --- a/task/hosting_task.module +++ b/task/hosting_task.module @@ -724,7 +724,7 @@ function hosting_tasks_queue($count = 20) { global $provision_errors; drush_log(dt("Running tasks queue")); - $tasks = _hosting_get_new_tasks($count); + $tasks = hosting_get_new_tasks($count); foreach ($tasks as $task) { drush_invoke_process('@self', "hosting-task", array($task->nid), array('strict' => FALSE), array('fork' => TRUE)); } @@ -1323,35 +1323,46 @@ function hosting_get_tasks($filter_by = NULL, $filter_value = NULL, $count = 5, } /** - * Retrieve a list of outstanding tasks. + * Retrieve a list of outstanding, queued, tasks. * * @param int $limit - * The amount of items to return. + * The maximum number of tasks to return. * * @return array * An associative array containing task nodes, indexed by node id. */ -function _hosting_get_new_tasks($limit = 20) { +function hosting_get_new_tasks($limit = 20) { $return = array(); - $result = db_query_range("SELECT t.nid - FROM {hosting_task} t - INNER JOIN {node} n - ON t.vid = n.vid - WHERE t.task_status = :task_status - GROUP BY t.rid - ORDER BY n.changed, n.nid ASC", - 0, - $limit, - array( - ':task_status' => HOSTING_TASK_QUEUED, - )); - foreach ($result as $node) { + $query = db_select('hosting_task', 't'); + $query->innerJoin('node', 'n', 't.vid = n.vid'); + $query + ->fields('t', array('nid')) + ->condition('t.task_status', HOSTING_TASK_QUEUED) + ->orderBy('n.changed') + ->orderBy('n.nid') + ->groupBy('t.rid') + ->range(0, $limit) + ->addTag('hosting_get_new_tasks'); + + foreach ($query->execute() as $node) { $return[$node->nid] = node_load($node->nid); } return $return; } /** + * Retrieve a list of outstanding, queued, tasks. + * + * @deprecated First deprecated in Hosting 3.9 because this function was made + * part of the public API, use hosting_get_new_tasks() instead. + * + * @see hosting_get_new_tasks + */ +function _hosting_get_new_tasks($limit = 20) { + return hosting_get_new_tasks($limit); +} + +/** * @name Error status definitions * @{ * Bitmask values used to generate the error code to return.