Git Product home page Git Product logo

cron's Issues

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

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?

Extract schema validation

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

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

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

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

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.

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?

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

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

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....

psr4

Update the project to follow psr4.

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.

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 v4
.github/workflows/stale.yaml
  • actions/stale v9

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

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

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.

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?

Create schema builder

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

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

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!

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 ?

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

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

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

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.