Documentation 1.x

Plugin cron jobs

Cron is a function that is executed regularly. If your plugin needs to run regular maintenance, the best place to add that code is in cron function.

How to create cron function in your plugin?

You need to create file with name cron.php in your plugin directory. And create a Cron class inside it:

<?php

namespace Modules\group_name\module_name; //change module_group and module_name to actual values of your plugin.

if (!defined('FRONTEND')&&!defined('BACKEND')) exit; //check if file is not accessed directly

class Cron{ //leave intact

  /**
   * Is executed each time, when main system cron job is executed.
   *
   * Parameter $options store information about last executed cron job.
   *
   * $options->firstTimeThisYear - true if cron is executed first time this year
   * $options->firstTimeThisMonth - true if cron is executed first time this month
   * $options->firstTimeThisWeek - true if cron is executed first time this week
   * $options->firstTimeThisDay - true if cron is executed first time this day
   * $options->firstTimeThisHour - true if cron is executed first time this hour
   * $options->lastTimeBefore - last cron execution time
   *
   * @param $options cronInformation
   */
  function execute($options){

    if($options->firstTimeThisDay){
      //do whatherver you like
    }
  }

}

Function execute will be executed each time, when main system cron will be executed. Use $options variable to get information about last execution time.

How to test your cron?

You can run your cron by visiting http://www.example.com/ip_cron.php?test=1. Test variable forces the script to execute cron each time the link is visited. It forces all variables, starting from $options->firstTimeThisDay to $options->firstTimeThisYear to be true even if the script was executed just a second ago.

Example plugin with cron function

Write a comment

You must be logged in to comment.