Componette

Componette

bileto

bileto / cronner v3.0.0

Simple tool which helps with maintenance of cron tasks.

download-cloud-line composer require stekycz/cronner

Cronner PHP Composer

Description

Simple tool which helps with maintenance of cron tasks.

It requires PHP >= 8.0 and Nette Framework >= 3.0.

Usage

It is very simple to use it because configuration is only in method annotations. Example class with tasks follows.

class CronTasks
{
    /**
     * @cronner-task E-mail sending
     * @cronner-period 1 day
     * @cronner-days working days
     * @cronner-time 23:30 - 05:00
     */
    public function sendEmails(): void
    {
        // Code which sends all your e-mails
    }


    /**
     * @cronner-task Important data replication
     * @cronner-period 3 hours
     */
    public function replicateImportantData(): void
    {
        // Replication code
    }
}

It is recommend to use compiler extension.

extension:
    cronner: Bileto\Cronner\DI\CronnerExtension

It does not require any configuration however your own implementation of timestamp storage could be better then the default storage. Your storage must be defined as a service in config.neon and Cronner will find it. However you can specify service manually if it is not autowireable.

cronner:
    timestampStorage: myCoolTimestampStorage

Or you can change the directory for default storage.

cronner:
    timestampStorage: Bileto\Cronner\TimestampStorage\FileStorage(%wwwDir%/../temp/cronner)

It is also possible to define maxExecutionTime for Cronner so you do not have make it by you own code (and probably for all your requests). Option criticalSectionTempDir can be change however the directory must be writable for php process. It is used to run each task only once at time.

At the end you would need to specify your task objects. It would be some service with high probability. You can add tag cronner.tasks to all services with Cronner tasks and those services will be bind automatically. However you can still add new task objects by your own using addTasks method.

Then you can use it very easily in Presenter

class CronPresenter extends \Nette\Application\UI\Presenter
{
    /**
     * @var \Bileto\Cronner\Cronner
     * @inject
     */
    public $cronner;

    
    public function actionCron(): void
    {
        $this->cronner->run();
    }
}

or in Command from Kdyby/Console.

Service configuration is also possible but it should not be used using new versions of Nette because extension usage is recommended and preferable way. However you will still need to call run method somewhere in your Presenter or console Command.

services:
    cronner: Bileto\Cronner\Cronner(Bileto\Cronner\TimestampStorage\FileStorage(%wwwDir%/../temp/cronner))
    setup:
    	- addTasks(new CronTasks())

Annotations

@cronner-task

This annotations is required for all public methods which should be used as a task. Its value is used as a name of task. If the value is missing the name is build from class name and method name.

If this annotation is single (for Cronner) in task method comment then the task is run every time when Cronner runs.

Note: Magic methods cannot be used as task (__construct, __sleep, etc.).

Example

/**
 * @cronner-task Fetches all public data from all registered social networks
 */

@cronner-period

Not required but recommended annotation which specifies period of task execution. The period is minimal time between two executions of the task. It's value can be anything what is acceptable for strtotime() function. The only restriction is usability with "+" sign before the time because it is added by Cronner automatically. So first day of this month is not acceptable however 1 month is acceptable.

Attention! The value of this annotation must not contain any sign (+ or -).

Example

/**
 * @cronner-period 1 day
 */

@cronner-days

Allows run the task only on specified days. Possible values are abbreviations of week day names. It means Mon, Tue, Wed, Thu, Fri, Sat and Sun. There are two shortcuts for easier usage: working days (Mon, Tue, Wed, Thu, Fri) and weekend (Sat and Sun) which are internally expanded to specific days. Multiple values must be separated by comma (Mon, Wed, Fri) or can be specified by range Mon-Thu.

Example

/**
 * @cronner-days working days, Sun
 */

@cronner-time

Specifies day time range (or ranges) in which the task can be run. It can be range or a specific minute. It uses 24 hour time model. Multiple values must be separated by comma.

The time can be defined over midnight as it is in following example.

Note: There is tolerance time of 5 seconds to run task as soon as possible if previous run have had slower start from any reason.

Example

/**
 * @cronner-time 11:00, 23:30 - 05:00
 */
  • v3.0.0 v3.0.0

  • v2.3.0 Package renamed to Bileto/Cronner

  • v2.2.0

    • PHP 7.2 support (#57)
  • v2.1.1

    • bugfix: duplicate service names (#56)
  • v2.1.0

    • CriticalSection class was removed and replaced by stekycz/critical-section package using interfaces and drivers for more flexibility
    • Fixed failing tests for Nette DI Extension on the newest version
  • v2.0.0 v2.0.0

    • minimal PHP version 7.0.0
    • minim Nette Framework version 2.4
    • using strict types
    • minor code improvements
  • v1.1.2 v1.1.2

    • a few fixes
    • last version supporting PHP 5
    • last version supporting Nette Framework 2.3 and older
  • v1.1.1

    • Fixed wrong period definition exception message and its requirements in README
  • v1.1.0

    • Refactored FileStorage
    • Updated dependencies to use Nette 2.3
  • v1.0.2

    • Fixed expanding of directory path for file storage
    • Fixed possibly wrong path to storage directory (containing trailing slash in directory path)
    • Added possibility to define tasks in extension section in config file
  • v1.0.1 v1.0.1

    Fixed Compiler Extension bug #33

  • v1.0.0 Finaly version 1.0.0

    • implemented critical section
    • updated compiler extension
    • updated documentation
    • fixed shifting tasks run
  • v0.8.0 Improved public API

    • removed annotating interface
    • used nette/tester for testing instead of PhpUnit
    • simplified tasks registration
    • improved notifications about task progress
bar-chart-fill

Statistics

download-cloud-fill
67466
star-fill
75
bug-fill
8
flashlight-fill
1.1y
price-tag-2-line

Badges

Componette Componette felix@nette.org