Romain
2 months ago
320
3
Laravel

Job sent to queue behave differently with Eloquent

Posted 2 months ago by Romain

Hi,

I have a very simple job that searches the active users in DB and triggers an action. When I don't queue the job the Eloquent query is respected and only the active users are fetched and triggers, however when I send the job to the queue (by adding ShouldQueue) then all users are triggered. I am using Horizon to process the queue and I can clearly see user tags being passed when they are set to inactive.

Could you think of any reason why that would be?


class TrackUsers implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // This loads all users instead of just the active ones.
        // If I remove implements ShouldQueue, then it loads only the active users
        $users = User::where('active', true)->get();

        // Scrape data
        $users->each(function($user) {
            TrackSingleUser::dispatch($user);
        });
    }
}

Please sign in or create an account to participate in this conversation.