user7175325
user7175325

Reputation:

How to prevent laravel scheduler from running before the cron job is finished

I have created a scheduler that runs a cron job. My code:

protected function schedule(Schedule $schedule)
{

    $user = User::first();

    $schedule->job((new SoapRequest)->checkStatusApplication($user))->everyMinute();
}

The task is to make soap calls to a web service to check for application status changes

This is the method checkStatusApplication in my SoapRequest class that performs soap call to check:

<?php

namespace App\ClientRequests;

use App\User;
use SoapClient;
use SoapFault;
use SoapParam;
use SoapVar;

class SoapRequest
{


    protected $password = config('application.password');
    protected $wsdl;
    protected $soapClient;

    public function __construct()
    {

        $this->soapClient = $this->makeSoapClient();
    }

    private function makeSoapClient()
    {

        return new SoapClient($this->wsdl, [
            'local_cert'   => base_path('storage/') 
            'passphrase'   => $this->password,
            'soap_version' => SOAP_1_1,
            'trace'        => 1,

        ]);
    }


    public static function checkStatusApplication(User $user)
    {

        $init = new self;

        $params = $init->makeSoapParams($init->makeXML($user));

        $response = $init->request('SubmitV2', [$params]);

        return $response;
    }

    private function request($functionName, $params)
    {

        try {

            return $this->soapClient->__soapCall($functionName, $params);

        } catch (SoapFault $e) {

            log_system('  fial: ', $this->soapClient->__getLastResponse());
            dd($e->getMessage());
        }
    }


}

INFO`: This job it's supposed to run in background.

MY POINT is: I want to grab all the users from database that status its not approved for the application and make soap calls in the background to the web service,

PROBLEM Let's say I have 200 users that I need to check for status changes.

QUESTION *What if from 200 users lets say my third request I get an exception or api server is down, what will happen with my other requests/scheduler or any idea how to handle that, and how to make sure that my scheduler will not run again until all my requests are done example timeout time from api.

NOTE I want to run scheduler every minute

Upvotes: 2

Views: 2728

Answers (1)

Mathew Tinsley
Mathew Tinsley

Reputation: 6976

You should be able to prevent your tasks from overlapping by using the withoutOverlapping method.

$schedule->job((new SoapRequest)->checkStatusApplication($user))->everyMinute()->withoutOverlapping();

https://laravel.com/docs/5.5/scheduling#preventing-task-overlaps

By default, scheduled tasks will be run even if the previous instance of the task is still running. To prevent this, you may use the withoutOverlapping method:

Upvotes: 3

Related Questions