Last active
August 26, 2016 13:56
-
-
Save maciejzgadzaj/8206063880125695f06bf53630ac4c4b to your computer and use it in GitHub Desktop.
Additional drush commands and aliases for Drupal advancedqueue module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git i/drush/advancedqueue.drush.inc w/drush/advancedqueue.drush.inc | |
| index 8e24a28..8de61db 100644 | |
| --- i/drush/advancedqueue.drush.inc | |
| +++ w/drush/advancedqueue.drush.inc | |
| @@ -10,6 +10,7 @@ | |
| */ | |
| function advancedqueue_drush_command() { | |
| $items = array(); | |
| + | |
| $items['advancedqueue-process-queue'] = array( | |
| 'description' => 'Run a processing job for a queue.', | |
| 'arguments' => array( | |
| @@ -19,14 +20,48 @@ function advancedqueue_drush_command() { | |
| 'timeout' => 'The maximum execution time of the script. Be warned that this is a rough estimate as the time is only checked between two items.', | |
| 'all' => 'Process all queues.', | |
| ), | |
| - 'aliases' => array('advancedqueue'), | |
| + 'aliases' => array('advancedqueue', 'aqp'), | |
| + ); | |
| + | |
| + $items['advancedqueue-delete'] = array( | |
| + 'description' => 'Deletes a queue and all items in it.', | |
| + 'arguments' => array( | |
| + 'queue' => dt('The name of the queue to delete.'), | |
| + ), | |
| + 'options' => array( | |
| + 'all' => 'Delete all queues.', | |
| + ), | |
| + 'aliases' => array('aqd'), | |
| + ); | |
| + | |
| + $items['advancedqueue-delete-item'] = array( | |
| + 'description' => 'Deletes an item from a queue.', | |
| + 'arguments' => array( | |
| + 'queue' => dt('The name of the queue to delete an item from.'), | |
| + 'item_id' => dt('The ID of the item to delete.'), | |
| + ), | |
| + 'aliases' => array('aqdi'), | |
| ); | |
| + | |
| $items['advancedqueue-list'] = array( | |
| 'description' => 'Returns a list of all defined queues', | |
| 'options' => array( | |
| 'pipe' => 'Return a comma delimited list of queues.' | |
| - ) | |
| + ), | |
| + 'aliases' => array('aql'), | |
| ); | |
| + | |
| + $items['advancedqueue-list-items'] = array( | |
| + 'description' => 'Returns a list of unprocessed items in a queue.', | |
| + 'arguments' => array( | |
| + 'queue' => dt('The name of the queue to return unprocessed items for.'), | |
| + ), | |
| + 'options' => array( | |
| + 'all' => 'Return unprocessed items for all queues.', | |
| + ), | |
| + 'aliases' => array('aqli'), | |
| + ); | |
| + | |
| return $items; | |
| } | |
| @@ -83,6 +118,73 @@ function drush_advancedqueue_process_queue($queue = NULL) { | |
| } | |
| /** | |
| + * Command callback for drush advancedqueue-delete. | |
| + * | |
| + * @param string $queue | |
| + * The name of the queue to delete. | |
| + */ | |
| +function drush_advancedqueue_delete($queue = NULL) { | |
| + // Load information about the registred queues, and sort them by weight. | |
| + if (!$queues_info = advancedqueue_get_queues_info()) { | |
| + return drush_set_error(dt('No queues exist.')); | |
| + } | |
| + | |
| + $all_option = drush_get_option('all'); | |
| + if (!$all_option && empty($queue)) { | |
| + return drush_set_error(dt('You have to specify either a set of queues or the --all parameter.')); | |
| + } | |
| + | |
| + if ($all_option) { | |
| + $queues = $queues_info; | |
| + } | |
| + else { | |
| + // Validate queues. | |
| + $queues = drupal_map_assoc(explode(',', $queue)); | |
| + if ($invalid_queues = array_diff_key($queues, $queues_info)) { | |
| + return drush_set_error(dt('The following queues are invalid: !queues. Aborting.', array('!queues' => implode(', ', $invalid_queues)))); | |
| + } | |
| + $queues = array_intersect_key($queues_info, $queues); | |
| + } | |
| + | |
| + foreach ($queues as $queue_name => $queue_info) { | |
| + $queue = DrupalQueue::get($queue_name); | |
| + $queue->deleteQueue(); | |
| + } | |
| +} | |
| + | |
| +/** | |
| + * Command callback for drush advancedqueue-delete-item. | |
| + * | |
| + * @param $queue_name | |
| + * The name of the queue to delete an item from. | |
| + * @param int $item_id | |
| + * The ID of the queue item to delete. | |
| + */ | |
| +function drush_advancedqueue_delete_item($queue_name, $item_id) { | |
| + if (!in_array($queue_name, array_keys(advancedqueue_get_queues_info()))) { | |
| + return drush_set_error(dt('The queue @queue_name does not exist.', array('@queue_name' => $queue_name))); | |
| + } | |
| + $queue = DrupalQueue::get($queue_name); | |
| + | |
| + $data = array( | |
| + ':name' => $queue_name, | |
| + ':item_id' => $item_id, | |
| + ); | |
| + if (!$item = db_query_range('SELECT item_id, uid, title, data FROM {advancedqueue} q WHERE name = :name AND item_id = :item_id', 0, 1, $data)->fetchObject()) { | |
| + return drush_set_error(dt('The item @item_id does not exist.', array('@item_id' => $item_id))); | |
| + } | |
| + | |
| + if ($queue && $item) { | |
| + $queue->deleteItem($item); | |
| + | |
| + drush_log(dt('Item @item_id deleted from queue @queue_name.', array( | |
| + '@item_id' => $item_id, | |
| + '@queue_name' => $queue_name, | |
| + )), 'ok'); | |
| + } | |
| +} | |
| + | |
| +/** | |
| * Command callback for drush advancedqueue-list. | |
| */ | |
| function drush_advancedqueue_list() { | |
| @@ -128,9 +230,50 @@ function drush_advancedqueue_get_queues() { | |
| 'cron' => array( | |
| 'callback' => $queue['worker callback'], | |
| 'time' => $queue['time'], | |
| - ) | |
| + ), | |
| ); | |
| } | |
| } | |
| return $queues; | |
| } | |
| + | |
| +/** | |
| + * Command callback for drush advancedqueue-list-items. | |
| + */ | |
| +function drush_advancedqueue_list_items($queue = NULL) { | |
| + // Load information about the registred queues, and sort them by weight. | |
| + if (!$queues_info = advancedqueue_get_queues_info()) { | |
| + return drush_set_error(dt('No queues exist.')); | |
| + } | |
| + | |
| + $all_option = drush_get_option('all'); | |
| + if (!$all_option && empty($queue)) { | |
| + return drush_set_error(dt('You have to specify either a set of queues or the --all parameter.')); | |
| + } | |
| + | |
| + if ($all_option) { | |
| + $queues = $queues_info; | |
| + } | |
| + else { | |
| + // Validate queues. | |
| + $queues = drupal_map_assoc(explode(',', $queue)); | |
| + if ($invalid_queues = array_diff_key($queues, $queues_info)) { | |
| + return drush_set_error(dt('The following queues are invalid: !queues. Aborting.', array('!queues' => implode(', ', $invalid_queues)))); | |
| + } | |
| + $queues = array_intersect_key($queues_info, $queues); | |
| + } | |
| + | |
| + $rows = array(array('Queue', 'Item ID', 'Title')); | |
| + | |
| + foreach ($queues as $queue_name => $queue_info) { | |
| + $result = db_query('SELECT name, item_id, title FROM {advancedqueue} q WHERE status <= 0 AND name = :name ORDER BY created ASC', array(':name' => $queue_name))->fetchAll(); | |
| + foreach ($result as $row) { | |
| + $rows[] = $row; | |
| + } | |
| + } | |
| + | |
| + drush_print_table($rows, TRUE); | |
| + | |
| + // Return the result for backend invoke. | |
| + return $rows; | |
| +} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment