Cron Job

Navigation:  Development > Helpers >

Cron Job

Previous pageReturn to chapter overviewNext page

Cron Helper used to run any job in a particular time.  We have two steps to setup this scheduler process.
 
Step 1: In first steps we create Scheduler Functions in helper file.
Step 2: In second step we add Cron File in a any scheduler.
 
Cron File:
There is a Cron file in Webroot folder that we have to add it in any scheduler like Schedule Job, Cron Tab or CLI to execute in a particular time. See below file location:

 
webroot/cron.php

 

This file can execute both in URL or Directory path mode.
 

Crate Schedule:

Below is the helper location to write all jobs :
 
/development/helper/cronjob.php

 

A function is considered as a schedule job if we add a keyword "Job" as a post fix. See below example.
 

protected function testfxJob()
{
    // Do operation
}
 

System always read the schedule time from the comments of a function. We write the comments like below
 
@schedule [07:00-8:00 12:00-13:00 18:00-20:30]

 

According to the above format the schedule function runs if Cron File run between 07:00 or 8:00 and 12.00 and 13.00

 

Also we can create like @schedule [*] then function will run each time Cron File execute.

 

See below example:

 

class Development_Helper_Cronjob extends appRain_Base_Modules_Cronjob
{
    /**
     * An Example funcntion
     * on each time Cron Job run
     *
     * @schedule [*]
     */
     protected function testFx1Job()
     {
     }
}
 

Step by Step example:

Lets see an example to send an email in a particular period. 
 
Step 1: First Create below method in 
 
/development/helper/cronjob.php
 
class Development_Helper_Cronjob extends appRain_Base_Modules_Cronjob
{
    /**
     * An Example funcntion
     * on each time Cron Job run
     *
     * @schedule [*]
     */
     protected function sendMyMailJob()
     {
        App::Utility()->Mailing('jhon@example.com','info@example.com','Cron Notification','This is a test Cron email');
        echo "Mail event execute successfully.";
     }
}

 

Step 2: Now add below file in a Cron Tab or Scheduler job. You can execute it both in URL or CLI on Directory path.
 
For a simple test browse www.example.com/cron.php.