php - How to handle multiple api calls on a background job laravel


I have a background job firing api calls to google and facebook api's I know margin on failure for those api's its really slim, but due to the fact that my app depends on the data I get from those calls I want to make sure that my app doesnt get stuck when api call fails:

public function handle()
    try {

        $googleService = new GoogleClient;

        $data = $googleService->report()
            ->during('20170101', '20170126')
             Impressions, CampaignId, Clicks, Ctr,Cost, AverageCost, Interactions, Conversions, Status, Id')


        $client = new FacebookClient;

        $response = $client->insights()->select("impressions, ad_id, clicks,ctr, cpc,campaign_name ")->during('2013-01-01', '2018-01-31')->where(['accountid' => 'act_1222222'])->get();


    } catch (Exception $e) {



I have done research on how to handle jobs when an api call fails, non of them helped.

So my question is: What is the best way to handle the situation when an api call fails, In case one of them fails then I want to log the failure and keep the worker going ?




You can use Laravel Queues to accomplish such tasks.

Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.

From Laravel's official site.




If your api call fails but your application depends on that response, you should try to retry the call again and again, until you get a proper response, you can probably use a timeout between requests too so you don't get theToo Many Requests response.

If your api call fails and your application doesn't depend on the response you can just use a logger and log the error in your file and either retry the call, or just skip it and continue.

But I agree with @Amit Merchant you should go with Laravel Queues




Laravel has some interesting helper functions that you should look into. Of particular interest would be the retry function. Just as it implies, it retries the execution of anything in the callback until either A) no exception is thrown, or B) the maximum threshold it reached.

Simply wrap your api call and specify a limit before handling a failed state.

Example from the docs:

return retry(5, function () {
   // Attempt 5 times while resting 100ms in between attempts...
}, 100);

People are also looking for solutions to the problem: php - Ignore accented chars Eloquent query


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.