Download the PHP package peppeocchi/php-cron-scheduler without Composer

On this page you can find all versions of the php package peppeocchi/php-cron-scheduler. 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?
peppeocchi/php-cron-scheduler
Rate from 1 - 5
Rated 5.00 based on 1 reviews

Informations about the package php-cron-scheduler

PHP Cron Scheduler

Latest Stable Version License Build Status Coverage Status StyleCI Total Downloads

This is a framework agnostic cron jobs scheduler that can be easily integrated with your project or run as a standalone command scheduler. The idea was originally inspired by the Laravel Task Scheduling.

Installing via Composer

The recommended way is to install the php-cron-scheduler is through Composer. Please refer to Getting Started on how to download and install Composer.

After you have downloaded/installed Composer, run

php composer.phar require peppeocchi/php-cron-scheduler

or add the package to your composer.json

Scheduler V4 requires php >= 7.3, please use the v3 branch for php versions < 7.3 and > 7.1 or the v2 branch for php versions < 7.1.

How it works

Create a scheduler.php file in the root your project with the following content.

Then add a new entry to your crontab to run scheduler.php every minute.

`

That's it! Your scheduler is up and running, now you can add your jobs without worring anymore about the crontab.

Scheduling jobs

By default all your jobs will try to run in background. PHP scripts and raw commands will run in background by default, while functions will always run in foreground. You can force a command to run in foreground by calling the inForeground() method. Jobs that have to send the output to email, will run foreground.

Schedule a php script

The php method accepts 4 arguments:

Schedule a raw command

The raw method accepts 3 arguments:

Schedule a function

The call method accepts 3 arguments:

All of the arguments you pass in the array will be injected to your function. For example

If you want to pass a key => value pair, please pass an array within the arguments array

Schedules execution time

There are a few methods to help you set the execution time of your schedules. If you don't call any of this method, the job will run every minute ( *).

There are additional helpers for weekdays (all accepting optionals hour and minute - defaulted at 00:00)

And additional helpers for months (all accepting optionals day, hour and minute - defaulted to the 1st of the month at 00:00)

You can also specify a date for when the job should run. The date can be specified as string or as instance of DateTime. In both cases you can specify the date only (e.g. 2018-01-01) or the time as well (e.g. 2018-01-01 10:30), if you don't specify the time it will run at 00:00 on that date. If you're providing a date in a "non standard" format, it is strongly adviced to pass an instance of DateTime. If you're using createFromFormat without specifying a time, and you want to default it to 00:00, just make sure to add a ! to the date format, otherwise the time would be the current time. Read more

Send output to file/s

You can define one or multiple files where you want the output of your script/command/function execution to be sent to.

Send output to email/s

You can define one or multiple email addresses where you want the output of your script/command/function execution to be sent to. In order for the email to be sent, the output of the job needs to be sent first to a file. In fact, the files will be attached to your email address. In order for this to work, you need to install swiftmailer/swiftmailer

You can optionally customize the Swift_Mailer instance with a custom Swift_Transport. You can configure:

The configuration can be set "globally" for all the scheduler commands, when creating the scheduler.

Or can be set on a job per job basis.

Schedule conditional execution

Sometimes you might want to execute a schedule not only when the execution is due, but also depending on some other condition.

You can delegate the execution of a cronjob to a truthful test with the method when.

Schedules execution order

The jobs that are due to run are being ordered by their execution: jobs that can run in background will be executed first.

Schedules overlapping

To prevent the execution of a schedule while the previous execution is still in progress, use the method onlyOne. To avoid overlapping, the Scheduler needs to create lock files. By default it will be used the directory path used for temporary files.

You can specify a custom directory path globally, when creating a new Scheduler instance.

Or you can define the directory path on a job per job basis.

In some cases you might want to run the job also if it's overlapping. For example if the last execution was more that 5 minutes ago. You can pass a function as a second parameter, the last execution time will be injected. The job will not run until this function returns false. If it returns true, the job will run if overlapping.

Before job execution

In some cases you might want to run some code, if the job is due to run, before it's being executed. For example you might want to add a log entry, ping a url or anything else. To do so, you can call the before like the example below.

After job execution

Sometime you might wish to do something after a job runs. The then methods provides you the flexibility to do anything you want after the job execution. The output of the job will be injected to this function. For example you might want to add an entry to you logs, ping a url etc... By default, the job will be forced to run in foreground (because the output is injected to the function), if you don't need the output, you can pass true as a second parameter to allow the execution in background (in this case $output will be empty).

Using "before" and "then" together

Multiple scheduler runs

In some cases you might need to run the scheduler multiple times in the same script. Although this is not a common case, the following methods will allow you to re-use the same instance of the scheduler.

Another handy method if you are re-using the same instance of the scheduler with different jobs (e.g. job coming from an external source - db, file ...) on every run, is to clear the current scheduled jobs.

Faking scheduler run time

When running the scheduler you might pass an DateTime to fake the scheduler run time. The resons for this feature are described here;

Job failures

If some job fails, you can access list of failed jobs and reasons for failures.

Worker

You can simulate a cronjob by starting a worker. Let's see a simple example

The above code starts a worker that will run your job/s every minute. This is meant to be a testing/debugging tool, but you're free to use it however you like. You can optionally pass an array of "seconds" of when you want the worker to run your jobs, for example by passing [0, 30], the worker will run your jobs at second 0 and at second 30 of the minute.

It is highly advisable that you run your worker separately from your scheduler, although you can run the worker within your scheduler. The problem comes when your scheduler has one or more synchronous job, and the worker will have to wait for your job to complete before continuing the loop. For example

The above will skip more than one execution, so it won't run anymore every minute but it will run probably every 2 or 3 minutes. Instead the preferred approach would be to separate the worker from your scheduler.

Then in your command line run php worker.php. This will start a foreground process that you can kill by simply exiting the command.

The worker is not meant to collect any data about your runs, and as already said it is meant to be a testing/debugging tool.

License

The MIT License (MIT)


All versions of php-cron-scheduler with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3 || ^8.0
dragonmantank/cron-expression Version ^3.0
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 peppeocchi/php-cron-scheduler contains the following files

Loading the files please wait ....