class SchedulingState in Webform Scheduled Tasks 8.2
Get information about the state of task scheduling.
Information related to the scheduling of a task such as the next time a task is due to be run or if the task has been stopped due to error are stored in state instead of configuration. This is so tasks can be deployed alongside webforms, as per a typical configuration management workflow and a those config values wil never be updated automatically by the scheduling system.
This ensures that the scheduling system operates according to the rules of if a task is halted or not and the upcoming scheduling intervals, regardless of deploys and config imports.
Hierarchy
- class \Drupal\webform_scheduled_tasks\SchedulingState implements SchedulingStateInterface
Expanded class hierarchy of SchedulingState
1 string reference to 'SchedulingState'
1 service uses SchedulingState
File
- src/
SchedulingState.php, line 21
Namespace
Drupal\webform_scheduled_tasksView source
class SchedulingState implements SchedulingStateInterface {
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The key used in state for storing scheduling information.
*/
const SCHEDULE_STATE_KEY = 'webform_scheduled_tasks.scheduling_info';
/**
* A state key for halted tasks.
*/
const HALTED_STATE_KEY = 'webform_scheduled_tasks.halted_tasks';
/**
* SchedulingState constructor.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public function hasTimeScheduled(WebformScheduledTaskInterface $scheduledTask) {
$scheduled_times = $this->state
->get(static::SCHEDULE_STATE_KEY);
return isset($scheduled_times[$scheduledTask
->id()]);
}
/**
* {@inheritdoc}
*/
public function getNextScheduledTime(WebformScheduledTaskInterface $scheduledTask) {
$scheduled_times = $this->state
->get(static::SCHEDULE_STATE_KEY);
return isset($scheduled_times[$scheduledTask
->id()]) ? $scheduled_times[$scheduledTask
->id()] : NULL;
}
/**
* {@inheritdoc}
*/
public function setNextScheduledTime(WebformScheduledTaskInterface $scheduledTask, $timestamp) {
$scheduled_times = $this->state
->get(static::SCHEDULE_STATE_KEY);
$scheduled_times[$scheduledTask
->id()] = $timestamp;
$this->state
->set(static::SCHEDULE_STATE_KEY, $scheduled_times);
}
/**
* {@inheritdoc}
*/
public function isHalted(WebformScheduledTaskInterface $scheduledTask) {
$halted_tasks = $this->state
->get(static::HALTED_STATE_KEY);
return isset($halted_tasks[$scheduledTask
->id()]);
}
/**
* {@inheritdoc}
*/
public function getHaltedMessage(WebformScheduledTaskInterface $scheduledTask) {
if (!$this
->isHalted($scheduledTask)) {
throw new \Exception('Tried to get the halted task message from a task that is not halted.');
}
$halted_tasks = $this->state
->get(static::HALTED_STATE_KEY);
return $halted_tasks[$scheduledTask
->id()];
}
/**
* {@inheritdoc}
*/
public function haltTask(WebformScheduledTaskInterface $scheduledTask, $message = '') {
$halted_tasks = $this->state
->get(static::HALTED_STATE_KEY);
$halted_tasks[$scheduledTask
->id()] = $message;
$this->state
->set(static::HALTED_STATE_KEY, $halted_tasks);
}
/**
* {@inheritdoc}
*/
public function resumeTask(WebformScheduledTaskInterface $scheduledTask) {
$halted_tasks = $this->state
->get(static::HALTED_STATE_KEY);
unset($halted_tasks[$scheduledTask
->id()]);
$this->state
->set(static::HALTED_STATE_KEY, $halted_tasks);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SchedulingState:: |
protected | property | The state service. | |
SchedulingState:: |
public | function |
Get the message associated with a halted task, if one exists. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
public | function |
Get the next time a task is scheduled to run. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
constant | A state key for halted tasks. | ||
SchedulingState:: |
public | function |
Halt a scheduled task. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
public | function |
Check if a time has been scheduled for a task to be run. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
public | function |
Check if a task has been halted. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
public | function |
Resume a task. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
constant | The key used in state for storing scheduling information. | ||
SchedulingState:: |
public | function |
Set the next scheduled time a task should run. Overrides SchedulingStateInterface:: |
|
SchedulingState:: |
public | function | SchedulingState constructor. |