Refactors for webhooks

This commit is contained in:
David Bomba 2023-02-17 14:47:52 +11:00
parent 7f1a984848
commit 8ae40d7097
3 changed files with 25 additions and 20 deletions

View File

@ -48,4 +48,5 @@ class InvoiceWasPaid
$this->company = $company; $this->company = $company;
$this->event_vars = $event_vars; $this->event_vars = $event_vars;
} }
} }

View File

@ -46,6 +46,7 @@ class WebhookSingle implements ShouldQueue
private string $includes; private string $includes;
private Company $company; private Company $company;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -121,41 +122,47 @@ class WebhookSingle implements ShouldQueue
RequestOptions::JSON => $data, // or 'json' => [...] RequestOptions::JSON => $data, // or 'json' => [...]
]); ]);
SystemLogger::dispatch( (new SystemLogger(
array_merge((array) $response, $data), array_merge((array) $response, $data),
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_SUCCESS, SystemLog::EVENT_WEBHOOK_SUCCESS,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company $this->company
); ))->handle();
} catch(\GuzzleHttp\Exception\ConnectException $e) { } catch(\GuzzleHttp\Exception\ConnectException $e) {
nlog("connection problem"); nlog("connection problem");
nlog($e->getCode()); nlog($e->getCode());
nlog($e->getMessage()); nlog($e->getMessage());
SystemLogger::dispatch( (new SystemLogger(
['message' => "Error connecting to ". $subscription->target_url], ['message' => "Error connecting to ". $subscription->target_url],
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE, SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company $this->company
); ))->handle();
} catch (BadResponseException $e) { } catch (BadResponseException $e) {
if ($e->getResponse()->getStatusCode() >= 400 && $e->getResponse()->getStatusCode() < 500) { if ($e->getResponse()->getStatusCode() >= 400 && $e->getResponse()->getStatusCode() < 500) {
$message = "Server encountered a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " scheduling retry."; $message = "There was a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode();
nlog($message); nlog($message);
SystemLogger::dispatch( (new SystemLogger(
['message' => $message], ['message' => $message],
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE, SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company $this->company
); ))->handle();
/* Some 400's should never be repeated */
if(in_array($e->getResponse()->getStatusCode(), [404, 410])){
$this->fail();
return;
}
$this->release($this->backoff()[$this->attempts()-1]); $this->release($this->backoff()[$this->attempts()-1]);
} }
@ -163,16 +170,16 @@ class WebhookSingle implements ShouldQueue
if ($e->getResponse()->getStatusCode() >= 500) { if ($e->getResponse()->getStatusCode() >= 500) {
nlog("endpoint returned a 500, failing"); nlog("endpoint returned a 500, failing");
$message = "Server encountered a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " no retry attempted."; $message = "The was a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " no retry attempted.";
SystemLogger::dispatch( (new SystemLogger(
['message' => $message], ['message' => $message],
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE, SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company $this->company
); ))->handle();
$this->fail(); $this->fail();
return; return;
@ -181,38 +188,38 @@ class WebhookSingle implements ShouldQueue
nlog("Server exception"); nlog("Server exception");
$error = json_decode($e->getResponse()->getBody()->getContents()); $error = json_decode($e->getResponse()->getBody()->getContents());
SystemLogger::dispatch( (new SystemLogger(
['message' => $error], ['message' => $error],
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE, SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company $this->company
); ))->handle();
} catch (ClientException $e) { } catch (ClientException $e) {
nlog("Client exception"); nlog("Client exception");
$error = json_decode($e->getResponse()->getBody()->getContents()); $error = json_decode($e->getResponse()->getBody()->getContents());
SystemLogger::dispatch( (new SystemLogger(
['message' => $error], ['message' => $error],
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE, SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company $this->company
); ))->handle();
} catch (\Exception $e) { } catch (\Exception $e) {
nlog("Exception handler => " . $e->getMessage()); nlog("Exception handler => " . $e->getMessage());
nlog($e->getCode()); nlog($e->getCode());
SystemLogger::dispatch( (new SystemLogger(
$e->getMessage(), $e->getMessage(),
SystemLog::CATEGORY_WEBHOOK, SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE, SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(), $this->resolveClient(),
$this->company, $this->company,
); ))->handle();
$this->release($this->backoff()[$this->attempts()-1]); $this->release($this->backoff()[$this->attempts()-1]);
} }
@ -220,7 +227,6 @@ class WebhookSingle implements ShouldQueue
private function resolveClient() private function resolveClient()
{ {
nlog(get_class($this->entity));
//make sure it isn't an instance of the Client Model //make sure it isn't an instance of the Client Model
if (!$this->entity instanceof \App\Models\Client && if (!$this->entity instanceof \App\Models\Client &&
@ -231,8 +237,8 @@ class WebhookSingle implements ShouldQueue
return $this->entity->client; return $this->entity->client;
} }
return null; return null;
} }
public function failed($exception = null) public function failed($exception = null)

View File

@ -416,7 +416,6 @@ class EventServiceProvider extends ServiceProvider
], ],
InvoiceWasPaid::class => [ InvoiceWasPaid::class => [
InvoicePaidActivity::class, InvoicePaidActivity::class,
CreateInvoicePdf::class,
], ],
InvoiceWasViewed::class => [ InvoiceWasViewed::class => [
InvoiceViewedActivity::class, InvoiceViewedActivity::class,
@ -434,7 +433,6 @@ class EventServiceProvider extends ServiceProvider
], ],
InvoiceWasDeleted::class => [ InvoiceWasDeleted::class => [
InvoiceDeletedActivity::class, InvoiceDeletedActivity::class,
CreateInvoicePdf::class,
], ],
InvoiceWasArchived::class => [ InvoiceWasArchived::class => [
InvoiceArchivedActivity::class, InvoiceArchivedActivity::class,