Zhong - 一个基于 Redis 的分布式 Cron(定时任务)服务

Useful, reliable distributed cron. Tired of your cron-like scheduler running key jobs twice? Would you like to be able to run your cron server on multiple machines and have it "just work"? Have we got the gem for you.

Zhong uses Redis to acquire exclusive locks on jobs, as well as recording when they last ran. This means that you can rest easy at night, knowing that your customers are getting their monthly Goat Fancy magazine subscriptions and you are rolling around in your piles of money without a care in the world.

Installation

gem 'zhone'

Usage

Zhong.redis = Redis.new(url: ENV["ZHONG_REDIS_URL"])

Zhong.schedule do
  category "stuff" do
    every 5.seconds, "foo" do
      puts "foo"
    end

    every(1.minute, "biz", at: ["**:26", "**:27"]) { puts "biz" }
    every(1.week, "baz", at: ["mon 22:45", "wed 23:13"]) { puts "baz" }
    every(10.seconds, "boom") { raise "fail" }
  end

  category "clutter" do
    every(1.second, "compute", if: -> (t) { t.wday == 3 && rand < 0.5 }) do
      puts "something happened on wednesday, maybe"
    end
  end

  # note: callbacks that explicitly false will cause event to not run
  on(:before_tick) do
    puts "ding"
    true
  end

  on(:after_tick) do
    puts "dong"
  end

  on(:before_run) do |job, time|
    puts "running #{job}"
    true # can conditionally run a specific job
  end

  on(:after_run) do |job, time, ran|
    puts "#{job} ran?: #{ran}"
  end

  on(:before_disable) do |job|
    puts "#{job} is going to be disabled"
  end

  on(:after_disable) do |job|
    puts "#{job} disabled"
  end

  on(:before_enable) do |job|
    puts "#{job} is going to be enabled"
  end

  on(:after_enable) do |job|
    puts "#{job} enabled"
  end

  error_handler do |e, job|
    puts "dang, #{job} messed up: #{e}"
  end
end

https://github.com/nickelser/zhong/

2 个赞
评论列表
9592
tangmonk 发表于 2017年06月21日

不错

3035
IChou 发表于 2017年04月19日

请问有谁造这个靠谱么?上生产有无风险 准备用来替换 whenever

2099
hz_qiuyuanxin 发表于 2017年04月20日

我不认可这种设计,因为我更加倾向于

  1. 像 whenever, clockwork 这一些只是负责触发定时任务的产生,不负责具体任务的运行
  2. 用队列的方式区分,让不同的机器运行不同的任务,或者混合交叉都可以
9800
pynix 发表于 2017年04月22日

Zhong=钟?

3035
IChou 发表于 2017年05月31日

呃 看了下 依赖 activesupport 我觉得若没有很大的必要还是把这个移除掉吧