GithubHelp home page GithubHelp logo

cron's Introduction

Cron

Packagist Build status Quality Coverage Packagist License

This library enables you to have only one general crontab entry that will trigger several different cronjobs that can be defined through this library. The Cron library will decide if the job needs to run or not.

Attention: make sure you set the server crontab job to a correctly chosen frequency because if there is for example a cronjob defined in code to run every minute, your general crontab job needs to run at least every minute as well to work properly.

Use Case

Say you need two cronjobs in your application. One that will write the contents of a folder to a log file, and one that will empty the folder. This library enables you to create separate scripts (for example: cron.php) where you notify the Cron library of the two cronjobs. After defining the Jobs with their specifics, they can be added to the resolver and the run command can be given.

Your server crontab could now look something like:

* * * * * /path/to/php /path/to/cron.php >/dev/null 2>&1

The code example below is matched to this use case.

Code example

<?php

require_once __DIR__ . '/vendor/autoload.php';

// Write folder content to log every five minutes.
$job1 = new \Cron\Job\ShellJob();
$job1->setCommand('ls -la /path/to/folder');
$job1->setSchedule(new \Cron\Schedule\CrontabSchedule('*/5 * * * *'));

// Remove folder contents every hour.
$job2 = new \Cron\Job\ShellJob();
$job2->setCommand('rm -rf /path/to/folder/*');
$job2->setSchedule(new \Cron\Schedule\CrontabSchedule('0 0 * * *'));

$resolver = new \Cron\Resolver\ArrayResolver();
$resolver->addJob($job1);
$resolver->addJob($job2);

$cron = new \Cron\Cron();
$cron->setExecutor(new \Cron\Executor\Executor());
$cron->setResolver($resolver);

$cron->run();

Cron currently only support triggering shell commands. This means you can trigger anything although it is highly encouraged not to call web urls. But if you really need to here are some example commands.

* * * * * /usr/bin/lynx -source http://example.com/cron.php
* * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php
* * * * * curl -s http://example.com/cron.php

Installation

Add the following to your project's composer.json:

$ composer require cron/cron
{
    "require": {
        "cron/cron": "^1.0"
    }
}

Crontab syntax

A CRON expression is a string representing the schedule for a particular command to execute. The parts of a CRON schedule are as follows:

*    *    *    *    *    *
-    -    -    -    -    -
|    |    |    |    |    |
|    |    |    |    |    + year [optional]
|    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
|    |    |    +---------- month (1 - 12)
|    |    +--------------- day of month (1 - 31)
|    +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)

Each of the parts supports wildcards (*), ranges (2-5) and lists (2,5,6,11).

Contributing

All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code.

Fork the project, create a feature branch, and send us a pull request.

To ensure a consistent code base, you should make sure the code follows the Coding Standards which we borrowed from Symfony. Make sure to check out php-cs-fixer as this will help you a lot.

If you would like to help, take a look at the list of issues.

Requirements

PHP 5.5.0 or above

Author and contributors

Dries De Peuter - [email protected] - http://nousefreak.be

See also the list of contributors who participated in this project.

License

Cron is licensed under the MIT license.

cron's People

Contributors

bukashk0zzz avatar davementens avatar etanec avatar grachevko avatar hallowelt42 avatar iamdevice avatar jeroeny avatar nousefreak avatar ozmodiar avatar renovate[bot] avatar samnela avatar surya4crowde avatar tjorvenb avatar wouter-toppy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cron's Issues

Extract shell job to a separate package

It is always a good idea in case of pluggable systems like this, to extract as many components as possible, so that the main package can provide a stable contract with as less changing part (aka. interfaces, code that is possibly not changing, like exceptions, main entry points for plugins, etc) as possible.

Split from #18

Event reporters

I would consider converting the reporters to an event-driven logic. That would allow other tasks (like logging). What do you think?

Split from #18

ShellJob->setCommand() fails

Hi, I got and error:

'Passing a command as string when creating a "Symfony\Component\Process\Process" instance is deprecated since Symfony 4.2, pass it as an array of its arguments instead, or use the "Process::fromShellCommandline()" constructor if you need features provided by the shell'

#0 error_handler()
#1 trigger_error() called at [../vendor/symfony/process/Process.php:147]
#2 Symfony\Component\Process\Process->__construct() called at [../vendor/cron/cron/src/Job/ShellJob.php:30]

Fixed it with replace $this->process = new Process($command); to $this->process = Process::fromShellCommandline($command); in Cron\Job\ShellJob

Remove interface suffix

This is something that I asked in case of many-many libraries with fear, because I don't like messing with authors personal preferences, but in most cases people welcomed my proposal. Interface suffixes is not PHP specific, unfortunately it is present in many other languages as well.

