aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Job/PushObject.php
blob: c3d7210814608eead0800e7a93e76ae39118f876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Digitigrade\Job;

use Digitigrade\Exception\AuthException;
use Digitigrade\Exception\EndpointMissingException;
use Digitigrade\Job;
use Digitigrade\Logger;
use Digitigrade\Model\Instance;
use Digitigrade\Model\PushableModel;

class PushObject extends Job {
    public string $objectType;
    public string $objectId;
    public string $recipientServerHost;

    public function __construct(PushableModel $object, Instance $recipient) {
        $this->objectType = $object::class;
        $this->objectId = $object->id;
        $this->recipientServerHost = $recipient->domain;
        $this->remainingTries = 8;
    }

    public function run() {
        $instance = Instance::findByHostname($this->recipientServerHost);
        assert($instance != null);
        $object = $this->objectType::find($this->objectId);
        assert($object instanceof PushableModel);

        $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
        } catch (AuthException $e) {
            // start auth now and hopefully by the next retry we'll be able to
            $instance->beginOutboundAuth();
            throw $e;
        }
    }
}