Skip to content

Commit

Permalink
sensible renaming of major classes
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrashoff committed Aug 22, 2023
1 parent d136d43 commit 894c7da
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 302 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ of many different cronjobs. Many different schedule options from yearly to minut
The logic of these cronjobs has to be implemented separately.

The repository consists of these classes:
* BaseCronJob: This is a base for all real cronjobs
* CronJobExecutor: This class contains all the logic to decide when which cronjob should be executed.
* CronJobModel: This is used to persist each wanted execution. It contains the info which cronjob should be executed when.
* CronJobloader: This is a small helper. As all Cronjobs are stored as PHP classes, this class checks given directories for available cronjob implementations.
* CronJobExecutionLog: In here, execution results are logged. If execution results should be logged can be defined per CronJobModel.
* BaseCronJob: This is a base for all real cronjobs. The actual logic for a cronjob is implemented in a class extending BaseCronJob.
* Scheduler: This is used to persist each wanted execution. It contains the info which BaseCronJob child should be executed when.
* Executor: This class contains all the logic to decide when which Scheduler (and hence the corresponding BaseCronJob child) should be executed.
* ExecutionLog: In here, execution results are logged. If execution results should be logged can be defined per CronJobModel.
* CronJobLoader: This is a small helper. As all Cronjobs are stored as PHP classes, this class checks given directories for available BaseCronJob implementations.
19 changes: 0 additions & 19 deletions config.php

This file was deleted.

Binary file modified coverage/phpunit.cov
Binary file not shown.
4 changes: 2 additions & 2 deletions src/CronJobExecutionLog.php → src/ExecutionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Atk4\Core\Exception;
use Atk4\Data\Model;

class CronJobExecutionLog extends Model
class ExecutionLog extends Model
{

public $table = 'cronjob_execution_log';
Expand Down Expand Up @@ -55,7 +55,7 @@ protected function init(): void
]
);

$this->hasOne('cronjob_id', ['model' => [CronJobModel::class]]);
$this->hasOne('cronjob_id', ['model' => [Scheduler::class]]);
$this->setOrder(['execution_datetime' => 'desc']);
}
}
48 changes: 24 additions & 24 deletions src/CronJobExecutor.php → src/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Throwable;


