This repository has been archived by the owner on Feb 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved RNG hook out of .module file to RNG cron class.
- Loading branch information
Showing
3 changed files
with
104 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |