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

Bedds's avatar
Level 5

Multiple queue workers on 3rd party api wont stay logged in

Laravel: 5.7 Many things in the code have been changed to "thirdparty" for privacy.

I need to make several thousand api calls rate limited to a third party but don't want to have to call the login each time.

I register a singleton in the AppServiceProvider

$this->app->singleton('THIRDPARTY\API', function($app) {

            $container = [];
            $history = Middleware::history($container);
            $stack = HandlerStack::create();
            $stack->push($history);

            $client = new Client([
                'handler'   => $stack,
                'base_uri' => env('THIRDPARTY_URI'),
                'sink' => storage_path('logs/THIRDPARTY_sink.txt'),
                'connect_timeout' => false,
                'timeout'         => 300.0,
                'cookies'         => true,
                'defaults' => [
                    'exceptions' => false
                ],
                'debug' => FALSE,
                'headers' => [
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]
            ]);

            return $client;
        });

I queue a job to login and batch the individual api requests so I can rate limit them

$client = resolve('THIRDPARTY\API');

        $data = [
            'name' => env('THIRDPARTY_NAME'),
            'password' => env('THIRDPARTY_PASSWORD'),
            'company' => env('THIRDPARTY_COMPANY'),
        ]; 

        $endpoint = env('THIRDPARTY_ENDPOINT'); 

        $response = $client->post('/entity/auth/login/', [
            'debug' => FALSE,
            'form_params' => $data
        ]);

        ............

        foreach($thirdparties as $thirdparty) {
            \App\Jobs\UPDATETHIRDPARTY::dispatch($thirdparty)->onConnection('redis')->onQueue('thirdparty');
        }

This queues them up so I can rate limit them

Redis::throttle('thirdparty-syncs')->allow(100)->every(60)->then(function () {

            $client = resolve('THIRDPARTY\API');
            $response = $client->put('/entity/AHS/18.200.003/THIRTPARTYACCOUNTS', [
                'headers' => [
                    'content-type' => 'application/json'
                ],
                'body' => json_encode($this->dhc->toThirdPartyJson()),
                'debug' => FALSE
            ]);
        }, function () {

        });

Everything starts off working perfectly but then I will get entire batches of 100 that randomly fail saying that the api user is not logged in. However, batches right after will work fine without even calling login again. This seems to happen the most if I have multiple workers on the queue.

I am not sure if the issue is something to do with the API i'm using or my own setup.

0 likes
0 replies

Please or to participate in this conversation.