PDO::FETCH_ASSOC)) ->fields('r') ->condition('date', time(), '<=') ->range(0, 30) ->execute(); foreach ($result as $task) { if ($component = rules_get_cache('comp_' . $task['config'])) { $replacements = array('%label' => $component->label(), '%plugin' => $component->plugin()); rules_log('Scheduled evaluation of %plugin %label.', $replacements, RulesLog::INFO, TRUE); $state = unserialize($task['state']); $state->restoreBlocks(); // Finally evaluate the component with the given state. $component->evaluate($state); rules_log('Finished evaluation of %plugin %label.', $replacements, RulesLog::INFO, FALSE); $state->cleanUp(); } db_delete('rules_scheduler') ->condition('tid', $task['tid']) ->execute(); } // Log the rules log to the system log. if ($log = RulesLog::logger()->render()) { watchdog('rules scheduler', 'Scheduled component evaluation: !log', array('!log' => $log), WATCHDOG_NOTICE); } } /** * Implements hook_menu(). */ function rules_scheduler_menu() { $items = array(); $items[RULES_SCHEDULER_PATH] = array( 'title' => 'Schedule', 'type' => MENU_LOCAL_TASK, 'page callback' => 'rules_scheduler_schedule_page', 'access arguments' => array('administer rules'), 'file' => 'rules_scheduler.admin.inc', ); $items[RULES_SCHEDULER_PATH .'/%rules_scheduler_task/delete'] = array( 'title' => 'Delete a scheduled task', 'type' => MENU_CALLBACK, 'page callback' => 'drupal_get_form', 'page arguments' => array('rules_scheduler_delete_task', 5), 'access arguments' => array('administer rules'), 'file' => 'rules_scheduler.admin.inc', ); return $items; } /** * Load a task by a given task ID. */ function rules_scheduler_task_load($tid) { $result = db_select('rules_scheduler', 'r') ->fields('r') ->condition('tid', (int) $tid) ->execute(); return $result->fetchAssoc(); } /** * Delete a task by a given task ID. */ function rules_scheduler_task_delete($tid) { db_delete('rules_scheduler') ->condition('tid', $tid) ->execute(); } /** * Schedule a task to be executed later on. * * @param $task * An array representing the task with the following keys: * - config: The machine readable name of the to be scheduled component. * - date: Timestamp when the component should be executed. * - state: An rules evaluation state to use for scheduling. * - identifier: User provided string to identify the task per scheduled * configuration. */ function rules_scheduler_schedule_task($task) { if (!empty($task['identifier'])) { // If there is a task with the same identifier and component, we replace it. db_delete('rules_scheduler') ->condition('config', $task['config']) ->condition('identifier', $task['identifier']) ->execute(); } drupal_write_record('rules_scheduler', $task); } /** * Implements hook_rules_config_delete(). */ function rules_scheduler_rules_config_delete($rules_config) { // Delete all tasks scheduled for this config. db_delete('rules_scheduler') ->condition('config', $rules_config->name) ->execute(); } /** * Implements hook_views_api(). */ function rules_scheduler_views_api() { return array( 'api' => '3.0-alpha1', 'path' => drupal_get_path('module', 'rules_scheduler') .'/includes', ); }