Tasks
This document provides a guide for developers on how to create and run tasks in the WebFramework. Tasks are used to perform specific operations, such as database initialization or updates, and can be executed from the command line.
Overview
Tasks in WebFramework implement the Task, which defines a single method, execute(). This method contains the logic for the task. Tasks are executed using the TaskRunner, which sets up the application environment and runs the specified task.
Creating Custom Tasks
To create a custom task, you need to implement the Task and define the execute() method. This method should contain the logic for your task.
Example: Custom Task
| PHP | |
|---|---|
In this example, the CustomTask class implements the Task and defines the execute() method, which contains the task logic.
Running Tasks from the Command Line
Tasks can be executed from the command line using PHP scripts. These scripts initialize the TaskRunner and execute the specified task.
Example: Running a Task
To run a task from the command line, create a script similar to the following:
| PHP | |
|---|---|
This script initializes the TaskRunner, builds the application environment, and executes the CustomTask.
Existing Tasks
The WebFramework includes several predefined tasks for common operations. Here are the existing tasks and their purposes:
SlimAppTask
- Purpose: Initializes and runs the Slim application.
- Script:
public/index.php - Script: Typically run as part of the web server setup, not directly from the command line.
DbInitTask
- Purpose: Initializes the database schema.
- Script:
scripts/db_init.php - Usage: Run this script to set up the initial database schema.
DbUpdateTask
- Purpose: Updates the database schema to the latest version.
- Script:
scripts/db_update.php - Usage: Run this script to apply database migrations and update the schema.
DbVersionTask
- Purpose: Displays the current database version.
- Script:
scripts/db_version.php - Usage: Run this script to check the current version of the database schema.
SanityCheckTask
- Purpose: Runs sanity checks on the application.
- Script:
scripts/sanity_check.php - Usage: Run this script to perform sanity checks and ensure the application environment is correct.
TaskRunnerTask
- Purpose: Executes a custom task class from the command line.
- Command:
task:run - Usage: Run this command to execute any task that implements the
Taskinterface.
Options
--continuous: Run the task continuously in a loop--delay <secs>: The delay between continuous runs in seconds (default: 1)--max-runtime <secs>: The maximum runtime in seconds for continuous execution--attempts <num>: The maximum number of attempts if the task fails (default: 1, no retries)--backoff <secs>: The backoff delay in seconds between retries (default: 1). The actual delay is multiplied by the attempt number (exponential backoff)
Examples
When using retry logic, if a task throws an exception, it will be retried up to the specified number of attempts. The delay between retries follows an exponential backoff pattern: backoff * attempt_number. For example, with --backoff 2 and --attempts 3, the delays will be 2 seconds (after attempt 1), 4 seconds (after attempt 2), and then the final exception will be thrown if attempt 3 also fails.