Skip to content

marko/queue-sync

Synchronous queue driver --- executes jobs immediately during the current request, ideal for development and testing. The sync driver runs jobs inline when they are pushed, with no external dependencies or background processes. Delayed jobs execute immediately. Failed jobs throw JobFailedException so errors surface instantly during development. Use marko/queue-database, marko/queue-redis or marko/queue-rabbitmq for production workloads.

Implements QueueInterface from marko/queue.

Terminal window
composer require marko/queue-sync

Bind SyncQueue as the QueueInterface implementation in your module:

module.php
use Marko\Queue\QueueInterface;
use Marko\Queue\Sync\SyncQueue;
return [
'bindings' => [
QueueInterface::class => SyncQueue::class,
],
];

Then dispatch jobs normally --- they execute synchronously:

use Marko\Queue\QueueInterface;
public function __construct(
private readonly QueueInterface $queue,
) {}
public function process(): void
{
// Executes immediately, throws on failure
$this->queue->push(new SendWelcomeEmail('user@example.com'));
}

SyncQueue is constructed with the JobRunnerInterface from marko/queue, which the container autowires. It runs each job through that runner, exactly as the worker does: the attempt is counted, a job that implements ContainerAwareJobInterface gets setContainer() and setJobEnvelope() before it runs and releaseContainer() once handle() returns or throws, and an exception is counted on the job before JobFailedException wraps it. JobProcessingEvent and JobProcessedEvent are dispatched around handle() as they are under the worker.

A job that throws has no further attempts, so SyncQueue hands it to the failed-job handler before throwing JobFailedException: the job’s failed() hook runs (if it implements FailableJobInterface) and JobFailedEvent is dispatched with the queue passed to push(), or queue.queue when none was given. This is how #[Observer(async: true)] observers run under the sync driver. The observer is queued as an AsyncObserverJob, and push() runs it straight away against a copy of the event unwrapped from the signed envelope. If the observer throws, dispatch() throws a JobFailedException.

The sync driver includes NullFailedJobRepository since jobs either succeed or throw immediately. The failed-job handler still calls failed() and dispatches JobFailedEvent; only the storing step does nothing:

module.php
use Marko\Queue\FailedJobRepositoryInterface;
use Marko\Queue\Sync\NullFailedJobRepository;
return [
'bindings' => [
FailedJobRepositoryInterface::class => NullFailedJobRepository::class,
],
];

Implements all methods from QueueInterface. See marko/queue for the full contract.

MethodDescription
__construct(JobRunnerInterface $jobRunner, FailedJobHandlerInterface $failedJobHandler, QueueConfig $config)Autowired. Every job runs through the runner, and a job that throws goes through the failed-job handler.
push(JobInterface $job, ?string $queue = null): PushResultExecute the job immediately through the JobRunner and return a pushed PushResult with its ID. Container-aware jobs get the container and job envelope first, and release them afterwards. On failure, hands the job to the failed-job handler, then throws JobFailedException, or the handler’s FailedJobNotStoredException when a bound repository can’t store the job.
later(int $delay, JobInterface $job, ?string $queue = null): PushResultIgnores the delay and executes immediately via push(). The sync driver stores nothing and runs every job at push time, so there is nothing to delay. This is the one documented exception to “throw when you can’t do it”: a development driver whose purpose is inline execution.
pop(?string $queue = null, bool $block = false): ?JobInterfaceAlways returns null --- no jobs are ever queued.
size(?string $queue = null): intAlways returns 0.
clear(?string $queue = null): intAlways returns 0.
delete(string $jobId): boolAlways returns false --- the sync queue never stores a job, so nothing is deleted.
release(JobInterface $job, int $delay = 0): boolAlways returns false --- the sync queue never reserves a job, and QueueInterface::release() returns false when no reserved job matches.

module.php binds QueueInterface straight to SyncQueue; there is no factory.

A no-op implementation of FailedJobRepositoryInterface --- since the sync driver throws on failure, there are no failed jobs to store.

MethodDescription
store(FailedJob $failedJob): voidNo-op.
all(): arrayAlways returns [].
find(string $id): ?FailedJobAlways returns null.
delete(string $id): boolAlways returns false.
clear(): intAlways returns 0.
count(): intAlways returns 0.