rules_scheduler.module 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Rules scheduler module.
  5. */
  6. define('RULES_SCHEDULER_PATH', 'admin/config/workflow/rules/schedule');
  7. /**
  8. * Implements hook_cron().
  9. */
  10. function rules_scheduler_cron() {
  11. if (rules_scheduler_queue_tasks()) {
  12. // hook_exit() is not invoked for cron runs, so register it as shutdown
  13. // callback for logging the rules log to the watchdog.
  14. drupal_register_shutdown_function('rules_exit');
  15. // Clear the log before running tasks via the queue to avoid logging
  16. // unrelated logs from previous cron-operations.
  17. RulesLog::logger()->clear();
  18. }
  19. }
  20. /**
  21. * Implements hook_cron_queue_info().
  22. */
  23. function rules_scheduler_cron_queue_info() {
  24. $queues['rules_scheduler_tasks'] = array(
  25. 'worker callback' => 'rules_scheduler_run_task',
  26. 'time' => 15,
  27. );
  28. return $queues;
  29. }
  30. /**
  31. * Queue worker callback for running a single task.
  32. *
  33. * @param array $task
  34. * The task to process.
  35. */
  36. function rules_scheduler_run_task(array $task) {
  37. try {
  38. // BC support for tasks that have been already queued, before update
  39. // rules_scheduler_update_7204() ran.
  40. if (isset($task['state'])) {
  41. $task['data'] = $task['state'];
  42. }
  43. rules_scheduler_task_handler($task)->runTask();
  44. }
  45. catch (RulesEvaluationException $e) {
  46. rules_log($e->msg, $e->args, $e->severity);
  47. rules_log('Unable to execute task with identifier %id scheduled on date %date.', array('%id' => $task['identifier'], '%date' => format_date($task['date'])), RulesLog::ERROR);
  48. }
  49. }
  50. /**
  51. * Returns the task handler for a given task.
  52. *
  53. * @param array $task
  54. * A task (queue item) array.
  55. *
  56. * @throws RulesEvaluationException
  57. * If the task handler class is missing.
  58. *
  59. * @return RulesSchedulerTaskHandlerInterface
  60. * The task handler.
  61. */
  62. function rules_scheduler_task_handler(array $task) {
  63. $class = !empty($task['handler']) ? $task['handler'] : 'RulesSchedulerDefaultTaskHandler';
  64. if (!class_exists($class)) {
  65. throw new RulesEvaluationException('Missing task handler implementation %class.', array('%class' => $class), NULL, RulesLog::ERROR);
  66. }
  67. return new $class($task);
  68. }
  69. /**
  70. * Implements hook_rules_ui_menu_alter().
  71. *
  72. * Adds a menu item for the 'schedule' operation.
  73. */
  74. function rules_scheduler_rules_ui_menu_alter(&$items, $base_path, $base_count) {
  75. $items[$base_path . '/manage/%rules_config/schedule'] = array(
  76. 'title callback' => 'rules_get_title',
  77. 'title arguments' => array('Schedule !plugin "!label"', $base_count + 1),
  78. 'page callback' => 'drupal_get_form',
  79. 'page arguments' => array('rules_scheduler_schedule_form', $base_count + 1, $base_path),
  80. 'access callback' => 'rules_config_access',
  81. 'access arguments' => array('update', $base_count + 1),
  82. 'file' => 'rules_scheduler.admin.inc',
  83. 'file path' => drupal_get_path('module', 'rules_scheduler'),
  84. );
  85. }
  86. /**
  87. * Implements hook_menu().
  88. */
  89. function rules_scheduler_menu() {
  90. $items = array();
  91. $items[RULES_SCHEDULER_PATH] = array(
  92. 'title' => 'Schedule',
  93. 'type' => MENU_LOCAL_TASK,
  94. 'page callback' => 'rules_scheduler_schedule_page',
  95. 'access arguments' => array('administer rules'),
  96. 'file' => 'rules_scheduler.admin.inc',
  97. );
  98. $items[RULES_SCHEDULER_PATH .'/%rules_scheduler_task/delete'] = array(
  99. 'title' => 'Delete a scheduled task',
  100. 'type' => MENU_CALLBACK,
  101. 'page callback' => 'drupal_get_form',
  102. 'page arguments' => array('rules_scheduler_delete_task', 5),
  103. 'access arguments' => array('administer rules'),
  104. 'file' => 'rules_scheduler.admin.inc',
  105. );
  106. return $items;
  107. }
  108. /**
  109. * Load a task by a given task ID.
  110. */
  111. function rules_scheduler_task_load($tid) {
  112. $result = db_select('rules_scheduler', 'r')
  113. ->fields('r')
  114. ->condition('tid', (int) $tid)
  115. ->execute();
  116. return $result->fetchAssoc();
  117. }
  118. /**
  119. * Delete a task by a given task ID.
  120. */
  121. function rules_scheduler_task_delete($tid) {
  122. db_delete('rules_scheduler')
  123. ->condition('tid', $tid)
  124. ->execute();
  125. }
  126. /**
  127. * Schedule a task to be executed later on.
  128. *
  129. * @param $task
  130. * An array representing the task with the following keys:
  131. * - config: The machine readable name of the to be scheduled component.
  132. * - date: Timestamp when the component should be executed.
  133. * - state: (deprecated) Rules evaluation state to use for scheduling.
  134. * - data: Any additional data to store with the task.
  135. * - handler: The name of the task handler class.
  136. * - identifier: User provided string to identify the task per scheduled
  137. * configuration.
  138. */
  139. function rules_scheduler_schedule_task($task) {
  140. // Map the deprecated 'state' property into 'data'.
  141. if (isset($task['state'])) {
  142. $task['data'] = $task['state'];
  143. unset($task['state']);
  144. }
  145. if (!empty($task['identifier'])) {
  146. // If there is a task with the same identifier and component, we replace it.
  147. db_delete('rules_scheduler')
  148. ->condition('config', $task['config'])
  149. ->condition('identifier', $task['identifier'])
  150. ->execute();
  151. }
  152. drupal_write_record('rules_scheduler', $task);
  153. }
  154. /**
  155. * Queue tasks that are ready for execution.
  156. *
  157. * @return boolean
  158. * Returns TRUE if any queue items where created, otherwise FALSE.
  159. */
  160. function rules_scheduler_queue_tasks() {
  161. $items_created = FALSE;
  162. // Limit adding tasks to 1000 per cron run.
  163. $result = db_select('rules_scheduler', 'r', array('fetch' => PDO::FETCH_ASSOC))
  164. ->fields('r')
  165. ->condition('date', time(), '<=')
  166. ->orderBy('date')
  167. ->range(0, 1000)
  168. ->execute();
  169. $queue = DrupalQueue::get('rules_scheduler_tasks');
  170. foreach ($result as $task) {
  171. // Add the task to the queue and remove the entry afterwards.
  172. if ($queue->createItem($task)) {
  173. $items_created = TRUE;
  174. rules_scheduler_task_handler($task)->afterTaskQueued();
  175. }
  176. }
  177. return $items_created;
  178. }
  179. /**
  180. * Implements hook_rules_config_delete().
  181. */
  182. function rules_scheduler_rules_config_delete($rules_config) {
  183. // Only react on real delete, not revert.
  184. if (!$rules_config->hasStatus(ENTITY_IN_CODE)) {
  185. // Delete all tasks scheduled for this config.
  186. db_delete('rules_scheduler')
  187. ->condition('config', $rules_config->name)
  188. ->execute();
  189. }
  190. }
  191. /**
  192. * Implements hook_views_api().
  193. */
  194. function rules_scheduler_views_api() {
  195. return array(
  196. 'api' => '3.0-alpha1',
  197. 'path' => drupal_get_path('module', 'rules_scheduler') .'/includes',
  198. );
  199. }