PHP 项目中的简单 cron 作业管理应该使用什么?

发布于 2024-10-16 23:02:01 字数 754 浏览 7 评论 0原文

我想要在 PHP 项目中进行简单的类似 cron 的管理 有一些我想要的东西:

  1. php job worker 只是简单的脚本,放置在项目目录内的某个子目录中,
  2. 项目根目录中有像 /cron/daily、/cron/monthly ... 等子树,其中包含所有内容工作人员
  3. 没有必要在添加每个工作人员时搞乱 crontab。
  4. 所有脚本都由类似 run-parts 的东西以相应的频率运行,并且它们各自的输出被记录到单独的文件中,例如 /var/log/projectname/cron/daily/somescript.log
  5. 如果 /cron/daemon 目录包含应该永远(每分钟)运行但不超过 1 个实例的脚本

我在一个项目中体验过这种调度系统,并且喜欢它。它提供了许多巧妙的东西:

  • 作业是项目脚本,驻留在项目目录中,由 git 跟踪。
  • 不需要搞乱 crontab。
  • 日志已整理。
  • 守护进程很容易构建。

我只会在项目 /cron 子目录上使用 /bin/run-parts ,但无法按照我想要的方式分割日志。分割日志记录是一个非常好的功能。

所以,我只是想这种系统以前已经创建过很多次了,有没有现成的PHP项目解决方案?基本上它只是一些更智能的运行部件的等效物。是不是应该再写一遍?

PS 还有很多作业队列特定的解决方案,例如 Gearman。他们很棒,但这个问题是关于项目 cron jobs 轻量级解决方案。

I want a simple cron-like management in PHP project
there are some things I would like to have:

  1. php job worker is just plain script that is placed in some subdir inside project directory
  2. there is subtree like /cron/daily, /cron/monthly ... etc in the project root that contains all that workers
  3. there is no need to mess with crontab with every worker added.
  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
  5. 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 project cron jobs lightweight solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-10-23 23:02:01

我们在我目前的工作中采取了略有不同的方法。我们使用 Jenkins(以前称为 Hudson)来满足 PHP 相关的调度需求。这很好,因为您可以利用现有的基础设施进行通知(jabber、电子邮件等),并且它与我们用于代码发布的其他构建作业并存。还有实时观察控制台输出、获取每次运行的记录等的能力。

我最近记录了我们组织 PHP 作业的方式,以便我们可以轻松地从 CLI 中利用我们的应用程序框架,这就是 Jenkins 与作业交互的方式。

以下是关于组织 PHP 批处理作业以与 Jenkins 或 Hudson 一起使用的帖子:

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

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/

守望孤独 2024-10-23 23:02:01

Periodic 是一个用 PHP 编写的 CRON 兼容任务管理器。为了使其按照您的意愿工作,仍然需要做一些工作,但它应该为您提供良好的基础。

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.

一杆小烟枪 2024-10-23 23:02:01

使用这个函数:

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 *'));

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 *'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文