Scheduling and Queues
Use Scheduled on the class to run detection periodically and Async on detect() to queue expensive detection work. Use health:detect --async to queue an immediate manual run.
Contents
Section titled “Contents”- Execution paths
- Schedule detection
- Write cron expressions
- Enable or disable scheduled detection
- Prevent overlapping work
- Run on multiple application servers
- Dispatch detection to a queue
- Async on detect only
- Choose a queue and connection
- Queue a manual run
- Unique detection jobs
- Configure a shared cache
- Operate the scheduler and workers
- Troubleshoot scheduled detection
Execution paths
Section titled “Execution paths”Detection reaches detect() through these paths:
Directapplication code ──────────────────────────► detect()
Manual command without Async, or with --synchealth:detect ─────────────────────────────► detect()
Manual command + Async or --asynchealth:detect ──► unique job ──► worker ────► detect()
Scheduledscheduler ──► overlap lock ──► detect()
Scheduled + Asyncscheduler ──► overlap lock ──► unique job ──► worker ──► detect()A direct call runs in the current process. Both scheduled paths are registered with Laravel’s scheduler and run on one server per occurrence when every scheduler host shares a lock-capable cache. The overlap lock belongs to that scheduler occurrence: without Async, the occurrence calls detect() directly; with Async, it dispatches one job and returns. A unique job prevents another job for the same finding from being dispatched while the first is queued or running, and the queue worker is what calls detect().
Schedule detection
Section titled “Schedule detection”A scheduled finding must be discoverable, implement CanDetect, define a static detect() method, and have a valid Scheduled cron expression.
Add Scheduled to the finding with a five-field cron expression:
use DataHealth\Attributes\Scheduled;use DataHealth\Contracts\CanDetect;use DataHealth\Finding;
#[Scheduled('*/5 * * * *')]class PaidOrderMarkedPending extends Finding implements CanDetect{ public static function detect(): null { // Detect and record findings.
return null; }}This finding becomes due every five minutes. Data Health registers it with Laravel’s scheduler when the application boots in the console, so you do not need to register each finding in routes/console.php or an application service provider.
Manual, scheduled, and queued detection all evaluate the result through DetectFinding: integers and null need no further action, returned callables are invoked through Laravel’s container, and Detector class names are resolved through the container before their detect() method runs.
See Detecting Problems for query, result, and idempotency guidance before automating a finding.
Write cron expressions
Section titled “Write cron expressions”The expression passed to Scheduled uses standard cron fields:
┌──────── minute (0–59)│ ┌────── hour (0–23)│ │ ┌──── day of month (1–31)│ │ │ ┌── month (1–12)│ │ │ │ ┌ day of week (0–7)│ │ │ │ │* * * * *| Expression | Runs |
|---|---|
* * * * * |
Every minute |
*/5 * * * * |
Every five minutes |
0 * * * * |
At the start of every hour |
0 2 * * * |
Daily at 02:00 |
0 3 * * 1 |
Mondays at 03:00 |
Data Health validates expressions while registering the schedule. An invalid expression throws an exception rather than silently creating a schedule that never runs.
Use Laravel’s configured application timezone when reasoning about due times, and inspect registered events with:
php artisan schedule:listEnable or disable scheduled detection
Section titled “Enable or disable scheduled detection”Scheduled detection is enabled by default. Disable all Data Health schedule registration with:
DATA_HEALTH_SCHEDULER_ENABLED=falseThe corresponding configuration value is data-health.scheduler.enabled. Disabling it does not prevent health:detect (with or without --async), direct calls to detect(), manual Filament detection, or custom jobs; it only prevents Data Health from registering scheduled events.
When Laravel configuration is cached, rebuild the cache after changing the environment value.
Prevent overlapping work
Section titled “Prevent overlapping work”Every Data Health scheduled event uses Laravel’s overlap protection. If one occurrence still holds its scheduler lock when the next becomes due, the new occurrence does not start.
This protects slow database scans and third-party checks whose runtime may exceed their interval. The lock is scoped to the finding class, so different findings may still run concurrently.
Detection should remain repeatable despite locking: processes can fail after partial work, locks eventually expire, and the same detector may also be triggered manually.
Run on multiple application servers
Section titled “Run on multiple application servers”Data Health configures each scheduled finding to run on one scheduler server per cron occurrence. Every application server may execute Laravel’s scheduler, but only the server that obtains the shared event lock runs the finding.
This works only when scheduler servers use the same compatible cache store. Local file or array caches cannot coordinate independent hosts.
Dispatch detection to a queue
Section titled “Dispatch detection to a queue”Without Async, detect() runs inside the scheduler process. Add Async to dispatch a queued detection job instead:
use DataHealth\Attributes\Async;use DataHealth\Attributes\Scheduled;
#[Scheduled('*/5 * * * *')]class PaidOrderMarkedPending extends Finding implements CanDetect{ #[Async] public static function detect(): null { // ...
return null; }}Use asynchronous detection when a check is slow, performs network requests, needs queue retries, or should not occupy the scheduler process.
Async on detect only
Section titled “Async on detect only”Apply #[Async] only to the finding’s detect() method. Package-managed manual
and scheduled detection honors it. The attribute does not intercept direct PHP
calls to MyFinding::detect(), which remain synchronous.
#[Async(queue: 'data-health', connection: 'redis')]public static function detect(): int|callable|string|null{ // Detect and report findings. return null;}Class-level attributes and placement on other methods are rejected during finding
discovery. Move existing class-level Async attributes directly onto detect().
Async does not schedule execution; keep Scheduled on the class when periodic
detection is wanted.
health:detect honors the method attribute by default. Use --sync to force
synchronous detection or --async to queue all selected findings; combining the
flags is an error. The --queue and --connection overrides require explicit
--async and take precedence independently over attribute routing. Omitted
attribute values use application queue defaults.
Verification and resolution always run synchronously through the manager, facade,
commands, and record browser. DataHealth::verify($record) and
DataHealth::resolve($record) return the actual boolean result immediately; their
commands have no async execution flags. AutoResolve also runs resolution
immediately. AutoVerify independently queues verification after commit with its
configured delay, active-record check, and application queue defaults. Detection’s
Async attribute does not configure automatic verification.
Choose a queue and connection
Section titled “Choose a queue and connection”Both Async arguments are optional. With no arguments, Laravel uses the application’s defaults:
#[Async]Choose a queue while keeping the default connection:
#[Async(queue: 'data-health')]Or choose both explicitly:
#[Async(queue: 'data-health', connection: 'redis')]Run a worker that consumes the selected destination:
php artisan queue:work redis --queue=data-healthQueue a manual run
Section titled “Queue a manual run”Queue all detectable findings immediately, without waiting for their schedules. Use -n to dispatch without an interactive selector:
php artisan health:detect --async -nThis includes findings without Scheduled or Async and works when data-health.scheduler.enabled is false. Routing uses Async on detect() or application queue defaults. Override either routing value for this run:
php artisan health:detect PaidOrderMarkedPending --async --queue=data-health --connection=redis -nphp artisan queue:work redis --queue=data-healthThe command uses the same per-finding unique-job lock as scheduled asynchronous detection. Pending or running jobs are reported as skipped. Its exit status covers dispatch failures; inspect worker logs and failed jobs for failures during background detection.
Without execution flags, the command honors Async on detect(); unannotated detection runs synchronously. Use --sync to force a synchronous run. The --sync and --async flags cannot be combined. Manual synchronous runs do not acquire the scheduler’s overlap lock or the queued-job uniqueness lock. See the Detection command for selectors, exclusions, and output, and Automation and Exit Codes for scripting and dispatch status.
Unique detection jobs
Section titled “Unique detection jobs”Queued Data Health detection jobs are unique by finding class. While one job for a finding is queued or running, another job for that same finding cannot be dispatched successfully.
This prevents a frequent schedule from building a backlog of identical full scans. Different finding classes have different unique IDs and may be queued independently.
Job uniqueness relies on a cache lock. It prevents duplicate queued jobs but does not make detection transactional or exactly-once: a failed job may already have recorded some findings, so detection must remain safe to retry.
Configure a shared cache
Section titled “Configure a shared cache”Laravel uses cache locks for overlap prevention, one-server scheduling, and unique queued jobs. Multi-server deployments must use a shared cache store that supports atomic locks, such as:
database;memcached;dynamodb; orredis.
Scheduler processes, manual dispatchers, and queue workers that must coordinate need the same central cache configuration. If each server uses an isolated cache, every server may believe it owns the lock.
Operate the scheduler and workers
Section titled “Operate the scheduler and workers”Data Health registers events with Laravel, but the application must still run Laravel’s scheduler.
In production, configure the usual system cron entry:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1For local development, run:
php artisan schedule:workWhen findings use Async, operate the corresponding queue workers as separately supervised processes. Deployments should restart long-running scheduler and worker processes so they load new finding classes and application code.
Useful operational signals include detection duration, findings reported, job failures, lock contention, queue latency, and the time of the last successful run.
Troubleshoot scheduled detection
Section titled “Troubleshoot scheduled detection”If a finding does not run, check these items in order:
- Discovery: Confirm its path and namespace follow Finding Discovery.
- Capability: Confirm the class implements
CanDetect. - Attribute: Confirm
Scheduledcontains a valid cron expression. - Configuration: Confirm
data-health.scheduler.enabledistruein the running process. - Registration: Run
php artisan schedule:listand look for the finding. - System scheduler: Confirm cron or
schedule:workis actually running. - Due time: Confirm the expression and application timezone match your expectation.
- Locks: Check whether a previous occurrence still owns an overlap lock; clear stale scheduler locks with Laravel’s scheduler cache-clear command when appropriate.
- Queue connection: For
Async, confirm the selected connection is notsyncwhen background work is expected. - Queue worker: Confirm a worker consumes the selected connection and queue, and inspect failed jobs.
- Shared cache: Confirm every server uses the same lock-capable store.
- Deployment: Restart long-running scheduler and worker processes after changing findings.
Continue with Metadata and Worklists to make automated findings easier to triage, or Finding Types and Detection to run detection manually.