death-notifier/src/main/php/com/fwdekker/deathnotifier/EmulateCronCliAction.php

66 lines
1.5 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier;
/**
* Periodically executes several other actions, as if cron jobs have been set up to do so.
*
* Intended for local development only.
*/
class EmulateCronCliAction extends Action
{
/**
* The number of seconds between executing tasks.
*/
private const INTERVAL = 15;
/**
* @var Action[] the actions to execute at an interval
*/
private readonly array $actions;
/**
* Constructs a new `EmulateCronAction`.
*
* @param Action[] $actions the actions to execute at an interval
*/
public function __construct(array $actions)
{
parent::__construct(ActionMethod::CLI, "emulate-cron");
$this->actions = $actions;
}
/**
* Validates the inputs of each registered action.
*
* @return void if the input is valid
* @throws ValidationException if the input is invalid
*/
public function validate_inputs(): void
{
foreach ($this->actions as $action)
$action->validate_inputs();
}
/**
* Updates all trackings and processes the mail queue at a regular interval.
*
* @return never
*/
public function handle(): never
{
// @phpstan-ignore-next-line
while (true) {
print("Emulating cron jobs.\n");
foreach ($this->actions as $action)
$action->handle();
print("Done\n");
sleep(self::INTERVAL);
}
}
}