rules_scheduler.handler.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Views integration for the rules scheduler module.
  5. */
  6. /**
  7. * Default scheduled task handler.
  8. */
  9. class RulesSchedulerDefaultTaskHandler implements RulesSchedulerTaskHandlerInterface {
  10. /**
  11. * The task array.
  12. *
  13. * @var array
  14. */
  15. protected $task;
  16. /**
  17. * Constructs a repetitive task handler object.
  18. */
  19. public function __construct(array $task) {
  20. $this->task = $task;
  21. }
  22. /**
  23. * Implements RulesSchedulerTaskHandlerInterface::runTask().
  24. */
  25. public function runTask() {
  26. if ($component = rules_get_cache('comp_' . $this->task['config'])) {
  27. $replacements = array('%label' => $component->label(), '%plugin' => $component->plugin());
  28. $replacements['%identifier'] = $this->task['identifier'] ? $this->task['identifier'] : t('without identifier');
  29. rules_log('Scheduled evaluation of %plugin %label, task %identifier.', $replacements, RulesLog::INFO, $component, TRUE);
  30. $state = unserialize($this->task['data']);
  31. $state->restoreBlocks();
  32. // Block the config to prevent any future recursion.
  33. $state->block($component);
  34. // Finally evaluate the component with the given state.
  35. $component->evaluate($state);
  36. $state->unblock($component);
  37. rules_log('Finished evaluation of %plugin %label, task %identifier.', $replacements, RulesLog::INFO, $component, FALSE);
  38. $state->cleanUp();
  39. }
  40. }
  41. /**
  42. * Implements RulesSchedulerTaskHandlerInterface::afterTaskQueued().
  43. */
  44. public function afterTaskQueued() {
  45. // Delete the task from the task list.
  46. db_delete('rules_scheduler')
  47. ->condition('tid', $this->task['tid'])
  48. ->execute();
  49. }
  50. /**
  51. * Implements RulesSchedulerTaskHandlerInterface::getTask().
  52. */
  53. public function getTask() {
  54. return $this->task;
  55. }
  56. }
  57. /**
  58. * Interface for scheduled task handlers.
  59. *
  60. * Task handlers control the behavior of a task when it's queued or executed.
  61. * Unless specified otherwise, the RulesSchedulerDefaultTaskHandler task handler
  62. * is used.
  63. *
  64. * @see rules_scheduler_run_task()
  65. * @see rules_scheduler_cron()
  66. * @see RulesSchedulerDefaultTaskHandler
  67. */
  68. interface RulesSchedulerTaskHandlerInterface {
  69. /**
  70. * Processes a queue item.
  71. *
  72. * @throws RulesEvaluationException
  73. * If there are any problems executing the task.
  74. *
  75. * @see rules_scheduler_run_task()
  76. */
  77. public function runTask();
  78. /**
  79. * Processes a task after it has been queued.
  80. *
  81. * @see rules_scheduler_cron()
  82. */
  83. public function afterTaskQueued();
  84. /**
  85. * Returns the task associated with the task handler.
  86. *
  87. * @return array
  88. * The task (queue item) array.
  89. */
  90. public function getTask();
  91. }