PHP Luminova: Scheduling Filesystem Tasks with Novakit
The Luminova Base Cron class offers advanced scheduling capabilities, allowing you to define specific controllers and methods for cron job executions.
Define filesystem-backed scheduled tasks in App\Tasks\TaskScheduler, which extends Luminova\Base\AbstractTaskScheduler. Register each command controller and method inside schedule(), then configure when it runs and where its log or output goes.
namespace App\Tasks;
use App\Controllers\Cli\DailyCommand;
use Luminova\Base\AbstractTaskScheduler;
final class TaskScheduler extends AbstractTaskScheduler
{
protected function schedule(): void
{
$this->service(DailyCommand::class, 'refresh')
->days()
->log('info');
}
}This example assumes DailyCommand::refresh() is a public command controller method. Run Novakit from your application directory to initialize and process the scheduled tasks:
php novakit cron:create
php novakit cron:runTo check for due tasks every minute, use the application’s absolute path in the crontab entry:
* * * * * cd /path/to/app && php novakit cron:runThe interval controls when a task becomes due. The worker must be running for the task to execute.
Class Definition
- Class namespace:
Luminova\Base\AbstractTaskScheduler - This class is an Abstract class
Methods
constructor
Initialize constructor, additionally you can set a custom location to store cron lock file and the filename.If not set, the default location will be used which is writeable/cron/schedules.json.
public function __construct()schedule
To define scheduled cron jobs within the extended AbstractTaskScheduler class, use this method.Called method service withing the schedule method, each service should have its own configurations.
abstract protected function schedule(): void;service
To define a specific service for execution in a cron job, use this method to specify the namespace of your command controller and the method to be called for the job execution.
Pass the command controller class and method name as separate arguments.
protected final function service(string $class, string $method): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$class | string | The controller class. |
$method | string | The controller method to execute. |
Return:
self - Returns the scheduler instance.
Note: Use a command controller that extends
Luminova\Base\Cli\AbstractController.
onComplete
To set a callback to execute on a successful task completion.
protected final function onComplete(callable $onComplete): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$onComplete | callable | The callback function to execute on completion. - Closure with one array parameter of task details. |
Return:
self - Returns the scheduler instance.
onFailure
To set a callback to execute on a failure to complete task.
protected final function onFailure(callable $onError): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$onError | callable | The callback function to execute on failure. - Closure with one array parameter of task details. |
Return:
self - Returns the scheduler instance.
pingOnComplete
To set a callback URL to ping on a successful task execution.The request payload will contain the executed task details.
protected final function pingOnComplete(string $url): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL to ping on completion. |
Return:
self - Returns the scheduler instance.
pingOnFailure
To set a callback URL to ping on a failure task execution.The request payload will contain the executed task details.
protected final function pingOnFailure(string $url): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL to ping on failure. |
Return:
self - Returns the scheduler instance.
log
To set a log level to use in logging the execution response messages and exceptions that my occur during execution of task.
protected final function log(string $level): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$level | string | The log level to use while logging execution response. |
Return:
self - Returns the scheduler instance.
Log levels
You can choose any of the following log levels.
- emergency
- alert
- critical
- error
- warning
- notice
- info
- debug
- exception
- php_errors
output
To set a log path where all output like error messages or other exceptions that my occur during execution of task.
protected final function output(string $filepath): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$filepath | string | The output full path and filename. |
Return:
self - Returns the scheduler instance.
This log is not related to your method, it mainly other errors that may occur due to code errors or probably PHP or framework errors.
description
Set a description for the cron task, the description will be used in logging your response.
protected final function description(string $description): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$description | string | The description of the cron job. |
Return:
self - Returns the scheduler instance.
seconds
To execute cron task at a specific seconds.
protected final function seconds(int $seconds = 5): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$seconds | int | Interval in seconds (default: 5). |
Return:
self - Returns the scheduler instance.
The minimum execution time which can be set in cron is
1 minute, using this method can only be useful for debugging in development environments, if you use it in production then your task will always be executed on every call except if use set a longer seconds.
minutes
To execute cron task at a specific minutes.
protected final function minutes(int $minutes = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$minutes | int | Interval in minutes. |
Return:
self - Returns the scheduler instance.
hours
To execute cron task at a specific hour(s).
protected final function hours(int $hours = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$hours | int | Interval in hours. |
Return:
self - Returns the scheduler instance.
days
To execute cron task at a specific day.
protected final function days(int $days = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$days | int | Interval in days. |
Return:
self - Returns the scheduler instance.
weeks
To execute cron task at a specific week.
protected final function weeks(int $weeks = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$weeks | int | Interval in weeks. |
Return:
self - Returns the scheduler instance.
months
To execute cron task in specific month(s).
protected final function months(int $months = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$months | int | Interval in months. |
Return:
self - Returns the scheduler instance.
years
To execute cron task in specific year(s).
protected final function years(int $years = 1): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$years | int | Interval in years. |
Return:
self - Returns the scheduler instance.
What Next?
Advanced Task Queue SystemLearn about the task queue system, including scheduling, monitoring, and background processing.
Cron Tasks ConfigurationRead how to define cron tasks using static configuration files.
Running Cron Jobs with NovaKitUnderstand how to execute and manage cron jobs through the CLI.
interval
Set the DateInterval format using a date/time designator.
public function interval(string $format, int $value, string $designator): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$format | string | DateInterval period prefix. |
$value | int | The numeric interval value. |
$designator | string | DateInterval designator. |
Return:
self - Returns the current instance for method chaining.
calendar
Sets a calendar-based execution date and time.
public function calendar(DateTimeInterface|string $datetime): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$datetime | DateTimeInterface|string | Execution date and time. |
Return:
self - Returns the current instance for method chaining.
timezone
Sets the timezone used for date and time calculations.
public function timezone(DateTimeZone|string $timezone): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timezone | DateTimeZone|string | Timezone instance or identifier. |
Return:
self - Returns the current instance for method chaining.
expression
Set task execution interval using the cron expression.
protected final function expression(string $interval): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$interval | string | Cron interval expression in (format: minute hour day month weekday). |
Return:
self - Return instance of file task queue.
getDefinitions
Retrieves task definitions from the cron scheduler.
public final function getDefinitions(?int $id = null): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$id | ?int | Optional controller ID to retrieve a specific task. |
Return:
array<int,array<string,mixed>>|array<string,mixed> - Returns indexed definitions when no ID is supplied, a single definition when the ID matches, or an empty array when it does not. Read the definitions once and reuse the result when looking up multiple tasks.
taskExists
Checks whether a controller exists in the cron scheduler.
public final function taskExists(int $id): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$id | int | Controller ID to check. |
Return:
bool - Returns true if the controller exists, false otherwise.
getTasks
Retrieves scheduled tasks from the configuration file.
public final function getTasks(): arrayReturn:
array<string,array<string,mixed>> - Returns persisted task definitions keyed by the controller and method string. Each definition has the following fields.
| Key | Type | Description |
|---|---|---|
id | int | Definition index. |
description | string | Task description. |
controller | string | Scheduled controller and method. |
lastExecution | int | Unix timestamp recorded for the most recent run, or the initial scheduling time. |
lastRunCompleted | bool | Whether the previous execution completed. |
completed | int | Successful execution count. |
failures | int | Failed execution count. |
retries | int | Retry count. |
log | string|null | Configured log level. |
output | string|null | Configured output file path. |
onFailure | bool | Whether a failure callback is registered. |
onComplete | bool | Whether a completion callback is registered. |
pingOnComplete | string|null | Completion webhook URL. |
pingOnFailure | string|null | Failure webhook URL. |
interval | array<string,string> | Interval format and timezone, when configured. |