PHPackages                             philippgrashoff/cronforatk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. philippgrashoff/cronforatk

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

philippgrashoff/cronforatk
==========================

6.0.3(3mo ago)21.1k↓46.4%1PHPPHP 8.3.\*CI failing

Since Apr 10Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/PhilippGrashoff/cronforatk)[ Packagist](https://packagist.org/packages/philippgrashoff/cronforatk)[ RSS](/packages/philippgrashoff-cronforatk/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (16)Used By (0)

cronforatk
==========

[](#cronforatk)

[![codecov](https://camo.githubusercontent.com/420daa4404a6d895a1fea243594cc42302649fff90a52d252cdc1b89158ab8af/68747470733a2f2f636f6465636f762e696f2f67682f5068696c69707047726173686f66662f63726f6e666f7261746b2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/PhilippGrashoff/cronforatk)

This repository is an extension for [atk4/data](https://github.com/atk4/data). It enables you to easily set and store the execution of many different cronjobs. Many different schedule options from yearly to minutely are available. 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. 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. **Executor::run() needs to be executed each minute** by a system cron for this repo to work properly.
- **ExecutionLog**: In here, execution results are logged. If execution results should be logged can be defined per Scheduler.
- **CronJobLoader**: This is a small helper. As all Cronjobs are stored as PHP classes, this class checks given directories for available BaseCronJob implementations.

How to use
==========

[](#how-to-use)

Installation
------------

[](#installation)

The easiest way to use this repository is to add it to your composer.json in the require section:

```
{
  "require": {
    "philippgrashoff/cronforatk": "4.0.*"
  }
}
```

Setup Database
--------------

[](#setup-database)

Two classes in this package need a database table: **Scheduler** and **ExecutionLog**You can use [Atk4\\Data\\Schema\\Migrator](https://github.com/atk4/data/blob/develop/src/Schema/Migrator.php) to create these 2 tables and the foreign key in your database.

```
$persistence = new Sql(
    'connection_string',
    'username',
    'password'
);

//Setup database
$scheduler = new Scheduler($this->db);
(new Migrator($scheduler))->create();
$executionLog = new ExecutionLog($this->db);
(new Migrator($executionLog))->create()->createForeignKey($executionLog->getReference('scheduler_id'));
```

Sample usage
------------

[](#sample-usage)

### 1) Create a real cronjob that extends BaseCronJob

[](#1-create-a-real-cronjob-that-extends-basecronjob)

```
class MyCronJob extends BaseCronJob
{

    public static string $name = 'SomeNameForThisCron';
    public static string $description = 'SomeDescriptionExplainingWhatThisIsDoing';

    public bool $strict = false;

    public function execute(): void
    {
        //do something
        $someModel = new SomeModel($this->persistence);
        $someModel->setLimit(100);
        foreach ($someModel as $entity) {
            if($this->strict) {
                $entity->doSomeStrictCheck();
            }
            else {
                $entity->doSomeOtherCheck();
            }
            //optionally add some output to log
            $this->executionLog[] = 'SomeModel With ID ' . $entity->getId()
                . 'checked at ' . (new \DateTime())->format(DATE_ATOM);
        }
    }
}
```

### 2) Add one or more Schedulers to schedule when the cronjob should be executed

[](#2-add-one-or-more-schedulers-to-schedule-when-the-cronjob-should-be-executed)

Note: You can create a nice UI to create and update schedulers by using [atk4\\ui](https://github.com/atk4/ui). In the following sample code, the schedulers are solely created on data level.

```
//tell the Schedulers where to look for Cronjob implementations.
//In real implementations, extend Scheduler to set this once for all Schedulers
$pathsToCronJobs = [__DIR__ => 'cronforatk\docs'];

//Have our Cronjob executed Daily.
$schedulerDaily = new Scheduler($persistence, ['cronFilesPaths' => $pathsToCronJobs]);
$schedulerDaily->set('cronjob_class', MyCronJob::class);
$schedulerDaily->set('interval', 'DAILY');
$schedulerDaily->set('time_daily', '03:45');
$schedulerDaily->save();

//you could add more schedulers executing the same cronjob in different intervals.
// Here, we will add a weekly check sets the "strict" of MyCronJob to true. Like this, cronjobs can be parametrized
$schedulerWeekly = new Scheduler($persistence, ['cronFilesPaths' => $pathsToCronJobs]);
$schedulerWeekly->set('cronjob_class', MyCronJob::class);
$schedulerWeekly->set('interval', 'WEEKLY');
$schedulerWeekly->set('weekday_weekly', 6); //Saturday
$schedulerWeekly->set('time_weekly', '01:32');
$schedulerWeekly->set('defaults', ['strict' => true]);
$schedulerWeekly->save();
```

### 3) Run Executor::run() minutely

[](#3-run-executorrun-minutely)

Executor::run() checks which schedulers (and hence cronjobs) need to be executed at each minute by the definitions set in the schedulers. Add a simple script which is executed each minute by the system cron:

```
$executor = new Executor($persistence);
$executor->run();
```

All sample code from this readme can be found in the `docs` directory.

Versioning
==========

[](#versioning)

The version numbers of this repository correspond with the atk4\\data versions. So 4.0.x is compatible with atk4\\data 4.0.x and so on.

Open Todos
==========

[](#open-todos)

- Currently, there are different time fields in Scheduler: time\_yearly, time\_monthly, time\_weekly and time\_daily. This should be replaced be a single time field.
- There is no locking implemented (A cronjob could still be in schedule/currently being executed while being executed again by the next minutely Executor::run()). This should only be an issue for crons that are executed minutely.
- There currently is no option to execute a cronjob at the last day of the month. This would be a sensible addition.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance86

Actively maintained with recent releases

Popularity23

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~125 days

Recently: every ~212 days

Total

15

Last Release

100d ago

Major Versions

2.3.2 → 4.0.02023-08-17

4.0.4 → 5.0.02023-10-02

5.0.0 → 6.0.02025-10-18

PHP version history (2 changes)4.0.0PHP 8.\*

6.0.0PHP 8.3.\*

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8d7dc9f818ca121a41b3d19bd263147b404e0488096d2357729dea14232ec6e?d=identicon)[Philipp Reisigl](/maintainers/Philipp%20Reisigl)

---

Top Contributors

[![PhilippGrashoff](https://avatars.githubusercontent.com/u/33204878?v=4)](https://github.com/PhilippGrashoff "PhilippGrashoff (57 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philippgrashoff-cronforatk/health.svg)

```
[![Health](https://phpackages.com/badges/philippgrashoff-cronforatk/health.svg)](https://phpackages.com/packages/philippgrashoff-cronforatk)
```

###  Alternatives

[symfony/polyfill-uuid

Symfony polyfill for uuid functions

688335.4M63](/packages/symfony-polyfill-uuid)[spatie/error-solutions

This is my package error-solutions

6853.2M11](/packages/spatie-error-solutions)[phpflo/phpflo

Flow-based programming for PHP

2173.3k4](/packages/phpflo-phpflo)[eftec/autoloadone

AutoloadOne is a program that generates an autoload class for PHP.

403.4k](/packages/eftec-autoloadone)[ys-tools/default-theme-configuration-bundle

OroCommerce Default Theme Configuration Bundle

124.2k](/packages/ys-tools-default-theme-configuration-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
