Fixes for webhook logic

This commit is contained in:
David Bomba 2023-11-10 12:06:49 +11:00
parent fdf3f252b1
commit 3ce689a53e
4 changed files with 35 additions and 8 deletions

View File

@ -64,7 +64,7 @@ class InvoiceCheckLateWebhook implements ShouldQueue
->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()])
->cursor()
->each(function ($invoice) {
WebhookHandler::dispatch(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2));
(new WebhookHandler(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company, 'client'))->handle();
});
} else {
foreach (MultiDB::$dbs as $db) {
@ -91,7 +91,7 @@ class InvoiceCheckLateWebhook implements ShouldQueue
->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()])
->cursor()
->each(function ($invoice) {
WebhookHandler::dispatch(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2));
(new WebhookHandler(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company, 'client'))->handle();
});
}
}

View File

@ -58,7 +58,7 @@ class WebhookHandler implements ShouldQueue
->where('event_id', $this->event_id)
->cursor()
->each(function ($subscription) {
WebhookSingle::dispatch($subscription->id, $this->entity, $this->company->db, $this->includes);
(new WebhookSingle($subscription->id, $this->entity, $this->company->db, $this->includes))->handle();
});
}

View File

@ -63,9 +63,7 @@ class WebhookSingle implements ShouldQueue
public function backoff()
{
// return [15, 35, 65, 185, 3605];
return [rand(10, 15), rand(30, 40), rand(60, 79), rand(160, 200), rand(3000, 5000)];
}
/**
@ -150,8 +148,28 @@ class WebhookSingle implements ShouldQueue
))->handle();
} catch (BadResponseException $e) {
if ($e->getResponse()->getStatusCode() >= 400 && $e->getResponse()->getStatusCode() < 500) {
/* Some 400's should never be repeated */
if (in_array($e->getResponse()->getStatusCode(), [404, 410, 405])) {
$message = "There was a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " This webhook call will be suspended until further action is taken.";
(new SystemLogger(
['message' => $message],
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
))->handle();
$subscription->delete();
$this->fail();
return;
}
$message = "There was a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode();
nlog($message);
(new SystemLogger(
@ -163,11 +181,11 @@ class WebhookSingle implements ShouldQueue
$this->company
))->handle();
/* Some 400's should never be repeated */
if (in_array($e->getResponse()->getStatusCode(), [404, 410])) {
if (in_array($e->getResponse()->getStatusCode(), [400])) {
$this->fail();
return;
}
$this->release($this->backoff()[$this->attempts()-1]);
}

File diff suppressed because one or more lines are too long