Skip to content

Commit

Permalink
Cronjob: Add ability to execute periodic tasks via cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
KreerC committed Mar 24, 2024
1 parent 8f3b7e7 commit 82c5ae9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
18 changes: 13 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@
// that includes proper error handling and security measures.

session_start();
require_once("vendor/autoload.php");
require_once ("vendor/autoload.php");

use A11yBuddy\Application;

$config = require_once("config.php");
$config = require_once ("config.php");

// Check if there is an example config file
if (file_exists("config.example.php")) {
$exampleConfig = require_once("config.example.php");
$exampleConfig = require_once ("config.example.php");

// Tell the user which keys are missing in their config file
$missingKeys = array_diff_key($exampleConfig, $config);
if (!empty($missingKeys)) {
if (!empty ($missingKeys)) {
echo "Your config file is missing the following keys:\n";
foreach ($missingKeys as $key => $value) {
echo " - $key\n";
}
echo "Please add them to your config file.\n";
exit(1);
exit (1);
}
}

$app = new Application($config);

// If we are a CLI script, only run the periodic tasks
if (php_sapi_name() === 'cli') {
$taskManager = new A11yBuddy\Cronjob\CronjobTaskManager();
$taskManager->runTasks();
exit (0);
}

$app->getBasePageRenderer()->render();
26 changes: 26 additions & 0 deletions src/Cronjob/CronjobTask.php
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;

}
49 changes: 49 additions & 0 deletions src/Cronjob/CronjobTaskManager.php
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();
}
}

}

0 comments on commit 82c5ae9

Please sign in to comment.