Download the PHP package xfra35/f3-cron without Composer

On this page you can find all versions of the php package xfra35/f3-cron. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package f3-cron

Cron

Job scheduling for the PHP Fat-Free Framework

This plugin for Fat-Free Framework helps you control job scheduling directly from your web app.

Installation

To install this plugin, just copy the lib/cron.php file into your lib/ or your AUTOLOAD folder.

Operation and basic usage

This plugin provides your app with an external interface to run scheduled jobs. The interface consists in 2 routes automatically added to your app:

By default, this interface is accessible in CLI mode only and is meant to be called by the server job scheduler:

  1. Unix cron or Windows Task Scheduler calls index.php /cron every minute (or at a slower rate).
  2. index.php /cron checks for due jobs at that time and executes them, asynchronously if possible.

Step 1:

Configure your server job scheduler so that it calls php index.php /cron every minute.

Here's how to do it on a *nix server, assuming that your application resides in /path/to/app/index.php:

NB1: depending on your hosting, you may need to ask your provider to perform that step.

NB2: if your cron job needs disk write access, take care about the UNIX user permissions.

Step 2:

Instantiate the Cron class and define the list and frequency of jobs with the following commands:

That's it!

Job1 will run every day and Job2 every week.

Schedule format

Crontab

Each job is scheduled using the (nearly) standard crontab format, which consists of 5 fields separated by spaces:

Each field may be:

Ranges have a default step value of 1, which can be adjusted using a /:

Examples

Presets

For easier reading, it is possible to define presets:

The following presets are defined by default:

Options

Logging

If you set $cron->log=TRUE, every successfully executed job will be logged in a cron.log file located in the LOGS folder.

Web interface

By default, the routes GET /cron and GET cron/@job are available in CLI mode only, which means that an HTTP request to them will throw a 404 error.

You can enable web routes by setting $cron->web=TRUE.

In that case, /cron can be triggered via HTTP on a periodic basis, for example by your web app, or by a web cron service, or even by your own crontab:

Script path

By default, the script called asynchronously is index.php located in the current working directory.

You may need to tweak this value if:

Examples:

PHP binary path

By default, the PHP binary used to trigger asynchronous job executions is either php or php-cli (smart guess).

You may need to tweak this value if none of these values correspond to an executable PHP CLI binary or if they are not in the path.

Example:

The PHP binary validity is checked every time the class is instanciated, which can lead to a performance hit (see https://github.com/xfra35/f3-cron/issues/9).

You can skip this check by forcing the path, using the 2nd parameter:

In that case, you are responsible for providing the correct path.

Ini configuration

Configuration is possible from within an .ini file, using the CRON variable. E.g:

If you need to force the PHP binary path, just pass TRUE as a 2nd parameter:

IMPORTANT: Don't forget to instantiate the class before running your app:

Asynchronicity

As configured in step 1, the cron plugin is instantianted every minute. Each instance is run independantly from each other.

Within an instance, there may be several due jobs, which can be run synchronously or asynchronously.

If you want due jobs to be run asynchronously within an instance, you'll need:

NB: The plugin will detect automatically if jobs can be run asynchronously. If not, jobs will be executed synchronously, which may take longer and add a risk of queue loss in case of a job failure.

Common pitfalls

UNIX user permissions

If one of your cron jobs writes data to disk, you may face some permission issues if both the cron user and the web server user try to write data to the same files.

For example, let's imagine that you cron job is executed as root and renders a HTML template to embed it in a reporting email. Then the next time the web server will try to recompile this template, it will not be allowed to modify the temporary file located in $f3->TEMP and the web user will face a 500 error.

See this issue for another example.

Here are two different ways to fix that kind of issue:

Overlapping jobs

If a job runs longer than what it was designed for, one instance may overlap another (e.g: a job running every 5 min that completes in 6 min).

Depending on the type of job, this situation may be undesirable (risk of data corruption).

If that's the case, you should prevent jobs from overlapping by using one of the following solutions:

Setting a max execution time

The max execution time in CLI mode defaults to 0. That means scripts can run forever.

Setting that parameter to a value slightly smaller than the job frequency will prevent jobs from overlapping.

NB: don't forget to send a report on job failure, otherwise you could end up with all jobs failing silently.

Using a resource locking mechanism

Inside your application code, you could keep track of running jobs using a lock file or a database flag, so that two cron instances can't execute the same job at the same time.

NB: don't forget to handle stale locks.

Alternatively, you can use the flock binary, which provides automatic lock management. Beware that this solution is slightly different as it prevents two cron instances (not jobs) from executing at the same time: if "job1" is still running, "job1" and "job2" will be skipped. This solution is easy to implement though: just replace the crontab defined in step 1 with the following one:

NB: the cron.lock can be located anywhere, provided the cron has write access to it.

API

log

Logging of successfully executed jobs (default=FALSE)

web

Web interface (default=FALSE)

script

Path of the script to call asynchronously (default='index.php')

Defaults to index.php in the current working directory.

clipath

Alias for script [deprecated]

binary

Path of the PHP CLI binary (default='php' or 'php-cli')

binary( $path, $force=FALSE )

Set PHP CLI binary path if it's valid (which means if it can be executed and is CLI)

If $force=TRUE, the path is forced without validation check. This option can be used for performance optimization (see https://github.com/xfra35/f3-cron/issues/9).

silent

Silent mode (default=TRUE)

Disable silent mode if you want the script to output the list of executed jobs.

set( $job, $handler, $expr )

Schedule a job

NB: Valid characters for job names are alphanumeric characters and hyphens.

preset( $name, $expr )

Define a schedule preset

NB: Valid characters for job names are alphanumeric characters.

isDue( $job, $time )

Returns TRUE if the requested job is due at the given time

execute( $job, $async=TRUE )

Execute a job

run( $time=NULL, $async=TRUE )

Run scheduler, i.e executes all due jobs at a given time


All versions of f3-cron with dependencies

PHP Build Version
Package Version
Requires bcosca/fatfree-core Version ~3.5
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package xfra35/f3-cron contains the following files

Loading the files please wait ....