nickelser / zhong Goto Github PK
View Code? Open in Web Editor NEWReliable, distributed cron.
License: Other
Reliable, distributed cron.
License: Other
Sidekiq-cron https://github.com/ondrejbartas/sidekiq-cron has a nice UI that allows to disable but also to run immediately.
You only have 1 real executable. The setup and console files in your bin directory are not important and should not be identified in your gemspec as globally executable.
How hard would it be to configure the background job framework? For example, would it be terribly difficult to support DelayedJob?
Hello Nick,
I have this error "unable to run due to client error: Suo::LockClientError" when trying to run zhong on our staging server (redis is located on different instance). Locally everything works fine.
Could it be some permission/configuration missing?
Thank you,
Alex
The idea sounds great, but more in-depth technical details would help.
nohup bundle exec bin/zhong
?All these details have operational implications, so the more information the better I think.
The README provides an example of modifying rails' router to mount a Zhong UI.
However, following the instructions gives a dependency error because zhong/web requires sinatra/base.
The naive user (me) adds sinatra-base to their Gemfile, at which point bundler complains that sinatra-base (1.4.0) and rails (5.1.5) have conflicting rack version requirements.
In fact, the user needs to add sinatra to their Gemfile, not sinatra-base. Probably worth clarifying in the README.
Testing an upgrade one of our app to Rails 7.0.1
We noticed a crash when starting zhong process (see below for stack trace)
This seems related to this issue on Rails.
I've tested manually to add a
require "active_support"
in zhong.rb and it seems to work, but I'm not sure it's the way to go.
bundle exec zhong zhong.rb
You can remove `require 'dalli/cas/client'` as this code has been rolled into the standard 'dalli/client'.
/Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/xml_mini.rb:184:in `current_thread_backend': uninitialized constant ActiveSupport::XmlMini::IsolatedExecutionState (NameError)
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/xml_mini.rb:103:in `backend='
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/xml_mini.rb:201:in `<module:ActiveSupport>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/xml_mini.rb:11:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/core_ext/array/conversions.rb:3:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/core_ext/array/conversions.rb:3:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/duration.rb:3:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/duration.rb:3:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/core_ext/time/calculations.rb:3:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/core_ext/time/calculations.rb:3:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/core_ext/time.rb:4:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/core_ext/time.rb:4:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/time.rb:12:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/activesupport-7.0.1/lib/active_support/time.rb:12:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/zhong-0.3.0/lib/zhong.rb:7:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/zhong-0.3.0/lib/zhong.rb:7:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/zhong-0.3.0/bin/zhong:5:in `require'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/gems/zhong-0.3.0/bin/zhong:5:in `<top (required)>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/bin/zhong:23:in `load'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/bin/zhong:23:in `<main>'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/bin/ruby_executable_hooks:22:in `eval'
from /Users/davidbourguignon/.rvm/gems/ruby-3.0.1@drivy-rangers/bin/ruby_executable_hooks:22:in `<main>'
Using version 0.2.4 of the gem, I had this configuration:
every 1.day, 'call Foo', at: ['05:00', '08:00', '11:00'] do
Foo.run( )
end
Foo.run( ) was only called at 5:00 am.
Do I need to change "every 1.day". to "every 1.hour" ?
Or is there a bug when using multiple times?
We have been running Zhong for months; occasionally, all of a sudden the process just stops without warning, no memory or dyno load issues, exceptions or anything.
We have added this to try to get more information. Any other suggestions, has anyone else seen this behavior?
on(:before_run) do |job, time|
puts "starting: #{job}, lock_key: #{job.lock_key}"
end
on(:after_run) do |job, time, ran|
puts "finished: #{job} ran: #{ran}, lock_key: #{job.lock_key} "
end
Notice in the logs we see starting/finishing then just heroku samples
From @gmccreight:
Hey Nick,
I'm not sure how big of a deal this is, but if you define:
every 1.day, 'something at a particular time', at: '16:15' do
Rails.logger.info "something at a particular time in process #{Process.pid}"
end
then, later on you change that to
every 1.day, 'something at a particular time', at: '18:15' do
Rails.logger.info "something at a particular time in process #{Process.pid}"
end
it doesn't run at 18:15.
We will need to store the desired run time, and then expire the last run if the desired run time in storage does not match the desired run time in code
What this means in zhong: every 1.minute, 'foobar', at: '**:20'
For me it's means that I want to run it each minute in whatever hour, but only when it's 20 minutes, but I see it running every minute in the console:
web_1 | 20:34:19 web.1 | 2017-11-10 20:34:19 +0000: running: foobar
web_1 | 20:34:19 web.1 | "hey joe!!!"
web_1 | 20:35:19 web.1 | 2017-11-10 20:35:19 +0000: running: foobar
web_1 | 20:35:19 web.1 | "hey joe!!!"
Suddenly it stopped running every 1 minute and I see this on '/zhong':
Would love to get an explanation about what is going on.
Once I add the zhong stuff to my routes file, my application breaks. Any insight?
routes.rb
Rails.application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
require 'zhong/web'
mount Zhong::Web => "/zhong"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "admin/desks#index"
end
➜ ZD-DataCollector git:(zhong) ✗ rails s
=> Booting Puma
=> Rails 5.0.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Exiting
/Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `require': cannot load such file -- sinatra/base (LoadError)
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `block in require'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `require'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/zhong-0.2.2/lib/zhong/web.rb:3:in `<top (required)>'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `require'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `block in require'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `require'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config/routes.rb:8:in `block in <top (required)>'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:389:in `instance_exec'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:389:in `eval_block'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/actionpack-5.0.2/lib/action_dispatch/routing/route_set.rb:371:in `draw'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config/routes.rb:1:in `<top (required)>'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:40:in `each'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/activesupport-5.0.2/lib/active_support/file_update_checker.rb:77:in `execute'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:27:in `updater'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application/finisher.rb:119:in `block in <module:Finisher>'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `run'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
from /Users/edahl/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/application.rb:352:in `initialize!'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config/environment.rb:5:in `<top (required)>'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config.ru:3:in `require_relative'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config.ru:3:in `block in <main>'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/builder.rb:55:in `instance_eval'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/builder.rb:55:in `initialize'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config.ru:in `new'
from /Users/edahl/Documents/ruby/rails/ZD-DataCollector/config.ru:in `<main>'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/builder.rb:49:in `eval'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/builder.rb:49:in `new_from_string'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/builder.rb:40:in `parse_file'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/server.rb:318:in `build_app_and_options_from_config'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/server.rb:218:in `app'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/server.rb:59:in `app'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/rack-2.0.1/lib/rack/server.rb:353:in `wrapped_app'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/server.rb:124:in `log_to_stdout'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/server.rb:77:in `start'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:90:in `block in server'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:85:in `tap'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:85:in `server'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/edahl/.rvm/gems/ruby-2.3.1@ZD-DataCollector/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'```
zhong.rb: https://gist.github.com/lefty313/ac57df1dfd97133c9672c8f9f0e77540
Most of my zhong jobs are spawning sidekiq background process, it works fine since months
as you see single job is schedule once per minute
recently I added following non sidekiq job to my zhong configuration
every 15.seconds, "check access matches" do
min5 = $redis.zcount("matches:accessed", 5.minutes.ago.to_i, "+inf")
min25 = $redis.zcount("matches:accessed", 25.minutes.ago.to_i, "+inf")
hour1 = $redis.zcount("matches:accessed", 1.hour.ago.to_i, "+inf")
hour24 = $redis.zcount("matches:accessed", 24.hour.ago.to_i, "+inf")
week = $redis.zcount("matches:accessed", 7.days.ago.to_i, "+inf")
total = $redis.zcount("matches:accessed", "-inf", "+inf")
NewRelic::Agent.record_metric("Database/MatchAccess/Last5Min", min5)
NewRelic::Agent.record_metric("Database/MatchAccess/Last25Min", min25)
NewRelic::Agent.record_metric("Database/MatchAccess/LastHour", hour1)
NewRelic::Agent.record_metric("Database/MatchAccess/LastDay", hour24)
NewRelic::Agent.record_metric("Database/MatchAccess/LastWeek", week)
NewRelic::Agent.record_metric("Database/MatchAccess/Alltime", total)
end
as you see it's not reporting at time to time. I can fix it by calling
$redis.del($redis.keys("zhong*"))
but than it will lock after few minutes again. During lock phase zhong is still working fine for other jobs.
Am I doing something wrong here ?
ps: I have autoscalling policy on my production server so one of my servers could be killed when zhong is working. Keep in mind that zhong is running on every single instance and there is always at least one
Say I have a job scheduled to run at 3 am each night. And for whatever reason, there's not a running Ruby process at exactly 3 am -- say it stops at 2:59 and resumes at 3:01.
Does zhong provide a way to automatically run that 3 am job if that scenario were to happen?
Oops
Looks like you lifted a significant amount of LGPL code from Sidekiq. You will need to rewrite that code or relicense as LGPL.
The README.md
to this project suggests that all you have to do is set up Redis, evaluate a Zhong.schedule
call, and boom! the job happens as specified. But there is a missing step.
For example, I ran the following in a Rails console running Instacart's own catalog
app, which uses Zhong in production. If Zhong "just works" with no configuration whatsoever, evaluating the following should result in a string being printed to the terminal every second:
Zhong.schedule do
every 1.seconds, 'This should do something' do
puts 'Hello!'
end
end
Instead of seeing anything printed to STDOUT, that expression just returns some object and does nothing else.
Zhong does absolutely nothing until the following is evaluated from somewhere:
Zhong.start
There is a bin/zhong
script included with this Gem that does almost nothing but this.
As demonstrated by Travis job https://travis-ci.org/nickelser/zhong/jobs/255642412.
# will return "300 seconds" instead of "5 minutes"
Zhong::Every.parse(5.minutes).to_s
I love the idea of this gem.
In my config/initializers/zhong.rb I did:
Zhong.schedule do
every 5.seconds, 'MyJob' do
MyJob.perform_later
end
end
However when I run zhong I get MyJob failed: uninitialized constant MyJob
. MyJob
is located in app/jobs/...
I can do hacky things to load but has anyone tried this and hopefully come up w a better solution? What would be the best way to autoload Rails classes?
Hi thanks for the gem.
I want to use zhong in my company for a rails project already in production.
I see the gem is still on 0.2.1, is it stable enough to use in production?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.