Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
README.md 3.98 KiB

CRON SERVICE

INTRODUCTION

However Drupal 8 allows a developer to implement cron related code by using workers, they still have limited functionality and the most common issue here - they don't provide any control when they should or should not run and don't allow a developer to run tasks from other parts of the project. The hook_cron() also doesn't provide anything like that and also forces developers to write code in functions the .module file which makes unit testing a bit harder. This module allows developers to get rid of plenty cron hooks from their code and makes control of jobs executing schedule a bit easier.

DESCRIPTION

The module provides a service which executes on every hook_cron() and calls to execute() method for every other service with cron_service tag.

#REQUIREMENTS Drupal core. This module uses standard hook_cron() and symfony service container for work.

#INSTALLATION composer require drupal/cron_service or other standard way of installing a drupal module

#CONFIGURATION No configuration required. Simply create your own service and it will work.

USAGE

The simpliest way to use it is to add cron_service tag to your service:

services:
  your_service_name:
    class: \Drupal\your_module\YourClass
    tags: [ { name: cron_service } ]

And be sure that your_class implements at least \Drupal\cron_service\CronServiceInterface

For example:

use Drupal\cron_service\ScheduledCronServiceInterface;
use Drupal\cron_service\TimeControllingCronServiceInterface;

class YourClass implements ScheduledCronServiceInterface, TimeControllingCronServiceInterface {

  /**
   * {@inheritdoc}
   */
  public function execute() {
    \Drupal::logger('cron')->debug('Doing stuff');
  }

  /**
   * {@inheritdoc}
   */
  public function getNextExecutionTime(): int {
    // Next run will be tommorrow.
    return time() + 86400;
  }

  /**
   * {@inheritdoc}
   */
  public function shouldRunNow(): bool {
    // Let the coin decide, should it run or not.
    return rand(0, 1) > 0;
  }

}

API

Interfaces

  • \Drupal\cron_service\CronServiceInterface - declares execute() method which must be the entry point of the desired logic. This method will be invoked on every cron run.
  • \Drupal\cron_service\ScheduledCronServiceInterface additionally to CronServiceInterface declares getNextExecutionTime():int method which should return the timestamp when execute() method should be executed next time.
  • \Drupal\cron_service\TimeControllingCronServiceInterface additionally to CronServiceInterface declares shouldRunNow():bool method which is called before every call to execute() and can prevent the execution by returning FALSE. It can contain additional checks of the current time or the current environment.

Interfaces can be freely combined and the manager will invoke the execute() method only when all of the checks would pass.

Service Manager

Public methods: