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 externally executed long-running tasks in Laravel (e.g., AWS Rekognition) by polling for status. Define tasks with a check() method returning ContinueChecking or StopChecking, store metadata, and run checks on a configurable interval until completion.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: Ideal for Laravel applications requiring asynchronous polling of external services (e.g., AWS Rekognition, payment processors, or batch APIs) where synchronous responses are impractical due to long execution times.
  • Event-Driven Pattern: Leverages Laravel’s queues and events to decouple task initiation from completion handling, reducing HTTP timeout risks and improving scalability.
  • State Management: Provides built-in task state tracking (e.g., pending, processing, completed, failed), enabling retry logic and progress monitoring.
  • Extensibility: Supports custom task classes, status polling logic, and result processing, making it adaptable to diverse external APIs.

Integration Feasibility

  • Laravel Native: Designed for Laravel (v9+), with minimal friction for adoption (e.g., spatie/laravel-package-tools integration, Artisan commands, and config publishing).
  • Queue System Agnostic: Works with database queues, Redis, or SQS, aligning with Laravel’s queue ecosystem.
  • Webhook Alternative: Fills a gap for services lacking webhook support, replacing manual polling with a structured, maintainable approach.
  • Testing Support: Includes mocking utilities for unit/integration tests, reducing onboarding complexity.

Technical Risk

  • Polling Overhead: Frequent API calls to external services may introduce cost (e.g., AWS API limits) or latency if not throttled. Requires careful configuration of pollInterval.
  • State Consistency: External service failures (e.g., API downtime) could leave tasks in ambiguous states. Mitigation: Implement idempotency and retry backoffs.
  • Queue Bloat: Unbounded task queues could strain resources. Mitigation: Use queue workers with supervision (e.g., Supervisor) and TTL for stale tasks.
  • Dependency on Laravel: Tight coupling to Laravel’s events, queues, and database may complicate multi-framework projects or microservices.

Key Questions

  1. External API Constraints:
    • What are the rate limits and costs of polling the external service? How does this package handle throttling?
    • Are there idempotency requirements for retries (e.g., duplicate task execution)?
  2. Task Lifecycle:
    • How will failed tasks be handled (e.g., alerts, dead-letter queues)?
    • What timeout thresholds should trigger task abandonment?
  3. Scaling:
    • How will the system handle spikes in task volume? Are there plans for horizontal scaling of workers?
  4. Monitoring:
    • How will task progress and failures be logged/monitored (e.g., Laravel Horizon, Prometheus)?
  5. Alternatives:
    • Could Laravel Jobs with delay() or Laravel Echo + Pusher (for webhook-based services) be a simpler fit?
    • Is there a need for distributed task coordination (e.g., Redis for shared state)?

Integration Approach

Stack Fit

  • Core Stack: Optimized for Laravel (v9/10) with PHP 8.1+. Compatible with:
    • Queues: Database, Redis, SQS, or any PSR-15-compliant queue.
    • Events: Laravel’s event system for task completion/failure notifications.
    • Database: MySQL, PostgreSQL, or SQLite (for task metadata storage).
  • External Dependencies:
    • HTTP Client: Uses Laravel’s Http client or Guzzle under the hood (configurable).
    • Logging: Integrates with Laravel’s log channels (e.g., Monolog, Stack).
  • Non-Fit: Avoid if using non-Laravel PHP or needing real-time updates (webhooks are preferable).

Migration Path

  1. Assessment Phase:
    • Audit existing long-running processes (e.g., cron jobs, manual polling scripts).
    • Identify candidate tasks for migration (e.g., AWS Rekognition, Stripe payments).
  2. Proof of Concept:
    • Implement a single task type (e.g., image processing) to validate polling logic and state management.
    • Test failure scenarios (e.g., API timeouts, rate limits).
  3. Incremental Rollout:
    • Replace synchronous polling with the package’s LongRunningTask class.
    • Migrate one service at a time, monitoring queue backlogs and performance.
  4. Queue Setup:
    • Configure queue workers (php artisan queue:work) with supervision (e.g., Supervisor).
    • Set up queue monitoring (e.g., Laravel Horizon for Redis).

Compatibility

  • Laravel Version: Tested on v9/10; may require adjustments for older versions.
  • PHP Version: Requires PHP 8.1+ (for named arguments, attributes).
  • Database: Schema migrations are provided; ensure compatibility with your DBMS.
  • Customization: Extend via:
    • Custom task classes (implement Spatie\LongRunningTasks\LongRunningTask).
    • Custom statuses (extend TaskStatus enum).
    • Custom polling logic (override poll() method).

Sequencing

  1. Prerequisites:
    • Laravel app with queues configured and database access.
    • External API credentials (e.g., AWS keys) stored securely (e.g., .env).
  2. Installation:
    composer require spatie/laravel-long-running-tasks
    php artisan vendor:publish --provider="Spatie\LongRunningTasks\LongRunningTasksServiceProvider"
    php artisan migrate
    
  3. Task Definition:
    • Create a task class (e.g., AnalyzeImageTask) extending LongRunningTask.
    • Implement poll() to check external service status.
  4. Dispatch Tasks:
    • Trigger tasks via dispatch() or manually (e.g., from a controller).
  5. Worker Setup:
    • Start queue workers: php artisan queue:work --sleep=3 --tries=3.
  6. Monitoring:
    • Add logging for task events (e.g., TaskCompleted, TaskFailed).
    • Set up alerts for failed tasks (e.g., Laravel Notifications).

Operational Impact

Maintenance

  • Package Updates:
    • Monitor releases for breaking changes (e.g., Laravel version drops).
    • Test updates in a staging environment before production deployment.
  • Custom Code:
    • Task classes and polling logic may require updates if external APIs change (e.g., new status fields).
  • Configuration:
    • Centralized in config/long-running-tasks.php (e.g., pollInterval, maxAttempts).

Support

  • Debugging:
    • Use php artisan queue:failed-table to inspect failed tasks.
    • Log task events for auditing (e.g., TaskStarted, PollingFailed).
  • Common Issues:
    • Stuck Tasks: Implement a TTL (e.g., created_at + 24h) to auto-clean stale tasks.
    • Rate Limiting: Add exponential backoff in poll() for API throttling.
    • State Inconsistency: Use database transactions for critical task updates.
  • Vendor Support:
    • Community-driven (MIT license); issues resolved via GitHub. Consider commercial support for critical systems.

Scaling

  • Horizontal Scaling:
    • Queue Workers: Scale workers horizontally (e.g., Kubernetes pods) for high task volume.
    • Database: Ensure task metadata table is optimized (e.g., indexes on status, task_id).
  • Performance:
    • Polling Interval: Adjust pollInterval based on API response times (e.g., 30s for slow services).
    • Batch Processing: For high-volume tasks, batch polling (e.g., 10 tasks per poll).
  • Resource Limits:
    • Monitor memory usage (long-running polls may consume resources).
    • Use queue throttling (e.g., queue:work --max-jobs=100) to prevent overload.

Failure Modes

Failure Scenario Impact Mitigation
External API downtime Tasks stuck in processing state Implement retry backoff and alerting for prolonged failures.
Queue worker crashes Tasks not processed Use Supervisor for process respawn; enable queue retries.
Database connection issues Task state lost Enable database queue failover (e.g., Redis as a fallback).
Rate limiting by external API Polling throttled Add exponential backoff in poll(); cache API responses.
Task timeout (e.g., 24h) Resource leaks Set TTL for tasks
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport