# jsdevart/laravel-managed-jobs architecture

> Architecture diagram of the GitHub repository jsdevart/laravel-managed-jobs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jsdevart/laravel-managed-jobs
- Repository: https://github.com/jsdevart/laravel-managed-jobs

## Overview

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: [`src/Jobs/BaseJob.php`](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Jobs/BaseJob.php) (BaseJob), [`src/Support/BroadcastChannelResolver.php`](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Support/BroadcastChannelResolver.php) (Channel Resolver), [`src/Models/ManagedJob.php`](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Models/ManagedJob.php) (ManagedJob), [`src/ManagedJobsServiceProvider.php`](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/ManagedJobsServiceProvider.php) (Service Provider)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_contracts["Public API"]
  node_node_jobowner["JobOwner<br/>contract<br/>[JobOwner.php]"]
  node_node_jobpayload["JobPayload<br/>contract<br/>[JobPayload.php]"]
end

subgraph group_group_runtime["Job Runtime"]
  node_node_jobrunner["JobRunner<br/>dispatcher<br/>[JobRunner.php]"]
  node_node_basejob["BaseJob<br/>job base<br/>[BaseJob.php]"]
  node_node_statusenum{{"JobStatus<br/>state enum<br/>[JobStatusEnum.php]"}}
end

subgraph group_group_persistence["Persistence"]
  node_node_managedjob[("ManagedJob<br/>model<br/>[ManagedJob.php]")]
  node_node_managedjobfile[("JobFile<br/>model<br/>[ManagedJobFile.php]")]
  node_node_migration_jobs["Jobs Table<br/>migration"]
  node_node_migration_files["Files Table<br/>migration"]
end

subgraph group_group_events["Lifecycle Events"]
  node_node_started(("JobStarted<br/>event<br/>[JobStarted.php]"))
  node_node_progress(("JobProgress<br/>event"))
  node_node_completed(("JobCompleted<br/>event<br/>[JobCompleted.php]"))
  node_node_failed(("JobFailed<br/>event<br/>[JobFailed.php]"))
  node_node_stopped(("JobStopped<br/>event<br/>[JobStopped.php]"))
end

subgraph group_group_support["Infrastructure"]
  node_node_channelresolver["Channel Resolver<br/>broadcast resolver"]
  node_node_expirecmd["Expire Files<br/>cleanup command"]
  node_node_provider["Service Provider<br/>package wiring"]
  node_node_config["Package Config<br/>configuration<br/>[managed-jobs.php]"]
end

node_node_jobrunner -->|"creates"| node_node_managedjob
node_node_jobrunner -->|"dispatches"| node_node_basejob
node_node_basejob -->|"updates"| node_node_managedjob
node_node_basejob -->|"uses"| node_node_statusenum
node_node_basejob -->|"accepts"| node_node_jobpayload
node_node_basejob -->|"tracks owner"| node_node_jobowner
node_node_basejob -->|"registers files"| node_node_managedjobfile
node_node_basejob -->|"emits"| node_node_started
node_node_basejob -->|"emits"| node_node_progress
node_node_basejob -->|"emits"| node_node_completed
node_node_basejob -->|"emits"| node_node_failed
node_node_basejob -->|"emits"| node_node_stopped
node_node_managedjob -->|"stores state"| node_node_statusenum
node_node_managedjob -->|"has many"| node_node_managedjobfile
node_node_migration_jobs -->|"backing table"| node_node_managedjob
node_node_migration_files -->|"backing table"| node_node_managedjobfile
node_node_channelresolver -->|"resolves channels from"| node_node_jobowner
node_node_channelresolver -->|"broadcasts"| node_node_progress
node_node_channelresolver -->|"broadcasts"| node_node_completed
node_node_channelresolver -->|"broadcasts"| node_node_failed
node_node_channelresolver -->|"broadcasts"| node_node_stopped
node_node_expirecmd -->|"cleans up"| node_node_managedjobfile
node_node_provider -->|"loads"| node_node_config
node_node_provider -->|"registers"| node_node_jobrunner
node_node_provider -->|"registers"| node_node_channelresolver
node_node_provider -->|"exposes"| node_node_expirecmd
node_node_config -->|"configures"| node_node_jobrunner
node_node_config -->|"configures"| node_node_channelresolver
node_node_config -->|"schedules"| node_node_expirecmd

click node_node_jobrunner "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Support/JobRunner.php"
click node_node_basejob "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Jobs/BaseJob.php"
click node_node_managedjob "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Models/ManagedJob.php"
click node_node_managedjobfile "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Models/ManagedJobFile.php"
click node_node_statusenum "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Enums/JobStatusEnum.php"
click node_node_jobowner "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Contracts/JobOwner.php"
click node_node_jobpayload "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Contracts/JobPayload.php"
click node_node_started "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobStarted.php"
click node_node_progress "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobProgressUpdated.php"
click node_node_completed "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobCompleted.php"
click node_node_failed "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobFailed.php"
click node_node_stopped "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobStopped.php"
click node_node_channelresolver "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Support/BroadcastChannelResolver.php"
click node_node_expirecmd "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Console/Commands/ExpireJobFilesCommand.php"
click node_node_provider "https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/ManagedJobsServiceProvider.php"
click node_node_config "https://github.com/jsdevart/laravel-managed-jobs/blob/master/config/managed-jobs.php"
click node_node_migration_jobs "https://github.com/jsdevart/laravel-managed-jobs/blob/master/database/migrations/2024_01_01_000001_create_managed_jobs_table.php"
click node_node_migration_files "https://github.com/jsdevart/laravel-managed-jobs/blob/master/database/migrations/2024_01_01_000002_create_managed_job_files_table.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_jobowner,node_node_jobpayload toneBlue
class node_node_jobrunner,node_node_basejob,node_node_statusenum toneAmber
class node_node_managedjob,node_node_managedjobfile,node_node_migration_jobs,node_node_migration_files toneMint
class node_node_started,node_node_progress,node_node_completed,node_node_failed,node_node_stopped toneRose
class node_node_channelresolver,node_node_expirecmd,node_node_provider,node_node_config toneIndigo
```

## Components

### Public API

Host-app facing contracts and abstractions

- [JobOwner](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Contracts/JobOwner.php): contract · Identifies the owning user or tenant · `src/Contracts/JobOwner.php`
- [JobPayload](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Contracts/JobPayload.php): contract · Marks the serialized input stored with the managed job · `src/Contracts/JobPayload.php`

### Job Runtime

Dispatch and execution orchestration

- [JobRunner](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Support/JobRunner.php): dispatcher · Creates managed job records and dispatches queued jobs · `src/Support/JobRunner.php`
- [BaseJob](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Jobs/BaseJob.php): job base · Executable base class that tracks progress, state, and files · `src/Jobs/BaseJob.php`
- [JobStatus](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Enums/JobStatusEnum.php): state enum · Pending, running, completed, failed, and stopped states · `src/Enums/JobStatusEnum.php`

### Persistence

Database-backed execution and file metadata

- [ManagedJob](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Models/ManagedJob.php): model · Primary execution record for lifecycle, payload, ownership, and progress · `src/Models/ManagedJob.php`
- [JobFile](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Models/ManagedJobFile.php): model · Metadata for downloadable job artifacts and expiration · `src/Models/ManagedJobFile.php`
- [Jobs Table](https://github.com/jsdevart/laravel-managed-jobs/blob/master/database/migrations/2024_01_01_000001_create_managed_jobs_table.php): migration · Creates the managed_jobs persistence schema · `database/migrations/2024_01_01_000001_create_managed_jobs_table.php`
- [Files Table](https://github.com/jsdevart/laravel-managed-jobs/blob/master/database/migrations/2024_01_01_000002_create_managed_job_files_table.php): migration · 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](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobStarted.php): event · Emitted when a managed job begins running · `src/Events/JobStarted.php`
- [JobProgress](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobProgressUpdated.php): event · Emitted when job progress changes · `src/Events/JobProgressUpdated.php`
- [JobCompleted](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobCompleted.php): event · Emitted when a job finishes successfully · `src/Events/JobCompleted.php`
- [JobFailed](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobFailed.php): event · Emitted when a job fails · `src/Events/JobFailed.php`
- [JobStopped](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Events/JobStopped.php): event · Emitted when a job is stopped · `src/Events/JobStopped.php`

### Infrastructure

Broadcasting, cleanup, and package wiring

- [Channel Resolver](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Support/BroadcastChannelResolver.php): broadcast resolver · Builds per-owner and optional per-tenant broadcast channels · `src/Support/BroadcastChannelResolver.php`
- [Expire Files](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/Console/Commands/ExpireJobFilesCommand.php): cleanup command · Deletes expired files and soft-deletes expired file records · `src/Console/Commands/ExpireJobFilesCommand.php`
- [Service Provider](https://github.com/jsdevart/laravel-managed-jobs/blob/master/src/ManagedJobsServiceProvider.php): package wiring · Registers package services, config, and Laravel integrations · `src/ManagedJobsServiceProvider.php`
- [Package Config](https://github.com/jsdevart/laravel-managed-jobs/blob/master/config/managed-jobs.php): configuration · Controls owner model, queue, storage, broadcasting, retention, and scheduling · `config/managed-jobs.php`

## Connections

- 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
