Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lordwhizy's avatar

How to check If the current app process is running within a queue environment

Hello,

I applied a global scope to my models via a trait, but I don't want the global scope applied when the model is called/processed from my Redis queue.

How do I detect if the current instance is a queue process? just like we have this

if (App::environment('local')) {
    // The environment is local
}

to detect if the app is running in local or production.

0 likes
3 replies
jivanrij's avatar

I also needed this, but with the distinction between running in artisan and the queue worker. After some digging, I found 2 solutions.

Solution one

I found out that Laravel (9) only registers \Illuminate\Queue\CallQueuedHandler in the service container when running a queue process.

So you can use this function to know if you are in the queue:

    /**
     * Returns true when the current process runs in a queued process
     * 
     * @return bool
     */
    public function inQueue(): bool
    {
        return App::resolved(\Illuminate\Queue\CallQueuedHandler::class);
    }

Solution two

When Laravel starts a Queue Job there is an event with a callback you can use to attach some currently-running-in-queue flag in the service container. https://laravel.com/docs/9.x/queues#job-events

// Within the boot method of a service provider
App::bind('processing-job', function ($app) {
    return false;
});
Queue::before(function (JobProcessing $event) {
    App::bind('processing-job', function ($app) {
        return true;
    });
});

Then you can run a check anywhere like this:

App::get('processing-job');
3 likes
jivanrij's avatar

I should mention this thing I discovered when testing:

When running tests, and env var QUEUE_CONNECTION is set to sync the test will think you are in a queued process. This is because when working in sync both your test and the job live within the same instance.

Please or to participate in this conversation.