Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
Moved RNG hook out of .module file to RNG cron class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpi committed Dec 30, 2016
1 parent 560b688 commit 9c9cdc2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 35 deletions.
41 changes: 6 additions & 35 deletions rng.module
Expand Up @@ -5,7 +5,6 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\rng\Entity\RuleSchedule;
use Drupal\rng\EventTypeInterface;
use Drupal\Core\Render\Element;

Expand All @@ -24,9 +23,9 @@ function rng_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_entity_access().
*/
function rng_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\rng\RngEntityAccess $rng_model */
$rng_model = \Drupal::service('rng.entity.access');
return $rng_model->hook_entity_access($entity, $operation, $account);
/** @var \Drupal\rng\RngEntityAccess $rng_access */
$rng_access = \Drupal::service('rng.entity.access');
return $rng_access->hook_entity_access($entity, $operation, $account);
}

/**
Expand All @@ -44,37 +43,9 @@ function rng_form_alter(&$form, FormStateInterface $form_state, $form_id) {
* Implements hook_cron().
*/
function rng_cron() {
// Add scheduled rules to be executed.
$queue = \Drupal::queue('rng_rule_scheduler');
$rule_scheduler_storage = \Drupal::entityTypeManager()
->getStorage('rng_rule_scheduler');

$ids = $rule_scheduler_storage->getQuery()
->condition('trigger_date', time(), '<=')
->condition('in_queue', 0, '=')
->condition('attempts', RuleSchedule::ATTEMPTS_MAX, '<=')
->execute();

foreach ($rule_scheduler_storage->loadMultiple($ids) as $rule_schedule) {
/** @var \Drupal\rng\RuleScheduleInterface $rule_schedule */
$data = ['rule_scheduler_id' => $rule_schedule->id()];
if ($queue->createItem($data)) {
$rule_schedule->setInQueue(TRUE)->save();

// De-activate the rule once it is queued.
if ($component = $rule_schedule->getComponent()) {
if ($rule = $component->getRule()) {
$rule->setIsActive(FALSE)->save();
}
}
}
}

// Delete scheduled rules which have had too many attempts.
$ids = $rule_scheduler_storage->getQuery()
->condition('attempts', RuleSchedule::ATTEMPTS_MAX, '>')
->execute();
$rule_scheduler_storage->delete($rule_scheduler_storage->loadMultiple($ids));
/** @var \Drupal\rng\RngCron $rng_cron */
$rng_cron = \Drupal::service('rng.cron');
$rng_cron->hook_cron();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions rng.services.yml
Expand Up @@ -6,6 +6,9 @@ services:
class: Drupal\rng\EventManager
arguments: ['@entity.manager']
parent: container.trait
rng.cron:
class: Drupal\rng\RngCron
arguments: ['@queue', '@entity_type.manager']
rng.entity.access:
class: Drupal\rng\RngEntityAccess
arguments: ['@rng.event_manager']
Expand Down
95 changes: 95 additions & 0 deletions src/RngCron.php
@@ -0,0 +1,95 @@
<?php

namespace Drupal\rng;

use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\rng\Entity\RuleSchedule;

/**
* RNG Cron.
*/
class RngCron {

/**
* Queue for rule scheduler.
*
* @var \Drupal\Core\Queue\QueueInterface
*/
protected $ruleSchedulerQueue;

/**
* Storage for rule scheduler entities.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $ruleSchedulerStorage;

/**
* Constructs a new RngEntityModel object.
*
* @param \Drupal\Core\Queue\QueueFactory $queue_factory
* The queue service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(QueueFactory $queue_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->ruleSchedulerQueue = $queue_factory->get('rng_rule_scheduler', FALSE);
$this->ruleSchedulerStorage = $entity_type_manager->getStorage('rng_rule_scheduler');
}

/**
* React to Drupal `hook_cron` hooks.
*
* @see hook_cron();
*/
public function hook_cron() {
$this->scheduleRules();
$this->deleteScheduleRules();
}

/**
* Add scheduled rules to be executed to the queue.
*/
protected function scheduleRules() {
$ids = $this->ruleSchedulerStorage
->getQuery()
->condition('trigger_date', time(), '<=')
->condition('in_queue', 0, '=')
->condition('attempts', RuleSchedule::ATTEMPTS_MAX, '<=')
->execute();

/** @var \Drupal\rng\RuleScheduleInterface[] $rule_schedules */
$rule_schedules = $this->ruleSchedulerStorage->loadMultiple($ids);

foreach ($rule_schedules as $rule_schedule) {
$data = ['rule_scheduler_id' => $rule_schedule->id()];
if ($this->ruleSchedulerQueue->createItem($data)) {
$rule_schedule->setInQueue(TRUE)->save();

// De-activate the rule once it is queued.
if ($component = $rule_schedule->getComponent()) {
if ($rule = $component->getRule()) {
$rule->setIsActive(FALSE)->save();
}
}
}
}
}

/**
* Delete scheduled rules which have had too many attempts.
*/
protected function deleteScheduleRules() {
$ids = $this->ruleSchedulerStorage
->getQuery()
->condition('attempts', RuleSchedule::ATTEMPTS_MAX, '>')
->execute();

/** @var \Drupal\rng\RuleScheduleInterface[] $rule_schedules */
$rule_schedules = $this->ruleSchedulerStorage->loadMultiple($ids);

$this->ruleSchedulerStorage->delete($rule_schedules);
}

}

0 comments on commit 9c9cdc2

Please sign in to comment.