dspows0637 2010-10-20 14:55
浏览 15
已采纳

MySQL多服务器任务回退机制

I have two web servers. Server 'A' and server 'B'. Both are exact duplicate of the other and are connected to a load-balancer for serving clients.

I have a tasks (cronjob) that is executed at every hour, every day. This task is currently running only on server 'A'. The problem is that this tasks is really important and if server 'A' crash for some reason the task will no longer run.

I would like server 'B' to execute the task but only if server 'A' didn't. Both tasks are PHP scripts interacting with a MySQL database.

I'm thinking about using some rows in a table (using MySQL locks?) as a flag for the server 'B' to start his task if server 'A' didn't do it and vice versa.

Please note that the task file on both servers must be exactly the same.

Thank you.

  • 写回答

1条回答 默认 最新

  • dpv46227 2010-10-20 16:34
    关注

    Your proposed solution is one I often employ for simpler tasks, usually with something like:

    • cronjob is started on both servers alike
    • both get different tokens (something based on server-id & process-id usually)
    • both try to claim a job in a table with UPDATE jobs SET process = '$token' WHERE process IS NULL (+ some other clauses not to the point right now).
    • both check for processes won/claimed: SELECT * FROM jobs WHERE process = '$token'
    • only the 'winner' performs the job, loser / jobless process just exits.

    Something to keep in mind though that this only works for jobs as long as the MySQL instance is up: no processing can be done without it. Then again, a cronjob which checks whether the MySQL server is alive can be performed on both servers, I don't mind to get 2 alerts for such a major catastrophe :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题