Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Long Running Tasks Laravel Package

spatie/laravel-long-running-tasks

Monitor and manage external long-running tasks (e.g., AWS Rekognition jobs) in Laravel by polling for status. Define a task with a check() method, store metadata, and keep checking at a configurable interval until completion.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Polling-Based External Task Monitoring: The package excels at monitoring asynchronous, externally triggered tasks (e.g., AWS Rekognition, payment processors, or batch APIs) where webhooks are unavailable or unreliable. This aligns well with Laravel’s event-driven architecture but shifts responsibility to the application for polling state changes.
  • Queue-Driven Execution: Leverages Laravel’s queue system (e.g., Redis, database, SQS), ensuring scalability and decoupling from HTTP requests. The package abstracts retry logic, backoff strategies, and timeouts, reducing boilerplate.
  • Stateful Task Tracking: Persists task metadata (status, exceptions, run count) in a database, enabling observability, debugging, and manual intervention (e.g., restarting failed tasks).
  • Extensibility: Supports custom models, jobs, and check strategies, making it adaptable to niche use cases (e.g., domain-specific task lifecycles or custom failure handling).

Integration Feasibility

  • Laravel Native: Designed for Laravel 10.x, with minimal friction for adoption (composer install, migrations, config publishing). Assumes queues are already configured (a common Laravel requirement).
  • External Service Agnostic: Works with any external API that requires polling (e.g., Stripe, Twilio, or custom microservices). The check() method is a blank slate for API interactions.
  • Database Dependency: Requires a long_running_task_log_items table, adding a schema migration. Low risk if the team follows Laravel’s migration practices.
  • Queue Worker Requirement: Tasks only execute when queue workers are running. Misconfiguration (e.g., no workers) could lead to silent task failures.

Technical Risk

  • Polling Overhead: Frequent checks (default: 10-second intervals) may strain external APIs or internal resources if tasks are numerous. Mitigated by backoff strategies (e.g., StandardBackoffCheckStrategy).
  • State Management: Tasks must be idempotent—repeated check() calls with the same meta data could cause unintended side effects (e.g., duplicate operations). The package doesn’t enforce this; it’s the developer’s responsibility.
  • Timeout Handling: Tasks marked didNotComplete after keep_checking_for_in_seconds are abandoned. No built-in alerting or escalation (e.g., Slack/email). Requires custom logic (e.g., listening to TaskFailed events).
  • Error Recovery: Exceptions in check() are caught and logged but don’t automatically trigger retries unless onFail() returns TaskResult::ContinueChecking. May need additional logic for transient failures (e.g., network blips).
  • Concurrency Limits: No built-in throttling for parallel task execution. High-volume systems may need queue throttling (e.g., queue:work --limit=1) or database-level locking.

Key Questions

  1. Use Case Alignment:
    • Are tasks truly long-running (minutes/hours) or could they be handled via synchronous HTTP requests or Laravel jobs?
    • Do external services require polling (no webhooks) or support callbacks?
  2. Scalability Needs:
    • What’s the expected volume of concurrent tasks? Will queue workers need scaling (e.g., Kubernetes HPA, SQS scaling)?
    • Are there rate limits on external APIs that could be hit by aggressive polling?
  3. Observability:
    • How will task statuses be monitored? (e.g., Laravel Horizon, custom dashboard, or third-party tools like Datadog?)
    • Are there SLA requirements for task completion? (e.g., "99% of tasks must complete within 1 hour.")
  4. Failure Handling:
    • What’s the recovery strategy for failed tasks? (e.g., manual retries, dead-letter queues, or automated escalation?)
    • Should tasks fail fast (e.g., after 3 retries) or persist indefinitely (e.g., for critical background jobs)?
  5. Customization:
    • Will custom models/jobs be needed to extend functionality (e.g., adding task priority, soft deletes, or audit logs)?
    • Are there security concerns with storing task metadata (e.g., PII in meta arrays)?

Integration Approach

