Lines 100.00% 13 / 13
Functions and Methods 100.00% 11 / 11
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerScheduler 100.00% 13 / 13 100.00% 11 / 11 11 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 forApp 100.00% 1 / 1 100.00% 1 / 1 1
 runEvery 100.00% 1 / 1 100.00% 1 / 1 1
 runDailyAt 100.00% 1 / 1 100.00% 1 / 1 1
 runOnCron 100.00% 1 / 1 100.00% 1 / 1 1
 runAfter 100.00% 1 / 1 100.00% 1 / 1 1
 runAt 100.00% 1 / 1 100.00% 1 / 1 1
 runAtEntityTime 100.00% 3 / 3 100.00% 1 / 1 1
 runAtSunEvent 100.00% 1 / 1 100.00% 1 / 1 1
 waitFor 100.00% 1 / 1 100.00% 1 / 1 1
 armSchedule 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Schedule;
6
7use Closure;
8use DateTimeImmutable;
9use Psr\Log\LoggerInterface;
10use Stewart\Contracts\App\AppId;
11use Stewart\Contracts\Entity\EntityId;
12use Stewart\Contracts\Schedule\DayOfWeek;
13use Stewart\Contracts\Schedule\ScheduledTask;
14use Stewart\Contracts\Schedule\Scheduler;
15use Stewart\Contracts\Schedule\TimeOfDay;
16use Stewart\Contracts\Sun\SunCalendar;
17use Stewart\Contracts\Sun\SunEvent;
18use Stewart\Contracts\Time\Duration;
19use Stewart\Contracts\Time\PendingWait;
20use Stewart\Contracts\Time\SunOffset;
21use Stewart\Runtime\Model\ResourceScope;
22use Stewart\Support\Schedule\CalendarSchedule;
23use Stewart\Support\Schedule\CronSchedule;
24use Stewart\Support\Schedule\DelaySchedule;
25use Stewart\Support\Schedule\ElapsedSchedule;
26use Stewart\Support\Schedule\EntityTime;
27use Stewart\Support\Schedule\IntervalSchedule;
28use Stewart\Support\Schedule\OneShotSchedule;
29use Stewart\Support\Schedule\SunEventSchedule;
30use Stewart\Support\Schedule\WallClockSchedule;
31use Stewart\Support\Time\Timers;
32
33final readonly class WorkerScheduler implements Scheduler
34{
35    public function __construct(
36        private ScheduleRegistry $registry,
37        private SunCalendar $sunCalendar,
38        private EntityTimeScheduler $entityTimes,
39        private LoggerInterface $logger,
40        private ResourceScope $resourceScope,
41        private Timers $loopTimers,
42    ) {}
43
44    public function forApp(AppId $appId, LoggerInterface $logger): self
45    {
46        return new self($this->registry, $this->sunCalendar, $this->entityTimes, $logger, ResourceScope::forApp($appId), $this->loopTimers);
47    }
48
49    public function runEvery(Duration $period, Closure $handler): ScheduledTask
50    {
51        return $this->armSchedule(IntervalSchedule::every($period), $handler);
52    }
53
54    public function runDailyAt(TimeOfDay|string $time, Closure $handler, DayOfWeek ...$onlyOn): ScheduledTask
55    {
56        return $this->armSchedule(CalendarSchedule::dailyAt($time, ...$onlyOn), $handler);
57    }
58
59    public function runOnCron(string $expression, Closure $handler): ScheduledTask
60    {
61        return $this->armSchedule(CronSchedule::parse($expression), $handler);
62    }
63
64    public function runAfter(Duration $delay, Closure $handler): ScheduledTask
65    {
66        return $this->armSchedule(DelaySchedule::after($delay), $handler);
67    }
68
69    public function runAt(DateTimeImmutable $moment, Closure $handler): ScheduledTask
70    {
71        return $this->armSchedule(OneShotSchedule::fromMoment($moment), $handler);
72    }
73
74    public function runAtEntityTime(EntityId|string $entity, Closure $handler): ScheduledTask
75    {
76        $entityId = EntityId::fromStringOrId($entity);
77        EntityTime::requireSupportedEntity($entityId);
78
79        return $this->entityTimes->armForEntity($this->resourceScope, $this->logger, $entityId, $handler);
80    }
81
82    public function runAtSunEvent(SunEvent $event, Closure $handler, ?SunOffset $offset = null): ScheduledTask
83    {
84        return $this->armSchedule(SunEventSchedule::forEvent($this->sunCalendar, $event, $offset), $handler);
85    }
86
87    // On the loop timers rather than a schedule, so a paused app's wait still times out.
88    /**
89     * @template T
90     * @param Closure(PendingWait<T>): (Closure(): void) $arm
91     * @return T|null
92     */
93    public function waitFor(Closure $arm, ?Duration $timeout = null): mixed
94    {
95        return $this->loopTimers->waitFor($arm, $timeout);
96    }
97
98    private function armSchedule(WallClockSchedule|ElapsedSchedule $schedule, Closure $handler): ScheduledTask
99    {
100        return $this->registry->arm($this->resourceScope, $schedule, $this->logger, $handler);
101    }
102}