Download the PHP package bayfrontmedia/cron-scheduler without Composer
On this page you can find all versions of the php package bayfrontmedia/cron-scheduler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bayfrontmedia/cron-scheduler
More information about bayfrontmedia/cron-scheduler
Files in bayfrontmedia/cron-scheduler
Package cron-scheduler
Short Description A flexible framework-agnostic cron job scheduler using human-readable expressions.
License MIT
Homepage https://github.com/bayfrontmedia/cron-scheduler
Informations about the package cron-scheduler
Cron Scheduler
A flexible framework-agnostic cron job scheduler using human-readable expressions.
- License
- Author
- Requirements
- Installation
- Usage
License
This project is open source and available under the MIT License.
Author
Requirements
- PHP
^8.0
Installation
Usage
Start using Cron Scheduler
First, create a file to be used to schedule jobs, for example cron.php
. The file can be named whatever you like.
Then, add a new entry to your crontab to run the file every minute:
Or, to save the output from the jobs to a log file, specify a file path, such as:
Now, your server will check the file every minute, and Cron Scheduler will only run the jobs that are due, according to their schedule.
Creating an instance
NOTE: All exceptions thrown by Cron Scheduler extend Bayfront\CronScheduler\CronException
, so you can choose to catch exceptions as narrowly or broadly as you like.
The constructor accepts two parameters as strings: $lock_file_path
and $output_file
.
To prevent overlapping jobs, Cron Scheduler creates temporary "lock" files.
These files are created for each job once it begins, and deleted once it completes.
Jobs will be skipped when a lock file exists, even if it is due to run.
If $lock_file_path === NULL
, lock files will never be created, and all jobs will be allowed to overlap.
When an $output_file
is specified, any output from the jobs that run will be saved to this file, unless a custom file is specified specifically for that job (see saveOutput).
This has the same effect as specifying a log file in the crontab (see above).
The constructor may throw a Bayfront\CronScheduler\FilesystemException
exception.
Example cron.php
:
Public methods
- getJobs
- getPreviousDate
- getNextDate
- run
- raw
- php
- call
- always
- when
- saveOutput
Job schedule
- at
- everyMinutes
- hourly
- everyHours
- daily
- weekly
- monthly
- everyMonths
- annually
- sunday
- monday
- tuesday
- wednesday
- thursday
- friday
- saturday
- january
- february
- march
- april
- may
- june
- july
- august
- september
- october
- november
- december
getJobs
Description:
Return scheduled cron jobs.
Parameters:
- (None)
Returns:
- (array)
Example:
getPreviousDate
Description:
Get previous date a given job was scheduled to run.
Parameters:
$label
(string)$date_format = 'Y-m-d H:i:s'
(string): Date format to return
See: https://www.php.net/manual/en/datetime.format.php
Returns:
- (string)
Throws:
Bayfront\CronScheduler\LabelExistsException
Bayfront\CronScheduler\SyntaxException
Example:
getNextDate
Description:
Get next date a given job is scheduled to run.
Parameters:
$label
(string)$date_format = 'Y-m-d H:i:s'
(string): Date format to return
See: https://www.php.net/manual/en/datetime.format.php
Returns:
- (string)
Throws:
Bayfront\CronScheduler\LabelExistsException
Bayfront\CronScheduler\SyntaxException
Example:
run
Description:
Runs all queued jobs that are due.
Parameters:
$current_time = 'now'
(DateTimeInterface
): Override current time by passing aDateTime
instance with a given time.
Returns:
- (array): Array of data relating to the completed jobs
Throws:
Bayfront\CronScheduler\FilesystemException
The array that is returned will contain the following keys:
jobs
: Array of jobs that ran, each with their ownstart
,end
,elapsed
andoutput
keys.start
: Timestamp of when the jobs started.end
: Timestamp of when the jobs ended.elapsed
: Total seconds elapsed to complete jobs.count
: Number of jobs that ran.
Throws:
Bayfront\CronScheduler\FilesystemException
Example:
Another example, this time overriding (or, "spoofing") the current time:
raw
Description:
Adds a raw command as a job.
By default, the job will run every minute.
Parameters:
$label
(string): Unique label to assign to this job$command
(string)
Returns:
- (self)
Throws:
Bayfront\CronScheduler\LabelExistsException
Example:
php
Description:
Adds a php file as a job.
By default, the job will run every minute.
Parameters:
$label
(string): Unique label to assign to this job$file
(string): Full path to file
Returns:
- (self)
Throws:
Bayfront\CronScheduler\LabelExistsException
Example:
call
Description:
Adds a callable function as a job.
NOTE: Functions should return
, not echo
output.
By default, the job will run every minute.
Parameters:
$label
(string): Unique label to assign to this job$callable
(callable)$params = []
(array): Parameters to pass to the callable
Returns:
- (self)
Throws:
Bayfront\CronScheduler\LabelExistsException
Example:
always
Description:
Always run job, even if previous execution is still in progress.
This prevents a lock file from being created for this job.
Parameters:
- (None)
Returns:
- (self)
Example:
when
Description:
Add a condition for the job to run, even if it is due.
The job will only run if the return value of $callable
is TRUE
.
Parameters:
$callable
(callable)$params = []
(array)
Returns:
- (self)
Example:
saveOutput
Description:
Save the output of the job to a given file.
This will override $output_file
, if specified in the constructor.
Parameters:
$output_file
(string)
Returns:
- (self)
Example:
at
Description:
Schedule job to run using a valid cron expression.
Parameters:
$at
(string)
Returns:
- (self)
Example:
everyMinutes
Description:
Schedule job to run every x number of minutes.
Parameters:
$minutes = 1
(int)
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
hourly
Description:
Schedule job to run on a given minute of every hour.
Parameters:
$minute = 0
(int)
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
everyHours
Description:
Schedule job to run on the hour every x number of hours.
Parameters:
$hours = 1
(int)
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
daily
Description:
Schedule job to run at a given time of every day.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
weekly
Description:
Schedule job to run on a given weekday and time of every week.
Parameters:
$weekday = 0
(int): 0-6 as Sunday-Saturday$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
monthly
Description:
Schedule job to run on a given day and time of every month.
Parameters:
$day = 1
(int): 1-31 as day of the month$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
everyMonths
Description:
Schedule job to run at a given time on the first day every x number of months.
Parameters:
$months = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
annually
Description:
Schedule job to run on a given month, day and time each year.
Parameters:
$month = 1
(int)$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
sunday
Description:
Schedule job to run at a given time every Sunday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
monday
Description:
Schedule job to run at a given time every Monday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
tuesday
Description:
Schedule job to run at a given time every Tuesday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
wednesday
Description:
Schedule job to run at a given time every Wednesday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
thursday
Description:
Schedule job to run at a given time every Thursday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
friday
Description:
Schedule job to run at a given time every Friday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
saturday
Description:
Schedule job to run at a given time every Saturday.
Parameters:
$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
january
Description:
Schedule job to run on a given day and time each January.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
february
Description:
Schedule job to run on a given day and time each February.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
march
Description:
Schedule job to run on a given day and time each March.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
april
Description:
Schedule job to run on a given day and time each April.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
may
Description:
Schedule job to run on a given day and time each May.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
june
Description:
Schedule job to run on a given day and time each June.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
july
Description:
Schedule job to run on a given day and time each July.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
august
Description:
Schedule job to run on a given day and time each August.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
september
Description:
Schedule job to run on a given day and time each September.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
october
Description:
Schedule job to run on a given day and time each October.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
november
Description:
Schedule job to run on a given day and time each November.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
december
Description:
Schedule job to run on a given day and time each December.
Parameters:
$day = 1
(int)$time = '00:00'
(string): Time in 24-hour format without leading zeros
Returns:
- (self)
Throws:
Bayfront\CronScheduler\SyntaxException
Example:
All versions of cron-scheduler with dependencies
bayfrontmedia/php-string-helpers Version ^2.0
dragonmantank/cron-expression Version 3.3.*