fanat98/laravel-task-orchestrator
Installation:
composer require fanat98/laravel-task-orchestrator
php artisan vendor:publish --provider="Fanat98\TaskOrchestrator\TaskOrchestratorServiceProvider" --tag="migrations"
php artisan migrate
Basic Task Definition:
Define a task in a service class (e.g., app/Services/ExampleTask.php):
namespace App\Services;
use Fanat98\TaskOrchestrator\Contracts\Task;
class ExampleTask implements Task
{
public function execute(): void
{
// Your task logic here
\Log::info('Task executed');
}
}
Register Task in Orchestrator:
In a service provider (e.g., AppServiceProvider):
public function boot()
{
\Fanat98\TaskOrchestrator\Facades\TaskOrchestrator::registerTask(
'example_task',
\App\Services\ExampleTask::class
);
}
Run Task via CLI:
php artisan task:run example_task
Dashboard Access:
Visit /task-orchestrator to monitor tasks in real-time.
Define a Scheduled Task:
use Illuminate\Support\Facades\Schedule;
Schedule::call(function () {
\Fanat98\TaskOrchestrator\Facades\TaskOrchestrator::run('example_task');
})->everyMinute();
Verify in Dashboard: Check the dashboard for execution logs, status, and duration.
Define dependencies between tasks to enforce execution order:
TaskOrchestrator::registerTask('task_a', TaskA::class)
->dependsOn(['task_b', 'task_c']);
Webhook Integration: Extend the dashboard with custom webhooks for notifications:
TaskOrchestrator::onTaskCompleted(function ($task) {
// Send Slack/email notification
});
Event Listeners: Listen to task events for custom logic:
TaskOrchestrator::listen('task.started', function ($task) {
\Log::debug("Task {$task->name} started");
});
Cron Jobs: Use Laravel’s scheduler to trigger orchestrated tasks:
Schedule::command('task:run workflow_name')->daily();
Dynamic Scheduling: Schedule tasks programmatically:
TaskOrchestrator::schedule('task_a', now()->addMinutes(5));
Configure retry logic for tasks:
TaskOrchestrator::registerTask('faulty_task', FaultyTask::class)
->retries(3)
->fallback(function ($task) {
\Log::error("Task {$task->name} failed after retries");
});
Task Registration Timing:
booted events.AppServiceProvider@boot or a dedicated provider.Dependency Loops:
task_a depends on task_b, which depends on task_a) will cause silent failures.Dashboard Permissions:
Route::middleware(['auth'])->group(function () {
TaskOrchestrator::routes();
});
Long-Running Tasks:
max_execution_time may appear stuck.TaskOrchestrator::queueTask()) or increase max_execution_time in php.ini.Log Task Execution: Enable debug mode and check Laravel logs for task lifecycle events:
TaskOrchestrator::enableDebugLogging();
Inspect Task State: Use Tinker to check task status:
php artisan tinker
>>> \Fanat98\TaskOrchestrator\Models\Task::first()->status
Clear Stuck Tasks: Manually update task status in the database if needed:
UPDATE tasks SET status = 'pending' WHERE name = 'stuck_task';
Custom Task Models:
Extend the Task model to add fields (e.g., priority, metadata):
php artisan make:model TaskExtension -m
Then update the migration and model.
Add Task Metrics:
Track custom metrics (e.g., CPU usage) by extending the Task class:
class CustomTask extends \Fanat98\TaskOrchestrator\Models\Task
{
protected $casts = [
'cpu_usage' => 'integer',
];
}
Integrate with Queues: Offload tasks to queues for async execution:
TaskOrchestrator::queueTask('long_running_task', now()->addHour());
Localization:
Override dashboard translations in resources/lang/{locale}/task-orchestrator.php:
return [
'status' => [
'pending' => 'In queue',
],
];
How can I help you explore Laravel packages today?