I work with jobs to send back webrequests (GET) to urls a user defined. To save load of my server I would like to disable the API access of user after 5 retrys (which all failed). Here's the full code of my job (excluding imports & some code parts that are unnecessary (disabling API access etc.).
use Exception;
class SendGetCallback implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public $address;
public $new_transaction;
//Address $address, Transaction $new_transaction
public function __construct ($address, $new_transaction)
{
$this->address = $address;
$this->new_transaction = $new_transaction;
}
public function handle()
{
Log::info("run: " . $this->attempts());
if ($this->attempts() <= 5) {
if ($this->attempts() > 1) {
Log::info("retry part");
//retry to send the callback
try{
Log::info("try part");
//prepare webrequest
if ($res->getStatusCode() != 200 || $res->getBody()->getContents() != "*ok*")
throw new Exception('callback url not reachable');
}
//if job failed again, release job after 300 seconds
catch(Exception $e){
Log::info("catch part");
$this->release(300);
}
}
//first run of job, just process normal
else{
//prepare webrequest
if ($res->getStatusCode() != 200 || $res->getBody()->getContents() != "*ok*")
throw new Exception('callback url not reachable');
}
}
//disable API access and inform user
else{
Log::info("disable access");
Mail::to($this->address->user->email)->send(new ApiAccessDisabled($this->address->user, $this->address->merchant_access));
$merchant_access = MerchantAccess::where('id', $this->address->merchant_access->id)->first();
$merchant_access->update([
'is_online' => false,
]);
}
}
public function failed(Exception $exception)
{
Log::info("failed called!");
}
}
For some reason the delay I use didn't work I think. The jobs just runs down without waiting.
Here's a part of the logfile:
[2017-01-01 18:25:02] local.INFO: run: 2
[2017-01-01 18:25:02] local.INFO: retry part
[2017-01-01 18:25:02] local.INFO: try part
[2017-01-01 18:25:03] local.INFO: catch part
[2017-01-01 18:25:14] local.INFO: run: 3
[2017-01-01 18:25:14] local.INFO: retry part
[2017-01-01 18:25:14] local.INFO: try part
[2017-01-01 18:25:14] local.INFO: catch part
[2017-01-01 18:25:24] local.INFO: run: 4
[2017-01-01 18:25:24] local.INFO: retry part
[2017-01-01 18:25:24] local.INFO: try part
[2017-01-01 18:25:24] local.INFO: catch part
[2017-01-01 18:25:35] local.INFO: run: 5
[2017-01-01 18:25:35] local.INFO: retry part
[2017-01-01 18:25:35] local.INFO: try part
[2017-01-01 18:25:35] local.INFO: catch part
[2017-01-01 18:25:45] local.INFO: run: 6
[2017-01-01 18:25:45] local.INFO: disable access
Any idea where things go wrong? Here's how the handle() method should work:
-->check if the run of the get request fails, for example when webserver of user is offline (throws exception) ->if yes set delay for job in 5 minutes
-->if job was runned for 5 times the api access should be disabled (working fine)