class CronJobExecutor
class Executor
{
protected Persistence $persistence;

Expand Down Expand Up @@ -47,8 +47,8 @@ public function run(DateTime $dateTime = null): void
$this->currentMinute = (int)$dateTime->format('i');

//execute yearly first, minutely last.
foreach (CronJobModel::$intervalSettings as $interval => $intervalName) {
$cronJobModels = new CronJobModel($this->persistence);
foreach (Scheduler::$intervalSettings as $interval => $intervalName) {
$cronJobModels = new Scheduler($this->persistence);
$cronJobModels->addCondition('interval', $interval);
$cronJobModels->addCondition('is_active', 1);

Expand All @@ -59,12 +59,12 @@ public function run(DateTime $dateTime = null): void
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return void
* @throws \Atk4\Core\Exception
* @throws Exception
*/
protected function executeCronIfScheduleMatches(CronJobModel $cronJobEntity): void
protected function executeCronIfScheduleMatches(Scheduler $cronJobEntity): void
{
$entityInterval = $cronJobEntity->get('interval');
if ($entityInterval === 'YEARLY' && $this->checkYearlyExecutionIsNow($cronJobEntity)) {
Expand All @@ -83,10 +83,10 @@ protected function executeCronIfScheduleMatches(CronJobModel $cronJobEntity): vo
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return bool
*/
protected function checkYearlyExecutionIsNow(CronJobModel $cronJobEntity): bool
protected function checkYearlyExecutionIsNow(Scheduler $cronJobEntity): bool
{
if (
!$cronJobEntity->get('date_yearly') instanceof DateTimeInterface
Expand All @@ -106,10 +106,10 @@ protected function checkYearlyExecutionIsNow(CronJobModel $cronJobEntity): bool
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return bool
*/
protected function checkMonthlyExecutionIsNow(CronJobModel $cronJobEntity): bool
protected function checkMonthlyExecutionIsNow(Scheduler $cronJobEntity): bool
{
if (
$cronJobEntity->get('day_monthly') < 1
Expand All @@ -128,10 +128,10 @@ protected function checkMonthlyExecutionIsNow(CronJobModel $cronJobEntity): bool
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return bool
*/
protected function checkWeeklyExecutionIsNow(CronJobModel $cronJobEntity): bool
protected function checkWeeklyExecutionIsNow(Scheduler $cronJobEntity): bool
{
if (
$cronJobEntity->get('weekday_weekly') !== $this->currentWeekday
Expand All @@ -144,10 +144,10 @@ protected function checkWeeklyExecutionIsNow(CronJobModel $cronJobEntity): bool
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return bool
*/
protected function checkDailyExecutionIsNow(CronJobModel $cronJobEntity): bool
protected function checkDailyExecutionIsNow(Scheduler $cronJobEntity): bool
{
if (
!$cronJobEntity->get('time_daily')
Expand All @@ -159,19 +159,19 @@ protected function checkDailyExecutionIsNow(CronJobModel $cronJobEntity): bool
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return bool
*/
protected function checkHourlyExecutionIsNow(CronJobModel $cronJobEntity): bool
protected function checkHourlyExecutionIsNow(Scheduler $cronJobEntity): bool
{
return $this->currentMinute === $cronJobEntity->get('minute_hourly');
}

/**
* @param CronJobModel $cronJobEntity
* @param Scheduler $cronJobEntity
* @return bool
*/
protected function checkMinutelyExecutionIsNow(CronJobModel $cronJobEntity): bool
protected function checkMinutelyExecutionIsNow(Scheduler $cronJobEntity): bool
{
if ($cronJobEntity->get('interval_minutely') == 'EVERY_MINUTE') {
return true;
Expand All @@ -190,14 +190,14 @@ protected function checkMinutelyExecutionIsNow(CronJobModel $cronJobEntity): boo
}

/**
* @param CronJobModel $entity
* @param Scheduler $entity
* @return void
* @throws \Atk4\Core\Exception
* @throws Exception
*/
protected function executeCronJob(CronJobModel $entity): void
protected function executeCronJob(Scheduler $entity): void
{
$executionLog = (new CronJobExecutionLog($this->persistence))->createEntity();
$executionLog = (new ExecutionLog($this->persistence))->createEntity();
$executionLog->set('cronjob_id', $entity->getId());
$executionLog->set('execution_datetime', new DateTime());
$startOfCron = microtime(true);
Expand Down Expand Up @@ -232,22 +232,22 @@ protected function executeCronJob(CronJobModel $entity): void
* This function can be implemented in child classes which extend this class in order you want some custom reporting
* (e.g. an Email), per successful cronjob run
*
* @param CronJobExecutionLog $cronJobExecutionLog
* @param ExecutionLog $cronJobExecutionLog
* @return void
*/
protected function reportSuccess(CronJobExecutionLog $cronJobExecutionLog): void
protected function reportSuccess(ExecutionLog $cronJobExecutionLog): void
{
}

/**
* This function can be implemented in child classes which extend this class in order you want some custom reporting
* (e.g. an Email), per successful cronjob run
*
* @param CronJobExecutionLog $cronJobExecutionLog
* @param ExecutionLog $cronJobExecutionLog
* @param Throwable $e
* @return void
*/
protected function reportFailure(CronJobExecutionLog $cronJobExecutionLog, Throwable $e): void
protected function reportFailure(ExecutionLog $cronJobExecutionLog, Throwable $e): void
{
}
}
8 changes: 4 additions & 4 deletions src/CronJobModel.php → src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Atk4\Data\Model;


class CronJobModel extends Model
class Scheduler extends Model
{

public $table = 'cronjob';
Expand Down Expand Up @@ -188,13 +188,13 @@ protected function init(): void
);

$this->hasMany(
CronJobExecutionLog::class,
['model' => [CronJobExecutionLog::class], 'theirField' => 'cronjob_id']
ExecutionLog::class,
['model' => [ExecutionLog::class], 'theirField' => 'cronjob_id']
);
$this->addExpression(
'last_executed',
[
'expr' => $this->refLink(CronJobExecutionLog::class)
'expr' => $this->refLink(ExecutionLog::class)
->action('field', ['execution_datetime'])
->limit(1),
'type' => 'datetime',
Expand Down
58 changes: 0 additions & 58 deletions tests/CronJobModelTest.php

This file was deleted.

Loading

0 comments on commit 894c7da

Please sign in to comment.