cron / cron Goto Github PK
View Code? Open in Web Editor NEWCron API
License: MIT License
Cron API
License: MIT License
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
Teste
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.
I can't really say anything about it. Are there any particular reasons why you implemented your own pattern matcher?
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.
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.
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.
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.
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?
Hi
Any reason why your class ShellJob->setCommand()
does not mirror the constructor for Symfony\Component\Process
?
Thank you
Extract the schema validation to a separate component. This way other tools can validate a schema received from third party sources.
In this line - https://github.com/Cron/Cron/blob/master/src/Cron/Schedule/CrontabSchedule.php#L90 - we can the an OR match on either the minute or the second.
Meaning, if I have the following scheduling pattern:
*/5 * * * *
so I want it to run every 5 minutes, and I trigger the main cron every minute (in the crontab file) then it'll run every minute, as the second 00 (every minute) will always trigger the minute pattern.
I'l tried get report by resolver, as
foreach ($cron->getResolver()->resolve() as $res) {
print_r($res->createReport()->getEndTime());
}
but report is empty
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
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
See dev-master commit hash in https://packagist.org/packages/cron/cron
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.
will it work or require some work?
Is really PHP 5.5.0 or above required?
Set Executor() as default executor if no specific executor has been set.
This makes the following line of code redundant in most cases (but a developer still has the option to use a different executor if they want): https://github.com/Cron/Symfony-Bundle/blob/master/Command/CronRunCommand.php#L46
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?
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
Use of \DateTimeInterface instead of \DateTime in CrontabSchedule::valid(), as the date to evaluate might not be a \DateTime object.
besides it complies with POO best pratices.
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
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....
I can't really say anything about it. Are there any particular reasons why you implemented your own pattern matcher?
Split from #18
Good day, i'm trying to run CORECTRL during boot without success.
Update the project to follow psr4.
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.
PS2.
After further checking, it follows that not only in this case, not every job is started, it happens so regardless of what setting.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These problems occurred while renovating this repository. View logs.
These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.
composer.json
php ^8.0
symfony/process ^4.0|^5.0|^6.0
phpunit/phpunit ^10.0
.github/workflows/ci.yaml
actions/checkout v4
php-actions/composer v6
php-actions/phpunit v4
.github/workflows/stale.yaml
actions/stale v9
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
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.
While trying to access crontab -e getting /var/spool/cron: chdir: Permission denied
error. Please suggest me the solution.
OS : Ubuntu 1
user : non root
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 a builder to create a schema in a readable way.
$builder
->setDayOfWeek(1)
->getSchema();
I'm very interested in using this library to schedule jobs for my job processing library. However, I had three very important questions.
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 * * * *'));
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?
<?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!
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 ?
Currently the following syntax is not supported:
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
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
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
According to this article https://tomasvotruba.com/blog/2018/02/05/how-to-run-symfony-processes-asynchronously/
We can run all cron task command once, and wait until they are finished.
It will be faster if cron/cron can do it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.