sFire PHP Framework

Schedule

Cronjobs also known as Schedules in sFire, are PHP scripts that can run in the background / terminal. This can be handy if you want to mail or run an import on a specific time without navigation to a specific URL in a browser.

Note: To run a schedule, you have to have "exec" privilages.

In this section we will handle

  • Getting ready
  • Blueprint / example
  • Setting the time
  • Enable / disable schedule
  • Start and complete methods
  • Run a Schedule manually
  • Logging
  • Simulate a request and execute controllers

Getting ready

To use the Schedule, you only have to set one cronjob to run every minute and sFire will do the rest for you.

Cronjob
* * * * * php vendor/sfire/framework/src/Schedule/Schedule.php

Blueprint / example

Below is the blueprint of a Schedule. A Schedule always has a start method and an optional complete method.

<?php
class ScheduleExample {

    const TIME      = '* * * * *';
    const ENABLED   = true;

    public function start() {
        return;
    }

    public function complete($output) {
    }
}
?>

Note: It is absolutly necessary that there are no characters like spaces or enters outside the PHP tags. Otherwise you get an error message "Warning: Cannot modify header information - headers already sent".

Setting the time

Like any other cronjob, you can set the time for this schedule to run. It uses the same format as the time format of a cronjob.

The following graph shows what it consists of:

* * * * *
| | | | |
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)

Enable / disable schedule

To enable or disable a schedule, you can change the ENABLED constant. This constant is a Boolean true to enable the schedule or false to disable the schedule.

//Disable the schedule
const ENABLED = false;

Start and complete methods

Every Schedule must have a start method and a optional complete method.

Start method

This method is the heart of the Schedule and will be called if the Schedule is enabled and when the Schedule should run based on a given time. Here you can mail or start an import, but you can even simulate a request to your application (see the "Simulate request and execute controllers" section for more information).

Everything you return or echo in this function will be logged and can be retrieved in the "output" parameter of the complete method.

public function start() {

    echo 'Hello';
    return ' world!';
}
Complete method

The complete method is a optional method which will be triggerd when the start method has finished. In other words, when this method is called, you can be certain that the start method successfully ran. This method comes with an invoked parameter which contains the start date, end date, everything that has been echoed using PHP "echo" and returned in the "start" method.

public function complete($output) {

    print_r($output);

    //Output similar to:
    array (
        'start' => '2018-01-01 12:00:00',
        'echo' => 'Hello',
        'return' => ' world!',
        'end' => '2018-01-01 12:00:05',
    )
}

Run a Schedule manually

You can also run a Schedule manually by calling the Schedule.php and giving the Schedule without the prefix you would like to run as parameter:

php vendor/sfire/framework/src/Schedule/Schedule.php Example

This will run "schedules/ScheduleExample.php". By default, the name of a Schedule begins with "Schedule" (you may change this configuration in the app.php).

Logging

Every schedule will be automatically logged in the "data/log/schedule/" folder. The log files will be appended the start date, end date, everything that has been echoed using PHP "echo" and returned in the "start" method as a JSON object.

Simulate request and execute controllers

If you want to use your application MVC structure by calling controller, you can use the redirect method of the "sFire\Routing\Router" class.

use sFire\Routing\Router;

public function start() {

    if(true === Router :: redirect('customer.details') -> params([52]) -> get()) {
        echo 'Success';
    }
}

If you are using domains in your routes.php you must give the domain name with HTTP protocol:

use sFire\Routing\Router;

public function start() {

    if(true === Router :: redirect('customer.details') -> params([52]) -> domain('example.com', 'https') -> get()) {
        echo 'Success';
    }
}