Projet

Général

Profil

Paste
Télécharger (2,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_scheduler / rules_scheduler.drush.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules Scheduler Drush integration.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 */
11
function rules_scheduler_drush_command() {
12
  $items = array();
13

    
14
  $items['rules-scheduler-tasks'] = array(
15
    'description' => 'Check for scheduled tasks to be added to the queue.',
16
    'options' => array(
17
      'claim' => 'Optionally claim tasks from the queue to work on. Any value set will override the default time spent on this queue.',
18
    ),
19
    'drupal dependencies' => array('rules', 'rules_scheduler'),
20
    'aliases' => array('rusch'),
21
    'examples' => array(
22
      'drush rusch' => 'Add scheduled tasks to the queue.',
23
      'drush rusch --claim' => 'Add scheduled tasks to the queue and claim items for the default amount of time.',
24
      'drush rusch --claim=30' => 'Add scheduled tasks to the queue and claim items for 30 seconds.',
25
    ),
26
  );
27

    
28
  return $items;
29
}
30

    
31
/**
32
 * Implements hook_drush_help().
33
 */
34
function rules_scheduler_drush_help($section) {
35
  switch ($section) {
36
    case 'drush:rules-scheduler-tasks':
37
      return dt('Checks for scheduled tasks to be added the queue. Can optionally claim tasks from the queue to work on.');
38
  }
39
}
40

    
41
/**
42
 * Command callback for processing the rules_scheduler_tasks queue.
43
 *
44
 * @see rules_scheduler_cron_queue_info()
45
 * @see rules_scheduler_cron()
46
 */
47
function drush_rules_scheduler_tasks() {
48
  if (rules_scheduler_queue_tasks()) {
49
    // hook_exit() is not invoked for drush runs, so register it as shutdown
50
    // callback for logging the rules log to the watchdog.
51
    drupal_register_shutdown_function('rules_exit');
52
    // Clear the log before running tasks via the queue to avoid logging
53
    // unrelated logs from previous operations.
54
    RulesLog::logger()->clear();
55
    drush_log(dt('Added scheduled tasks to the queue.'), 'success');
56
  }
57

    
58
  $claim = drush_get_option('claim', FALSE);
59
  if ($claim) {
60
    // Fetch the queue information and let other modules alter it.
61
    $queue_name = 'rules_scheduler_tasks';
62
    $info = module_invoke('rules_scheduler', 'cron_queue_info');
63
    drupal_alter('cron_queue_info', $info);
64

    
65
    $function = $info[$queue_name]['worker callback'];
66
    // The drush option can override the default process time.
67
    $time = is_numeric($claim) ? (int) $claim : $info[$queue_name]['time'];
68
    $end = time() + $time;
69
    // Claim items and process the queue.
70
    $queue = DrupalQueue::get($queue_name);
71
    $claimed = 0;
72
    while (time() < $end && ($item = $queue->claimItem())) {
73
      $function($item->data);
74
      $queue->deleteItem($item);
75
      $claimed++;
76
    }
77
    if ($claimed) {
78
      drush_log(dt('Claimed and worked on !claimed scheduled tasks for up to !time seconds.', array('!claimed' => $claimed, '!time' => $time)), 'success');
79
    }
80
  }
81
}