How jsdevart/laravel-managed-jobs works
1. This repository is a Laravel package, not a standalone application. Its center of gravity is “managed background jobs”: jobs are queued, tracked in the database, broadcast in real time, and can attach downloadable files.
2. The main public integration surface is small:
• JobRunner is the dispatch entry point.
• BaseJob is the class app jobs extend to participate in lifecycle tracking.
• JobOwner identifies the owning user/tenant.
• JobPayload marks the input object that gets serialized into the job record.
3. The core runtime flow is:
dispatch job → create ManagedJob row with payload/status metadata → queue the concrete job → worker picks it up → BaseJob middleware updates lifecycle state → job code calls progress/state helpers → completion/failure emits events and updates the row.
4. The persistent model boundary is two tables:
• managed_jobs stores the lifecycle record, payload, state checkpoint, progress, ownership, timestamps, and failure reason.
• managed_job_files stores metadata for files produced by a job, including expiration.
These are represented by ManagedJob and ManagedJobFile Eloquent models.
5. BaseJob and Support/JobRunner are the package’s main orchestration layer. BaseJob exposes the job-facing API described in the README: progress updates, stop checks, checkpoint state, file registration, and access to the current ManagedJob record. JobRunner is responsible for creating and dispatching the managed execution record plus the queued job.
6. Lifecycle is modeled as a finite state machine in JobStatusEnum:
pending → running → completed / failed / stopped.
The package fires Laravel events at each important transition: JobStarted, JobProgressUpdated, JobCompleted, JobStopped, and JobFailed. Those events all carry the ManagedJob record, making them the integration point for notifications, logging, webhooks, etc.
7. Real-time updates are handled through Laravel broadcasting. BroadcastChannelResolver determines the per-owner and optional per-tenant channel names. The package broadcasts job progress and terminal states to channels like jobs.{owner_user_id} and optionally jobs.{owner_tenant_id} depending on configuration and the owner contract implementation.
8. File handling is a separate subsystem layered on top of job execution. Jobs register generated files against the current execution; the file metadata is stored in managed_job_files, while the bytes live on the configured Laravel storage disk. This lets the app later list and download non-expired artifacts without embedding storage concerns into the job itself.
9. File expiration is maintained by Console/Commands/ExpireJobFilesCommand. The command deletes expired physical files and soft-deletes the corresponding database records, and the package config allows the command to be scheduled automatically. This creates a clear boundary between job execution time and cleanup time.
10. Configuration in config/managed-jobs.php controls the package’s infrastructure touchpoints:
• which user model owns jobs,
• optional table prefix,
• file retention days,
• broadcast channel type/prefix,
• queue connection/name overrides,
• storage disk,
• and scheduling behavior for expiration.
These settings determine how deeply the package integrates with the host Laravel app’s queue, storage, and broadcasting subsystems.
11. The repository’s structure reflects those boundaries cleanly:
• Contracts define host-app requirements.
• Jobs holds the base executable job abstraction.
• Models and database/migrations define persistence.
• Events defines lifecycle notifications.
• Support contains orchestration and channel resolution.
• Console contains cleanup automation.
• ManagedJobsServiceProvider wires the package into Laravel.
12. Architecturally, the package sits between the host application and Laravel infrastructure: app code creates payloads and jobs, the package manages persistence and lifecycle semantics, and Laravel’s queue/broadcast/storage/scheduler subsystems provide execution, realtime communication, artifact storage, and cleanup.
Main entry points: BaseJob.php BaseJob · BroadcastChannelResolver.php Channel Resolver · ManagedJob.php ManagedJob · ManagedJobsServiceProvider.php Service Provider
Components 18
Public API
Host-app facing contracts and abstractions
- JobOwner · Identifies the owning user or tenant
src/Contracts/JobOwner.php - JobPayload · Marks the serialized input stored with the managed job
src/Contracts/JobPayload.php
Job Runtime
Dispatch and execution orchestration
Persistence
Database-backed execution and file metadata
- ManagedJob · Primary execution record for lifecycle, payload, ownership, and progress
src/Models/ManagedJob.php - JobFile · Metadata for downloadable job artifacts and expiration
src/Models/ManagedJobFile.php - Jobs Table · Creates the managed_jobs persistence schema
database/migrations/2024_01_01_000001_create_managed_jobs_table.php - Files Table · Creates the managed_job_files persistence schema
database/migrations/2024_01_01_000002_create_managed_job_files_table.php
Lifecycle Events
State transition notifications
- JobStarted · Emitted when a managed job begins running
src/Events/JobStarted.php - JobProgress · Emitted when job progress changes
src/Events/JobProgressUpdated.php - JobCompleted · Emitted when a job finishes successfully
src/Events/JobCompleted.php - JobFailed · Emitted when a job fails
src/Events/JobFailed.php - JobStopped · Emitted when a job is stopped
src/Events/JobStopped.php
Infrastructure
Broadcasting, cleanup, and package wiring
- Channel Resolver · Builds per-owner and optional per-tenant broadcast channels
src/Support/BroadcastChannelResolver.php - Expire Files · Deletes expired files and soft-deletes expired file records
src/Console/Commands/ExpireJobFilesCommand.php - Service Provider · Registers package services, config, and Laravel integrations
src/ManagedJobsServiceProvider.php - Package Config · Controls owner model, queue, storage, broadcasting, retention, and scheduling
config/managed-jobs.php
Connections 29
- JobRunner → ManagedJob: creates · Persist a new managed execution row before dispatching work
- JobRunner → BaseJob: dispatches · Queues the concrete managed job implementation
- BaseJob → ManagedJob: updates · Reads and mutates the lifecycle record during execution
- BaseJob → JobStatus: uses · Maps execution progress onto lifecycle states
- BaseJob → JobPayload: accepts · Consumes serialized job input through the payload contract
- BaseJob → JobOwner: tracks owner · Resolves the owning user or tenant for the current job
- BaseJob → JobFile: registers files · Attaches generated artifacts to the current execution
- BaseJob → JobStarted: emits · Signals execution start
- BaseJob → JobProgress: emits · Signals progress updates
- BaseJob → JobCompleted: emits · Signals successful completion
- BaseJob → JobFailed: emits · Signals failure
- BaseJob → JobStopped: emits · Signals job stop events
- ManagedJob → JobStatus: stores state · Persists the finite state machine value
- ManagedJob → JobFile: has many · Owns downloadable artifact metadata
- Jobs Table → ManagedJob: backing table · Defines the managed_jobs schema
- Files Table → JobFile: backing table · Defines the managed_job_files schema
- Channel Resolver → JobOwner: resolves channels from · Derives broadcast destinations from the owner contract
- Channel Resolver → JobProgress: broadcasts · Routes progress updates over Laravel broadcasting
- Channel Resolver → JobCompleted: broadcasts · Routes terminal success updates over Laravel broadcasting
- Channel Resolver → JobFailed: broadcasts · Routes terminal failure updates over Laravel broadcasting
- Channel Resolver → JobStopped: broadcasts · Routes stopped updates over Laravel broadcasting
- Expire Files → JobFile: cleans up · Removes expired file records and physical files
- Service Provider → Package Config: loads · Wires package behavior from configuration
- Service Provider → JobRunner: registers · Makes the dispatcher available to the host app
- Service Provider → Channel Resolver: registers · Binds broadcasting support into Laravel
- Service Provider → Expire Files: exposes · Makes cleanup command available for scheduling
- Package Config → JobRunner: configures · Controls queue, storage, and retention behavior
- Package Config → Channel Resolver: configures · Controls broadcast channel type and prefix
- Package Config → Expire Files: schedules · Enables automated file-expiration cleanup