rules_scheduler.handler.inc 2.6 KB

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