Lines
100.00%
13 / 13
Methods
100.00%
11 / 11
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __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 | ||
| 33 | final 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 | } |