I usually link Mathias Verraes's blog post about this topic because he lists every argument against interface suffixes.

http://verraes.net/2013/09/sensible-interfaces/

My opinion is that it makes the library more readable from a programmer point of view. The lazy ones, how doesn't care about reading source code (and neither docs in most cases), just want to use it would say otherwise.

Split from #18

i gave this syntax for cron "17 17 18 2 5" but didnt execute my fucntion at that time, but "* * * * *" this one is working

var CronJob = require("cron").CronJob;

const SendEmail = (payload) => {
console.log("SENDING EMAIL", payload);
};

const ScheduleCronJob = (payload, cronTime) => {
console.log("ScheduleCronJob.........", cronTime);
const job = new CronJob(cronTime, () => {
console.log("You will see this message every second");
SendEmail(payload);
job.stop();
});
return job;
};
const payload = { name: "sarath" };
const job = ScheduleCronJob(payload, "17 17 18 2 5");
job.start();

what it does is to execute the function and stop it

Question: Removing & Adding Jobs

I'm very interested in using this library to schedule jobs for my job processing library. However, I had three very important questions.

1. Consider the following:

The command, $cron->run();, is running in the background using Cron.

Question:
If this is the case then can you add a job to Cron from a different file? Such as having another class add a job using the following code:

$job1 = new \Cron\Job\ShellJob();
$job1->setCommand('ls -la /path/to/folder');
$job1->setSchedule(new \Cron\Schedule\CrontabSchedule('*/5 * * * *'));

2. Consider the following:

Environment is same as question 1.

Question:
If there is a job added to execute every 1 minute, how would one remove the job from the execution list?

3. If you wanted to run a worker to execute the library in the background every 5 seconds, is the following code sufficient enough?

<?php

require_once(__DIR__ . '/vendor/autoload.php');

$cron = new \Cron\Cron();
$cron->setExecutor(new \Cron\Executor\Executor());
$cron->setResolver($resolver);

$cron->run();

Thank you in advanced for all your help!

Create schema builder

Create a builder to create a schema in a readable way.

$builder
  ->setDayOfWeek(1)
  ->getSchema();

Separate spec and integration tests

PHPUnit is great, but spec testing is simply better to make sure your code behaves exactly you want. So I usually suggest writting spec tests for code (where possible). In a builder like context (like this is) it is absolutely possible. Looking at the external dependencies, mocking is also possible.

Split from #18

Complete report handing

Reports hold references to the started processes.

A CronReport is returned and has the option to check if the Job is still running. This add the ability for the users to wait for the jobs to finish.

Docker support

Hello,
I used your code example in a CentOS container, and in a Debian one, but for an unknown reason, it doesn't work.
I just wrote a command like that before adding it to crontab :

$ php exampleCron.php

But it works for my physical system, any ideas ?

Weekday "7" not accepted

I try to use "Sunday" as weekday "7" but this isn't accepted. According to the documentation this is a valid value but an exception is thrown.
What is correct? The documentation or the exception?

psr4

Update the project to follow psr4.

Extract schema validation

Extract the schema validation to a separate component. This way other tools can validate a schema received from third party sources.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Unsupported composer value

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

composer
composer.json
  • php ^8.0
  • symfony/process ^4.0|^5.0|^6.0
  • phpunit/phpunit ^10.0
github-actions
.github/workflows/ci.yaml
  • actions/checkout v4
  • php-actions/composer v6
  • php-actions/phpunit v3
.github/workflows/stale.yaml
  • actions/stale v9

  • Check this box to trigger a request for Renovate to run again on this repository

How to get generated report for job?

I'l tried get report by resolver, as

        foreach ($cron->getResolver()->resolve() as $res) {
            print_r($res->createReport()->getEndTime());
        }

but report is empty

Need to wait for processes to stop (call job->wait())

I tried the example of the Readme, and I've found that the main thread should wait for its children to finish, because if not, the children get killed at the end of the parent.

I've done a workaround extending the ShellJob class to wait them:

class FixShellJob extends \Cron\Job\ShellJob
{
    public function run(\Cron\Report\JobReport $report)
    {
        $this->report = $report;
        $report->setStartTime(microtime(true));
        $process = $this->getProcess();
        $process->start(function ($type, $buffer) use ($report) {
            if (Process::ERR === $type) {
                $report->addError($buffer);
            } else {
                $report->addOutput($buffer);
            }
        });
        // FIXME this is necessary to not be killed when parent thread ends
        $process->wait();
    }
}

$job1 = new FixShellJob();
$job1->setCommand('touch /tmp/test');
$job1->setSchedule(new \Cron\Schedule\CrontabSchedule('* * * * *'));

