public static function Schedule::executeSchedule in Business Rules 8
Same name and namespace in other branches
- 2.x src/Entity/Schedule.php \Drupal\business_rules\Entity\Schedule::executeSchedule()
Execute the scheduled tasks.
Parameters
\Drupal\business_rules\Events\BusinessRulesEvent $event: The cron event.
Overrides ScheduleInterface::executeSchedule
1 call to Schedule::executeSchedule()
- business_rules_cron in ./
business_rules.module - Implements hook_cron().
File
- src/
Entity/ Schedule.php, line 395
Class
- Schedule
- Defines the Schedule entity.
Namespace
Drupal\business_rules\EntityCode
public static function executeSchedule(BusinessRulesEvent $event) {
// Check if the schedule execution is enabled on Business Rules settings.
$config = \Drupal::configFactory()
->get('business_rules.settings');
$enabled = $config
->get('enable_scheduler');
if ($enabled) {
// Load non-executed tasks from the action.
$ids = \Drupal::entityTypeManager()
->getStorage('business_rules_schedule')
->getQuery()
->condition('status', 0)
->condition('scheduled', time(), '<=')
->execute();
$tasks = self::loadMultiple($ids);
$container = \Drupal::getContainer();
$util = new BusinessRulesUtil($container);
if (count($tasks)) {
/** @var \Drupal\business_rules\Entity\Schedule $task */
foreach ($tasks as $task) {
/** @var \Drupal\business_rules\Entity\Action $action */
$action = $task
->getTriggeredBy();
$items = $action
->getSettings('items');
$task_event = $task
->getEvent();
try {
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $task_event
->getSubject() instanceof Entity ? $task_event
->getSubject() : FALSE;
if ($entity) {
$entity = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
$task_event
->setArgument('entity', $entity);
$task_event = new BusinessRulesEvent($entity, $task_event
->getArguments());
}
foreach ($items as $item) {
$action_item = Action::load($item['id']);
$action_item
->execute($task_event);
}
if ($entity && $task
->getUpdateEntity()) {
$entity_exists = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
if ($entity_exists instanceof EntityInterface) {
$entity
->save();
}
}
$task
->setExecuted(1);
$task
->save();
$util->logger
->notice(t('Scheduled task id: @id, name: "@name", triggered by: "@by" has been executed at: @time', [
'@id' => $task
->id(),
'@name' => $task
->getName(),
'@by' => $task
->getTriggeredBy()
->id(),
'@time' => $container
->get('date.formatter')
->format(time(), 'medium'),
]));
} catch (\Exception $e) {
$util->logger
->error($e
->getMessage());
}
}
}
}
}