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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!