Projet

Général

Profil

Paste
Télécharger (6,31 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules scheduler module.
6
 */
7

    
8
define('RULES_SCHEDULER_PATH', 'admin/config/workflow/rules/schedule');
9

    
10
/**
11
 * Implements hook_cron().
12
 */
13
function rules_scheduler_cron() {
14
  if (rules_scheduler_queue_tasks()) {
15
    // hook_exit() is not invoked for cron runs, so register it as shutdown
16
    // callback for logging the rules log to the watchdog.
17
    drupal_register_shutdown_function('rules_exit');
18
    // Clear the log before running tasks via the queue to avoid logging
19
    // unrelated logs from previous cron-operations.
20
    RulesLog::logger()->clear();
21
  }
22
}
23

    
24
/**
25
 * Implements hook_cron_queue_info().
26
 */
27
function rules_scheduler_cron_queue_info() {
28
  $queues['rules_scheduler_tasks'] = array(
29
    'worker callback' => 'rules_scheduler_run_task',
30
    'time' => 15,
31
  );
32
  return $queues;
33
}
34

    
35
/**
36
 * Queue worker callback for running a single task.
37
 *
38
 * @param array $task
39
 *   The task to process.
40
 */
41
function rules_scheduler_run_task(array $task) {
42
  try {
43
    // BC support for tasks that have been already queued, before update
44
    // rules_scheduler_update_7204() ran.
45
    if (isset($task['state'])) {
46
      $task['data'] = $task['state'];
47
    }
48
    rules_scheduler_task_handler($task)->runTask();
49
  }
50
  catch (RulesEvaluationException $e) {
51
    rules_log($e->msg, $e->args, $e->severity);
52
    rules_log('Unable to execute task with identifier %id scheduled on date %date.', array('%id' => $task['identifier'], '%date' => format_date($task['date'])), RulesLog::ERROR);
53
  }
54
}
55

    
56
/**
57
 * Returns the task handler for a given task.
58
 *
59
 * @param array $task
60
 *   A task (queue item) array.
61
 *
62
 * @throws RulesEvaluationException
63
 *   If the task handler class is missing.
64
 *
65
 * @return RulesSchedulerTaskHandlerInterface
66
 *   The task handler.
67
 */
68
function rules_scheduler_task_handler(array $task) {
69
  $class = !empty($task['handler']) ? $task['handler'] : 'RulesSchedulerDefaultTaskHandler';
70
  if (!class_exists($class)) {
71
    throw new RulesEvaluationException('Missing task handler implementation %class.', array('%class' => $class), NULL, RulesLog::ERROR);
72
  }
73
  return new $class($task);
74
}
75

    
76
/**
77
 * Implements hook_rules_ui_menu_alter().
78
 *
79
 * Adds a menu item for the 'schedule' operation.
80
 */
81
function rules_scheduler_rules_ui_menu_alter(&$items, $base_path, $base_count) {
82
  $items[$base_path . '/manage/%rules_config/schedule'] = array(
83
    'title callback' => 'rules_get_title',
84
    'title arguments' => array('Schedule !plugin "!label"', $base_count + 1),
85
    'page callback' => 'drupal_get_form',
86
    'page arguments' => array('rules_scheduler_schedule_form', $base_count + 1, $base_path),
87
    'access callback' => 'rules_config_access',
88
    'access arguments' => array('update', $base_count + 1),
89
    'file' => 'rules_scheduler.admin.inc',
90
    'file path' => drupal_get_path('module', 'rules_scheduler'),
91
  );
92
}
93

    
94
/**
95
 * Implements hook_menu().
96
 */
97
function rules_scheduler_menu() {
98
  $items = array();
99
  $items[RULES_SCHEDULER_PATH] = array(
100
    'title' => 'Schedule',
101
    'type' => MENU_LOCAL_TASK,
102
    'page callback' => 'rules_scheduler_schedule_page',
103
    'access arguments' => array('administer rules'),
104
    'file' => 'rules_scheduler.admin.inc',
105
  );
106
  $items[RULES_SCHEDULER_PATH . '/%rules_scheduler_task/delete'] = array(
107
    'title' => 'Delete a scheduled task',
108
    'type' => MENU_CALLBACK,
109
    'page callback' => 'drupal_get_form',
110
    'page arguments' => array('rules_scheduler_delete_task', 5),
111
    'access arguments' => array('administer rules'),
112
    'file' => 'rules_scheduler.admin.inc',
113
  );
114
  return $items;
115
}
116

    
117
/**
118
 * Loads a task by a given task ID.
119
 *
120
 * @param int $tid
121
 *   The task ID.
122
 */
123
function rules_scheduler_task_load($tid) {
124
  $result = db_select('rules_scheduler', 'r')
125
    ->fields('r')
126
    ->condition('tid', (int) $tid)
127
    ->execute();
128
  return $result->fetchAssoc();
129
}
130

    
131
/**
132
 * Deletes a task by a given task ID.
133
 *
134
 * @param int $tid
135
 *   The task ID.
136
 */
137
function rules_scheduler_task_delete($tid) {
138
  db_delete('rules_scheduler')
139
    ->condition('tid', $tid)
140
    ->execute();
141
}
142

    
143
/**
144
 * Schedule a task to be executed later on.
145
 *
146
 * @param array $task
147
 *   An array representing the task with the following keys:
148
 *   - config: The machine readable name of the to-be-scheduled component.
149
 *   - date: Timestamp when the component should be executed.
150
 *   - state: (deprecated) Rules evaluation state to use for scheduling.
151
 *   - data: Any additional data to store with the task.
152
 *   - handler: The name of the task handler class.
153
 *   - identifier: User provided string to identify the task per scheduled
154
 *   configuration.
155
 */
156
function rules_scheduler_schedule_task($task) {
157
  // Map the deprecated 'state' property into 'data'.
158
  if (isset($task['state'])) {
159
    $task['data'] = $task['state'];
160
    unset($task['state']);
161
  }
162
  if (!empty($task['identifier'])) {
163
    // If there is a task with the same identifier and component, we replace it.
164
    db_delete('rules_scheduler')
165
      ->condition('config', $task['config'])
166
      ->condition('identifier', $task['identifier'])
167
      ->execute();
168
  }
169
  drupal_write_record('rules_scheduler', $task);
170
}
171

    
172
/**
173
 * Queue tasks that are ready for execution.
174
 *
175
 * @return bool
176
 *   TRUE if any queue items where created, otherwise FALSE.
177
 */
178
function rules_scheduler_queue_tasks() {
179
  $items_created = FALSE;
180
  // Limit adding tasks to 1000 per cron run.
181
  $result = db_select('rules_scheduler', 'r', array('fetch' => PDO::FETCH_ASSOC))
182
    ->fields('r')
183
    ->condition('date', time(), '<=')
184
    ->orderBy('date')
185
    ->range(0, 1000)
186
    ->execute();
187

    
188
  $queue = DrupalQueue::get('rules_scheduler_tasks');
189
  foreach ($result as $task) {
190
    // Add the task to the queue and remove the entry afterwards.
191
    if ($queue->createItem($task)) {
192
      $items_created = TRUE;
193
      rules_scheduler_task_handler($task)->afterTaskQueued();
194
    }
195
  }
196
  return $items_created;
197
}
198

    
199
/**
200
 * Implements hook_rules_config_delete().
201
 */
202
function rules_scheduler_rules_config_delete($rules_config) {
203
  // Only react on real delete, not revert.
204
  if (!$rules_config->hasStatus(ENTITY_IN_CODE)) {
205
    // Delete all tasks scheduled for this config.
206
    db_delete('rules_scheduler')
207
      ->condition('config', $rules_config->name)
208
      ->execute();
209
  }
210
}
211

    
212
/**
213
 * Implements hook_views_api().
214
 */
215
function rules_scheduler_views_api() {
216
  return array(
217
    'api' => '3.0-alpha1',
218
    'path' => drupal_get_path('module', 'rules_scheduler') . '/includes',
219
  );
220
}