• Services

Cronjobs

If your Module needs to receive big amounts of data from external servers or process lots of data, it might be neccesary to do this asynchroniously, not at the moment the user actually needs that data.

That's why the Incluble platform supports cronjobs.

Setting up cronjobs

Adding a cronjob is as simple as adding a new controller to your module.json file.

module.json

{
    "controllers": {
        "cron-60": "cron.php"
    },
    ...
}

The above example will create a cron job which runs every 60 minutes. You can change this value to any value you want. It is also possible to create multiple cronjobs, but not two with the same interval.

The maximum number of cronjobs per module is theoretically unlimited, but please don’t use more cronjobs then neccesary. Also try to use as big of an interval as possible for the best performance.

Class availability

Most of the PHP classes in the Module API are also available in cronjobs, with the following exceptions:

  • $this->user is not available because the cronjob is called on a Community-level, not for an individual user.
  • $this->request only contains the method variable and $this->response is unavailable, because there isn’t a HTTP request when executing a cronjob.
  • $this->site->theme is missing.

You can still access the Site, Storage and Language classes, along with some others.

Implementation details

Limitations

Cron job executions have a soft time limit of 30 seconds, and a hard limit of 120 seconds. After 120 seconds the cron job is killed and marked as failed.

Note that for cron jobs with an interval of less than 120 seconds, the time limit is equal to INTERVAL - 10s. So for a cron job that runs every minute, the time limit is 50 seconds.

Each cron job execution is executed in a separate PHP process, where it has access to a maximum of 128 MB of memory. If your script tries to allocate more than this amount of memory, it is also killed and marked as failed.

To work around this limitation, we recommend combining cron jobs with queue messages. Your cron job could detect the kind of work that needs to be done, and then spread the workload by dispatching queue messages.

Do note that all queue executions have the same time and memory limitations as described above, so you will need to split up your workload. Simply doing the exact same action through a queue execution that doesn't work in a cron job won't help you.

Precision

Background tasks on Includable don't have an exact precision. We prefer fairly sharing resources between modules over firing with second-precision.

That means that your cronjob might be executed slightly later than you expect. Our allowed variance is one minute above or below your interval. So if you have cron job that is supposed to run every 60 seconds, it might run on minute 60, but also on minute 59 or minute 61.

This imprecision allows us to schedule cron jobs with a focus on giving all modules an equal opportunity to have their cron jobs executed, even when one module has many more cron jobs than another one.

Penalties

When a cron job fails, either due to a script or parse error, or crossing the time and memory limits, it will be marked as failed.

Cron jobs that often fail, might be executed less often, to make sure that cron jobs that constantly leak memory or leave dangling connections don't hog up all of the system resources.

Penalties are calculated by looking at the results of the cron job over the past 24 hours. As soon as a cron job starts behaving normally again, it will slowly revert back to the normal interval defined in module.json.

Development environment

The Includable development environment (sadev.io) does support background tasks, but has slightly different limitations from the production platform:

  • Background processes stop running after a community has not been used (by running inc run or similar) for over a week.
  • All tasks have a time limit of 20 seconds and 64 MB. Tasks that exceed these limits will be killed automatically.
  • The minimum interval of a cron job in the development environment is 60 seconds.