aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Job/PeriodicFetchRemoteFeed.php
blob: e84757fe2944e04ddff48c8360154ca6441e8ec7 (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
<?php
namespace Digitigrade\Job;

use Digitigrade\GlobalSettings;
use Digitigrade\Job;
use Digitigrade\JobQueue;
use Digitigrade\Logger;
use Digitigrade\Model\Actor;

class PeriodicFetchRemoteFeed extends Job {
    public int $actorId;

    public function __construct(Actor $targetActor) {
        $this->actorId = $targetActor->id;
        $this->remainingTries = 8;
    }

    public function run() {
        $actor = Actor::find($this->actorId);
        $logger = Logger::getInstance();
        // only bother if someone local still follows the actor
        if (count(array_filter($actor->findFollowers(), fn(Actor $a) => $a->isLocal)) > 0) {
            $logger->info('Fetching feed for simple remote actor ' . $actor->getFullHandle());
            $actor->backfill(GlobalSettings::getInstance()->get('instance.feedUpdateLimit') ?? 50);
            $this->submit();
        } else {
            $logger->info('No longer fetching feed for ' . $actor->getFullHandle() . ' as nobody follows them anymore');
        }
    }

    public function submit() {
        JobQueue::getInstance()->submitDelayedUntil(
            $this,
            (new \DateTimeImmutable)->add(new \DateInterval(GlobalSettings::getInstance()->get('instance.feedUpdateInterval') ?? 'PT1H'))
        );
    }
}