I want a simple cron-like management in PHP project there are some things I would like to have:
- php job worker is just plain script that is placed in some subdir inside project directory
- there is subtree like /cron/daily, /cron/monthly ... etc in the project root that contains all that workers
- there is no need to mess with crontab with every worker added.
- all scripts are run by something like run-parts with the corresponding frequency, and their respective output is logged into separate files like /var/log/projectname/cron/daily/somescript.log
- would be great to have /cron/daemon dir containing scripts that should be run forever (minutely) but no more than 1 instance
I've had experience with that kind of scheduling system in one project and loved it. It provides a number of neat things:
- jobs are project scripts and reside in project dir, tracked by git.
- no need for crontab messing.
- logs are sorted out.
- daemons are easy to build.
I would just use /bin/run-parts on project /cron subdirs, but didn't manage to split logs as I wanted. And splitted logging is very nice feature to have.
So, I just thought this kind of systems were created many times before, is there any ready to use solution for PHP project? Basically it's just some more smart run-parts equivalent. Should just write it once again?
P.S. There are many more job-queue specific solutions like Gearman. They are great, but this quesion is about pr开发者_如何学JAVAoject cron jobs lightweight solution.
We've taken a slightly different approach at my current job. We use Jenkins (formerly Hudson) for our PHP related scheduling needs. It's nice because you can leverage the existing infrastructure for notifications (jabber, email, etc), and it sits along side our other build jobs for code releases. There's also the ability to watch console output in real time, get transcripts of every run, etc.
I documented the way we organize our PHP jobs recently so that we can easily leverage our application framework from CLI, which is how Jenkins interfaces with the jobs.
Here's the post about organizing PHP batch jobs for use with Jenkins or Hudson:
http://blog.shupp.org/2011/03/15/organizing-php-batch-jobs/
Periodic is a CRON compatible task manager written in PHP. In order to make it work like you desire it, there will still some work to be done, but it should give you a good basis.
Use this function:
function parse_crontab($time, $crontab)
{$time=explode(' ', date('i G j n w', strtotime($time)));
$crontab=explode(' ', $crontab);
foreach ($crontab as $k=>&$v)
{$v=explode(',', $v);
foreach ($v as &$v1)
{$v1=preg_replace(array('/^\*$/', '/^\d+$/', '/^(\d+)\-(\d+)$/', '/^\*\/(\d+)$/'),
array('true', $time[$k].'===\0', '(\1<='.$time[$k].' and '.$time[$k].'<=\2)', $time[$k].'%\1===0'),
$v1
);
}
$v='('.implode(' or ', $v).')';
}
$crontab=implode(' and ', $crontab);
return eval('return '.$crontab.';');
}
var_export(parse_crontab('2011-05-04 02:08:03', '*/2,3-5,9 2 3-5 */2 *'));
var_export(parse_crontab('2011-05-04 02:08:03', '*/8 */2 */4 */5 *'));
精彩评论