diff options
| author | winter | 2025-01-14 17:04:57 +0000 |
|---|---|---|
| committer | winter | 2025-01-14 17:04:57 +0000 |
| commit | d7940cf9470ee8377cf6484f4a20869c62d48685 (patch) | |
| tree | 7d6848093e1111636b380e894fda89562f9278d8 | |
| parent | 3ce58a33945fc858ac4f8b2b8471dff0cf294863 (diff) | |
don't retry pushing to an instance with no push endpoint
| -rw-r--r-- | Digitigrade/Exception/EndpointMissingException.php | 23 | ||||
| -rw-r--r-- | Digitigrade/Job/PushObject.php | 12 | ||||
| -rw-r--r-- | Digitigrade/Model/Instance.php | 11 |
3 files changed, 39 insertions, 7 deletions
diff --git a/Digitigrade/Exception/EndpointMissingException.php b/Digitigrade/Exception/EndpointMissingException.php new file mode 100644 index 0000000..b347838 --- /dev/null +++ b/Digitigrade/Exception/EndpointMissingException.php @@ -0,0 +1,23 @@ +<?php +namespace Digitigrade\Exception; + +use Digitigrade\Model\Instance; + +class EndpointMissingException extends \RuntimeException { + protected Instance $instance; + protected string $endpointName; + + public function __construct(Instance $instance, string $endpointName, \Throwable $previous = null) { + parent::__construct("Instance $instance->domain has no $endpointName endpoint", previous: $previous); + $this->instance = $instance; + $this->endpointName = $endpointName; + } + + public function getInstance(): Instance { + return $this->instance; + } + + public function getEndpointName(): string { + return $this->endpointName; + } +}
\ No newline at end of file diff --git a/Digitigrade/Job/PushObject.php b/Digitigrade/Job/PushObject.php index f5f1430..ee5841e 100644 --- a/Digitigrade/Job/PushObject.php +++ b/Digitigrade/Job/PushObject.php @@ -1,6 +1,7 @@ <?php namespace Digitigrade\Job; +use Digitigrade\Exception\EndpointMissingException; use Digitigrade\Job; use Digitigrade\Logger; use Digitigrade\Model\Instance; @@ -24,7 +25,14 @@ class PushObject extends Job { $object = $this->objectType::find($this->objectId); assert($object instanceof PushableModel); - Logger::getInstance()->info("pushing object $object->uri to server $instance->domain"); - $instance->pushObject($object); + $log = Logger::getInstance(); + $log->info("pushing object $object->uri to server $instance->domain"); + try { + $instance->pushObject($object); + } catch (EndpointMissingException $e) { + $log->warning("can't push to $instance->domain because it has no push endpoint. giving up"); + // and don't throw again here because we want it to look successful and not be retried + // reasoning: i think it's unlikely for an instance with no endpoint to suddenly acquire one + } } }
\ No newline at end of file diff --git a/Digitigrade/Model/Instance.php b/Digitigrade/Model/Instance.php index b3a179f..2b07b9a 100644 --- a/Digitigrade/Model/Instance.php +++ b/Digitigrade/Model/Instance.php @@ -1,6 +1,7 @@ <?php namespace Digitigrade\Model; +use Digitigrade\Exception\EndpointMissingException; use Digitigrade\GlobalConfig; use Digitigrade\Job\RefreshOutboundAuthToken; @@ -88,7 +89,7 @@ class Instance extends FetchableModel { public function beginOutboundAuth() { $authEndpoint = $this->endpoints->auth; if (!isset($authEndpoint)) { - throw new \RuntimeException("can't authenticate with $this->domain because it doesn't have an auth endpoint"); + throw new EndpointMissingException($this, 'auth'); } file_get_contents($authEndpoint . '?phase=dialback&target=' . urlencode(path_to_uri('/auth/dialback'))); } @@ -96,7 +97,7 @@ class Instance extends FetchableModel { public function requestOutboundAuthToken(string $secret) { $authEndpoint = $this->endpoints->auth; if (!isset($authEndpoint)) { - throw new \RuntimeException("can't authenticate with $this->domain because it doesn't have an auth endpoint"); + throw new EndpointMissingException($this, 'auth'); } $response = file_get_contents($authEndpoint . '?phase=token&secret=' . urlencode($secret)); if ($response === false) { @@ -123,7 +124,7 @@ class Instance extends FetchableModel { public function refreshOutboundAuthToken() { $authEndpoint = $this->endpoints->auth; if (!isset($authEndpoint)) { - throw new \RuntimeException("can't authenticate with $this->domain because it doesn't have an auth endpoint"); + throw new EndpointMissingException($this, 'auth'); } if (!isset($this->auth->outboundToken)) { throw new \RuntimeException("can't refresh outbound token for $this->domain because i don't already have one"); @@ -147,7 +148,7 @@ class Instance extends FetchableModel { private function subscribeOrUnsubscribe(bool $isUnsubscribe): bool { if (!isset($this->endpoints->subscribe, $this->endpoints->unsubscribe)) { - throw new \RuntimeException("can't (un)subscribe to $this->domain because it doesn't have the right endpoints"); + throw new EndpointMissingException($this, 'subscribe'); } if (!isset($this->auth->outboundToken)) { throw new \RuntimeException("can't (un)subscribe to $this->domain because i don't have an outbound token for it"); @@ -189,7 +190,7 @@ class Instance extends FetchableModel { public function pushObject(PushableModel $obj) { if (!isset($this->endpoints->push)) { - throw new \RuntimeException("can't push to $this->domain because it doesn't have a push endpoint"); + throw new EndpointMissingException($this, 'push'); } if (!isset($this->auth->outboundToken)) { throw new \RuntimeException("can't push to $this->domain because i don't have an outbound token for it"); |
