Skip to content

Queues, workers and scheduler ​

ZATCA calls never run inside your web request (unless you choose B2C sync mode). They run on queue workers.

Queues ​

QueueUsed for
zatca-b2c-reportingReporting simplified invoices
zatca-b2c-retryRetries of B2C reporting
zatca-b2b-generationSigning standard invoices received with b2b()->submit()
zatca-b2b-clearanceClearing standard invoices
zatca-b2b-retryRetries of B2B clearance

Names are configurable in zatca.queues.names; the connection in zatca.queues.connection.

Workers ​

Simple setup (one worker process, all ZATCA queues):

bash
php artisan queue:work --queue=zatca-b2c-reporting,zatca-b2b-clearance,zatca-b2b-generation,zatca-b2c-retry,zatca-b2b-retry

Production setup with Supervisor:

ini
[program:zatca-worker]
command=php /var/www/app/artisan queue:work redis --queue=zatca-b2c-reporting,zatca-b2b-clearance,zatca-b2b-generation,zatca-b2c-retry,zatca-b2b-retry --sleep=1 --tries=1 --max-time=3600
numprocs=4
autostart=true
autorestart=true
stopwaitsecs=120

[program:zatca-relay]
command=php /var/www/app/artisan zatca:outbox:relay --daemon --max-time=3600
numprocs=1
autostart=true
autorestart=true
  • --tries=1: the package handles retries itself (with backoff and deadlines). Do not let Laravel retry ZATCA jobs.
  • The relay daemon is optional but recommended. Without it, messages whose immediate push failed are picked up by the scheduler within a minute.

Horizon ​

php
// config/horizon.php
'zatca' => [
    'connection' => 'redis',
    'queue' => ['zatca-b2c-reporting', 'zatca-b2b-clearance', 'zatca-b2b-generation', 'zatca-b2c-retry', 'zatca-b2b-retry'],
    'balance' => 'auto',
    'maxProcesses' => 8,
    'tries' => 1,
    'timeout' => 120,
],

Scheduler ​

These tasks are registered automatically (Laravel 9–13). Only the cron entry for schedule:run is needed.

CommandWhenPurpose
zatca:outbox:relayevery minutePush submissions whose immediate dispatch failed; delayed retries
zatca:deadlines:checkevery 10 minutesB2C reporting deadline alerts and breaches
zatca:reconcileevery 15 minutesRe-queue work whose queue message was lost
zatca:chain:verifydaily 02:45Verify every device's hash chain
zatca:devices:check-expirydaily 03:15Certificate expiry warnings
zatca:outbox:prunedaily 03:30Delete processed outbox rows

Disable with zatca.outbox.schedule = false (outbox, deadlines, reconcile, chain) or zatca.devices.schedule_expiry_check = false if you schedule them yourself.

Retries ​

Temporary failures (no connection, timeout, HTTP 429 or 5xx) are retried automatically with the same signed document:

WorkflowDefault schedule (seconds, randomised 50–100 %)Attempts
B2B60, 120, 300, 900, 1800, 3600, 7200, 14400, 28800, 4320010
B2C30, 60, 120, 300, 600, 1200, 1800, 3600, 7200, 1080010

For B2C the schedule is compressed so that attempts finish at least b2c_deadline_margin_hours (default 4) before the 24-hour deadline. After the last attempt the invoice becomes FAILED; an operator can retry it (see Corrections).

When ZATCA keeps failing, a circuit breaker pauses submissions for that environment (5 consecutive transport failures → 60 seconds), instead of hammering an unavailable service.

Testing locally without workers ​

With QUEUE_CONNECTION=sync, the package does not run ZATCA jobs inside your request. Run the relay to process them:

bash
php artisan zatca:outbox:relay

Not affiliated with or endorsed by ZATCA. Verify regulatory requirements with ZATCA and your tax advisor.