dongmou5628 2011-06-23 17:11
浏览 39
已采纳

负载平衡Web服务器上的Cron任务

I'm looking for better solution to handling our cron tasks in a load balanced environment.

Currently have:

  • PHP application running on 3 CentOS servers behind a load balancer.
  • Tasks that need to be run periodically but only on a single machine at a time.
  • Good old cron set up to run those tasks on the first server.
  • Problems if the first server is out of play for whatever reason.

Looking for:

  • Something more robust and de-centralized.
  • Load balancing the tasks so multiple tasks would run only once but on random/different servers to spread the load.
  • Preventing not having the tasks run when the first server goes down.
  • Being able to manage tasks and see aggregate reports ideally using a web interface.
  • Notifications if anything goes wrong.

The solution doesn't need to be implemented in PHP but it would be nice as it would allow us to easily tweak it if needed.

I have found two projects that look promissing. GNUBatch and Job Scheduler. Will most likely further test both but I wonder if someone has better solution for the above.

Thanks.

  • 写回答

3条回答 默认 最新

  • douquanjie9326 2013-12-05 02:11
    关注

    You can use this small library that uses redis to create a temporary timed lock:

    https://github.com/AlexDisler/MutexLock

    The servers should be identical and have the same cron configuration. The server that will be first to create the lock will also execute the task. The other servers will see the lock and exit without executing anything.

    For example, in the php file that executes the scheduled task:

    MutexLock\Lock::init([
      'host'   => $redisHost,
      'port'   => $redisPort
    ]);
    
    // check if a lock was already created,
    // if it was, it means that another server is already executing this task
    if (!MutexLock\Lock::set($lockKeyName, $lockTimeInSeconds)) {
      return;
    }
    
    // if no lock was created, execute the scheduled task
    scheduledTaskThatRunsOnlyOnce();
    

    To run the tasks in a de-centralized way and spread the load, take a look at: https://github.com/chrisboulton/php-resque It's a php port of the ruby version of resque and it stores the data in the same exact format so you can use https://github.com/resque/resque-web or http://resqueboard.kamisama.me/ to monitor the workers and see reports

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助