You are here

public function JobScheduler::set in Job Scheduler 8.3

Same name and namespace in other branches
  1. 8.2 src/JobScheduler.php \Drupal\job_scheduler\JobScheduler::set()

Adds a job to the schedule, replace any existing job.

A job is uniquely identified by $job = array(type, id).

function worker_callback($job) {

  // Work off job.
  // Set next time to be called. If this portion of the code is not
  // reached for some reason, the scheduler will keep periodically invoking
  // the callback() with the period value initially specified.
  $scheduler
    ->set($job);
}

Parameters

array $job: An array that must contain the following keys: 'type' - A string identifier of the type of job. 'id' - A numeric identifier of the job. 'period' - The time when the task should be executed. 'periodic' - True if the task should be repeated periodically.

Throws

\Exception Exceptions thrown by code called by this method are passed on.

Overrides JobSchedulerInterface::set

1 call to JobScheduler::set()
JobScheduler::rebuild in src/JobScheduler.php
Rebuilds a single scheduler.

File

src/JobScheduler.php, line 55

Class

JobScheduler
Manage scheduled jobs.

Namespace

Drupal\job_scheduler

Code

public function set(array $job) {
  $storage = $this->jobScheduleStorage;
  $timestamp = time();
  $job['last'] = $timestamp;
  if (!empty($job['crontab'])) {
    $crontab = $this->crontabDecorator
      ->decorate($job['crontab']);
    $job['next'] = $crontab
      ->nextTime($timestamp);
  }
  else {
    $job['next'] = $timestamp + $job['period'];
  }
  $entity = $storage
    ->create($job);
  $entity
    ->save();
}