In the Symfony Process doc it mentions that when running asynchronously, you can do more stuff, but you should wait for the children to finish before finishing the parent: http://symfony.com/doc/current/components/process.html#running-processes-asynchronously

Cron does not always start job with pattern * * * * *

If I add ShellJob where setSchedule() is set to run every minute, the job is sometimes started every 2 or even once every 5 minutes.

I do not know if this information is enough, if I have questions, I will be happy to answer them.

PS.
It looks as if jobs were run randomly between 1 - 10 minutes.
image

PS2.
After further checking, it follows that not only in this case, not every job is started, it happens so regardless of what setting.

Add job queue plugin (async executor)

In some cases long running tasks can overlap. This is my biggest problem with most cron runners. In these cases a queue would be a good choice, where only one job is executed at a time and the others wait for it (and are not getting lost)

Of course this should be plugin, not part of the main package.

Split from #18

My script does not work

I'm using "cron/cron": "1.0.5".
I wrote the following code inside a script callable by cli:

        $job1 = new \Cron\Job\ShellJob();
        $job1->setCommand('touch myfile');
        $job1->setSchedule(new \Cron\Schedule\CrontabSchedule('* * * * *'));

        $resolver = new \Cron\Resolver\ArrayResolver();
        $resolver->addJob($job1);

        $cron = new \Cron\Cron();
        $cron->setExecutor(new \Cron\Executor\Executor());
        $cron->setResolver($resolver);

        $report = $cron->run();

I run this script in cli (also with root user) and nothing seems to happened (I expect to see myfile).

So I decided to dump the job output

foreach ($report->getReports() as $jobReport) {
     var_dump($jobReport->isSuccessful(), $jobReport->getOutput());
}

and the result of this dump is:

NULL
array(0) {
}

I don't think to do something wrong, also because I copied and pasted the example in the doc.

Have you any idea about my issue?

Cron and the time zone

Hello,
Is it possible to set the time zone against which the cron pattern will be checked?
If not, will it be added in subsequent versions?

For example to be possible set some like this:
5 2 3 * * TZ="America/New_York" /do/command > /dev/null 2>&1

Thanks

Improvement ideas

I was recently developping a cron package just like yours. I usually implement everything that I need and look around afterwards and see how existing implementations compare to that.

Your library and my needs (what I have done so far) are pretty much the same, but there are a few differences, so I wanted to reach you out and ask if you are open to my suggestions. If yes, I am happy to provide a PR with all the changes.

Actually most of them contains some workflow/package quality related concerns.

Use Michael Dowling's Cron expression engine

I can't really say anything about it. Are there any particular reasons why you implemented your own pattern matcher?

Remove interface suffix

This is something that I asked in case of many-many libraries with fear, because I don't like messing with authors personal preferences, but in most cases people welcomed my proposal. Interface suffixes is not PHP specific, unfortunately it is present in many other languages as well.

I usually link Mathias Verraes's blog post about this topic because he lists every argument against interface suffixes.

http://verraes.net/2013/09/sensible-interfaces/

My opinion is that it makes the library more readable from a programmer point of view. The lazy ones, how doesn't care about reading source code (and neither docs in most cases), just want to use it would say otherwise.

Separate spec and integration tests

PHPUnit is great, but spec testing is simply better to make sure your code behaves exactly you want. So I usually suggest writting spec tests for code (where possible). In a builder like context (like this is) it is absolutely possible. Looking at the external dependencies, mocking is also possible.

Add job queue plugin (async executor)

In some cases long running tasks can overlap. This is my biggest problem with most cron runners. In these cases a queue would be a good choice, where only one job is executed at a time and the others wait for it (and are not getting lost)

Of course this should be plugin, not part of the main package.

Extract shell job to a separate package

It is always a good idea in case of pluggable systems like this, to extract as many components as possible, so that the main package can provide a stable contract with as less changing part (aka. interfaces, code that is possibly not changing, like exceptions, main entry points for plugins, etc) as possible.

Event reporters

I would consider converting the reporters to an event-driven logic. That would allow other tasks (like logging). What do you think?

I think that all the point above improves the project's general quality. What do you think?

Provide a solution to output logging

The job in the example captures it's output to it's own logfile. This is something that could/should be handled by the library.

Add an optional per job LogHandler and a global Cron LogHandler. All output should be logged to the LogHandler.

Note: Unless I missed something this means the script keeps running until all jobs finish.

Issue or Question: how to know a cron is currently running

I set my cronJobs during the initialization of the app (one has to go to myapp/install)

I would like to know: how can I be sure that the cronJob has been set and is running, in case someone goes again to myapp/install ?

sudo -u php -H crontab -l 

is empty....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.