简体   繁体   English

我应该在PHP项目中使用什么来进行简单的cron作业管理?

[英]What should I use for simple cron jobs management in PHP project?

I want a simple cron-like management in PHP project there are some things I would like to have: 我想在PHP项目中进行简单的类似cron的管理,有一些我想要的东西:

  1. php job worker is just plain script that is placed in some subdir inside project directory php job worker只是简单的脚本,放在项目目录中的某个子目录中
  2. there is subtree like /cron/daily, /cron/monthly ... etc in the project root that contains all that workers 在项目根目录中有/ cron / daily,/ cron / monthly ...等子树,其中包含所有工作者
  3. there is no need to mess with crontab with every worker added. 每个工人都添加了crontab是不必要的。
  4. 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 所有脚本都由具有相应频率的run-parts运行,并且它们各自的输出被记录到单独的文件中,例如/var/log/projectname/cron/daily/somescript.log
  5. would be great to have /cron/daemon dir containing scripts that should be run forever (minutely) but no more than 1 instance 让/ cron / daemon目录包含应该永久运行(每分钟)但不超过1个实例的脚本会很棒

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. 作业是项目脚本,驻留在项目目录中,由git跟踪。
  • no need for crontab messing. 不需要crontab搞乱。
  • 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. 我只想在project / cron子目录上使用/ bin / run-parts,但是没有设法按我的意愿分割日志。 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? 所以,我只是觉得这种系统之前已经创建了很多次,是否有任何现成的PHP项目解决方案? Basically it's just some more smart run-parts equivalent. 基本上它只是一些更智能的运行部件相当于。 Should just write it once again? 应该再写一次吗?

PS There are many more job-queue specific solutions like Gearman. PS还有更多像Jobman这样的特定于作业队列的解决方案。 They are great, but this quesion is about project cron jobs lightweight solution. 他们很棒,但这个问题是关于项目cron jobs轻量级解决方案。

We've taken a slightly different approach at my current job. 我目前的工作采取了略微不同的方法。 We use Jenkins (formerly Hudson) for our PHP related scheduling needs. 我们使用Jenkins (以前的Hudson)来满足我们与PHP相关的调度需求。 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. 这很好,因为您可以利用现有的通知基础设施(jabber,电子邮件等),它与我们的代码发布的其他构建作业并列。 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. 我记录了最近我们组织PHP作业的方式,以便我们可以轻松利用CLI中的应用程序框架,这是Jenkins与作业的接口方式。

Here's the post about organizing PHP batch jobs for use with Jenkins or Hudson: 这是关于组织用于Jenkins或Hudson的PHP批处理作业的帖子:

http://blog.shupp.org/2011/03/15/organizing-php-batch-jobs/ http://blog.shupp.org/2011/03/15/organizing-php-batch-jobs/

Periodic is a CRON compatible task manager written in PHP. Periodic是一个用PHP编写的CRON兼容任务管理器 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 *'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM