Installation:
composer require milestone/task
Add the service provider to config/app.php:
'providers' => [
// ...
Milestone\Task\TaskServiceProvider::class,
],
Publish Config:
php artisan vendor:publish --provider="Milestone\Task\TaskServiceProvider" --tag="config"
Review config/task.php for default settings (e.g., default_driver, queue_connection).
First Use Case: Define a task in a controller or command:
use Milestone\Task\Task;
public function createTask()
{
$task = Task::create([
'name' => 'Process user uploads',
'payload' => ['user_id' => 123],
'timeout' => 3600, // 1 hour
]);
return $task->id;
}
Run the Task:
php artisan task:run
(Check config/task.php for command settings.)
Task Creation:
Task::create() or Task::dispatch() (if queue-based).payload (serialized to JSON):
$task = Task::create([
'name' => 'Send email',
'payload' => ['template' => 'welcome', 'user_id' => 42],
'delay' => 60, // Run after 60 seconds
]);
Queue Integration:
queue_connection in config/task.php (e.g., database, redis).TaskListener facade:
use Milestone\Task\TaskListener;
TaskListener::listen(function ($task) {
// Handle task logic here
return ['status' => 'completed'];
});
Retry Logic:
max_attempts and backoff (exponential) in task config:
$task = Task::create([
'name' => 'Retryable job',
'payload' => [...],
'max_attempts' => 3,
'backoff' => 1000, // 1 second initial delay
]);
Task Chaining:
Task::chain() to link tasks sequentially:
$task1 = Task::create(['name' => 'Step 1']);
$task2 = Task::create(['name' => 'Step 2']);
Task::chain($task1->id, $task2->id);
Laravel Events:
Bind to task.created, task.failed, or task.completed:
event(new TaskCreated($task));
Database Schema:
Extend the tasks table via migrations (e.g., add priority column):
Schema::table('tasks', function (Blueprint $table) {
$table->integer('priority')->default(5);
});
Testing:
Mock the TaskListener in PHPUnit:
$this->mock(TaskListener::class)->shouldReceive('listen')->once();
Queue Stuck Tasks:
php artisan task:run hangs, check for deadlocks in the queue table.failed_jobs table and restart the queue worker.Payload Serialization:
payload will fail silently.json_encode()-compatible data or implement __serialize().Missing Config:
TaskServiceProvider errors.php artisan vendor:publish --tag=config after install.Timeouts:
timeout set to 0 will run indefinitely.3600 for 1 hour).Log Tasks:
Enable debug in config/task.php to log task lifecycle events to storage/logs/task.log.
Inspect Queue:
Use php artisan task:list to view pending tasks and their status.
Failed Tasks:
Check the failed_jobs table for errors. Reprocess with:
php artisan task:retry <task_id>
Custom Drivers:
Override the default queue driver by implementing Milestone\Task\Contracts\TaskDriver.
Task Events:
Extend core events (e.g., TaskFailed) in app/Providers/EventServiceProvider:
protected $listen = [
'Milestone\Task\Events\TaskFailed' => [
'App\Listeners\LogFailedTask',
],
];
Middleware:
Add middleware to tasks via the middleware config array:
'middleware' => [
\App\Http\Middleware\Authenticate::class,
],
API Endpoints:
Expose task management via routes (e.g., /tasks/{id}/retry):
Route::post('/tasks/{id}/retry', [TaskController::class, 'retry']);
How can I help you explore Laravel packages today?