-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cronjob: Add ability to execute periodic tasks via cron job
- Loading branch information
Showing
3 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace A11yBuddy\Cronjob; | ||
|
||
/** | ||
* A task that can be run periodically. | ||
* Tasks will be run via CLI through cron job. | ||
*/ | ||
abstract class CronjobTask | ||
{ | ||
|
||
/** | ||
* Checks if the task can be run. | ||
* Because cron jobs run every minute, this method can be used to check if the task should be run at this time or not. | ||
*/ | ||
public function canRun(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Runs the task. | ||
*/ | ||
public abstract function run(): void; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace A11yBuddy\Cronjob; | ||
|
||
/** | ||
* Manages periodic tasks that run via cron job. | ||
*/ | ||
class CronjobTaskManager | ||
{ | ||
|
||
/** | ||
* @var CronjobTask[] The tasks that are registered with the manager. | ||
*/ | ||
private array $tasks = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->registerAllTasks(); | ||
} | ||
|
||
/** | ||
* Registers all tasks that are shipped and required by the application. | ||
*/ | ||
private function registerAllTasks(): void | ||
{ | ||
// TODO | ||
return; | ||
} | ||
|
||
/** | ||
* Adds a task to the manager. | ||
*/ | ||
public function addTask(CronjobTask $task): void | ||
{ | ||
$this->tasks[] = $task; | ||
} | ||
|
||
/** | ||
* Runs all tasks that can be run. | ||
*/ | ||
public function runTasks() | ||
{ | ||
foreach ($this->tasks as $task) { | ||
if ($task->canRun()) | ||
$task->run(); | ||
} | ||
} | ||
|
||
} |