Stack Fit

  • Laravel Core: Seamlessly integrates with Laravel’s queue system, Eloquent models, and event system. No external dependencies beyond Laravel’s ecosystem.
  • Queue Backends: Compatible with all Laravel-supported queue drivers (database, Redis, Amazon SQS, etc.). Performance may vary (e.g., Redis is faster for high-throughput scenarios).
  • Database: Uses Eloquent for task logging, so any database supported by Laravel (MySQL, PostgreSQL, SQLite) works. Consider indexing status and stop_checking_at for large-scale deployments.
  • Event System: Emits events (TaskStarted, TaskCompleted, TaskFailed) that can be listened to for custom logic (e.g., notifications, analytics).

Migration Path

  1. Prerequisites:
    • Ensure queues are configured (QUEUE_CONNECTION in .env) and workers are running (php artisan queue:work).
    • Verify database permissions for the new long_running_task_log_items table.
  2. Installation:
    composer require spatie/laravel-long-running-tasks
    php artisan vendor:publish --tag="long-running-tasks-migrations"
    php artisan vendor:publish --tag="long-running-tasks-config"
    php artisan migrate
    
  3. Configuration:
    • Adjust config/long-running-tasks.php for:
      • Queue name (e.g., high-priority for critical tasks).
      • Check frequency (e.g., 30 seconds for API rate limits).
      • Backoff strategy (e.g., StandardBackoffCheckStrategy for exponential retries).
  4. Implementation:
    • Create task classes extending Spatie\LongRunningTasks\LongRunningTask.
    • Example:
      class ProcessPaymentTask extends LongRunningTask {
          public function check(LongRunningTaskLogItem $logItem): TaskResult {
              $paymentId = $logItem->meta['payment_id'];
              $status = PaymentService::checkStatus($paymentId);
              return $status === 'completed' ? TaskResult::StopChecking : TaskResult::ContinueChecking;
          }
      }
      
    • Start tasks via:
      ProcessPaymentTask::make()->meta(['payment_id' => 123])->start();
      
  5. Testing:
    • Mock external API calls in check() to test all status transitions (pending → running → completed/failed).
    • Verify queue jobs execute correctly using php artisan queue:work --once.

Compatibility

  • Laravel Version: Tested for Laravel 10.x. May require adjustments for older versions (e.g., queue job syntax).
  • PHP Version: Requires PHP 8.1+. Check compatibility with your runtime (e.g., Laravel Sail, Forge, or custom server).
  • Queue Workers: Must run continuously to process tasks. Consider:
    • Supervisor for Linux (/etc/supervisor/conf.d/laravel-worker.conf).
    • PM2 for Node.js-based Laravel setups.
    • Kubernetes CronJobs for cloud-native deployments.

Sequencing

  1. Phase 1: Pilot Task Type
    • Start with a non-critical task type (e.g., image processing, report generation) to validate the integration.
    • Monitor queue performance and task completion rates.
  2. Phase 2: Critical Tasks
    • Gradually introduce tasks tied to user-facing flows (e.g., payments, notifications).
    • Implement observability (e.g., Horizon dashboard, custom metrics).
  3. Phase 3: Customization
    • Extend the package with custom models/jobs if needed (e.g., adding task priority or soft deletes).
    • Integrate with existing alerting (e.g., Slack for TaskFailed events).
  4. Phase 4: Optimization
    • Tune check frequencies and backoff strategies based on real-world data.
    • Scale queue workers horizontally if needed.

Operational Impact

Maintenance

  • Package Updates: Spatie packages are well-maintained (MIT license, active GitHub). Monitor for Laravel version compatibility.
  • Database Schema: Migrations are idempotent, but schema changes (e.g., new columns) may require custom migrations if extending the model.
  • Configuration Drift: Centralized config (long-running-tasks.php) reduces risk of misconfiguration. Use environment variables for sensitive values (e.g., queue connections).
  • Dependency Management: No hard dependencies on external services; only Laravel core and queue drivers.

Support

  • Debugging:
    • Query the long_running_task_log_items table for stuck tasks:
      SELECT * FROM long_running_task_log_items WHERE status = 'running' AND last_check_started_at < NOW() - INTERVAL 1 HOUR;
      
    • Use onFail() to log exceptions to a monitoring system (e.g., Sentry, Laravel Log).
  • Common Issues:
    • Stuck Tasks: Tasks may hang if check() enters an infinite loop. Add timeouts or circuit break
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai