ProcessCronJobs by Neue Rituale

Manages the execution of CronJobs

ProcessCronJobs

What it does


The module provides paths under which cronjobs can be registered. It lists all registered cronjobs and can execute individual ones manually. The last execution, the last status and, in the event of an error, the last error message are displayed.

Features


  • Clear overview of all registered CronJobs
  • Easy to set timing (onInit or onReady) and delay (LazyCron)
  • Individual path (endpoint) that executes the CronJobs. Configuration of a secret path segment and additional path segments (namespaces) for selected CronJobs.
  • Display of the time of the last execution and any error messages

Install


  1. Copy the files for this module to /site/modules/ProcessCronJobs/
  2. In admin: Modules > Refresh. Install ProcessCronJobs.
  3. Go to Setup > CronJobs
  4. Copy and Install example Module (modules/ProcessCronJobs/example/ProcessCronJobsRegistration.module.example) to register your CronJobs in the __constructor() method. You can also use any other __constructor() method. It makes sense to register the CronJobs as early as possible so that they can also be executed on onInit.
  5. Set up the real cron that calls the ProcessCronJobs provided endpoint.

Install via composer


  1. Execute the following command in your website root directory.
    composer require nr/processcronjobs

Register a CronJob


Simple

A simple example for registering a CronJob.

wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){
	/** @var ProcessCronJobs $processCronJobs */
	$processCronJobs = $event->object;
	$processCronJobs->add(
		'SuperSimpleCronJobOnDemand',
		function(CronJob $cron){ echo "Hello Cron"; },
	);
});

Delayed

This CronJob should run once a day at init.

wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){
	/** @var ProcessCronJobs $processCronJobs */
	$processCronJobs = $event->object;
	$processCronJobs->add(
		'MyFirstCronJobEveryDay',
		function(CronJob $cron){ echo "What a beautiful day"; },
		[
			'lazyCron' => 'LazyCron::everyDay',
			'timing' => CronJob::timingInit,
		]
	);
});

Long Running

This CronJob runs for a very long time and is called directly by a "real" CronJob so as not to block other CronJobs. The endpoint for this CronJob is https://example.com/cron/longrunning/ or https://example.com/cron/###your_secret###/longrunning/.

CronJobs that have a namespace (own path segment) cannot be delayed with LazyCron, because LazyCron can only be started by a single request. LazyCron creates a lock file and thus blocks the execution of parallel calls.

wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){
	/** @var ProcessCronJobs $processCronJobs */
	$processCronJobs = $event->object;
	$processCronJobs->add(
		'SuperLongRunningSpecialCronJob',
		function(CronJob $cron){ echo "I have so much work to do"; },
		[
			'timing' => CronJob::timingInit,
			'ns' => 'longrunning'
		]
	);
});

Process View


ProcessView

Configuration


Modules > Configure > ProcessCronJobs

You will find the following configuration options in the module settings:

  • The trigger path, i.e. the path that triggers the CronJob processing, can be adjusted here (default: cron/).
  • A secret path segment can be created that must be appended to the trigger path so that processing is started.
  • Automatic processing can generally be stopped (status).
  • The cache of ProcessCronJobs can be emptied. Things like the time of the last call, the status of the last call and possibly an error message are stored in the cache.

Configuration

The CronJob object


OptionTypeDefaultDescription
nameStringUnique name in PascalCase e.g. MyFirstCronJob.
callbackCallablefunction(CronJob $cron){}Function to be executed
lazyCronnull, StringnullIf empty, the CronJob is executed without delay as soon as the path is called.
nsnull, StringnullIf empty, the CronJob is called via the default path.
timingIntegerCronJob::timingReadyThe CronJon can be called either at onInit (1) or onReady (2). OnInit is earlier and therefore faster, but not all functions of ProcessWire are available here, e.g. page and language.
timingStrStringonReadyThis is just a getter property.
disabledBooleanfalseThis can be used to deactivate the cronjob, e.g. disabled = $config->debug.
triggerIntegerCronJob::triggerNeverDisplays the last trigger for execution. Possible values are:
1 (Never): CronJob has never been executed
2 (Auto): CronJob was last executed directly via the "real" Cron (onDemand).
4 (Lazy): CronJob was called up with a time delay via the LazyCron.
8 (Force): The CronJob was started manually
16 (Error): The last call ended with an error (see log).
triggerStrStringUnknownThis is just a getter property.
lastRunInteger0Contains the last execution time as a Unix timestamp. Stored and retrieved in the ProcessWire cache.
lastErrorStringPossible error message from the last call

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Latest news

  • ProcessWire Weekly #550
    In the 550th issue of ProcessWire Weekly we're going to check out the latest core updates, introduce a new third party module called PagefileMetadata, and more. Read on!
    Weekly.pw / 23 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“…building with ProcessWire was a breeze, I really love all the flexibility the system provides. I can’t imagine using any other CMS in the future.” —Thomas Aull