Class: RabbitJobs::Scheduler
- Inherits:
-
Object
- Object
- RabbitJobs::Scheduler
- Includes:
- MainLoop
- Defined in:
- lib/rabbit_jobs/scheduler.rb
Overview
Scheduler daemon.
Instance Attribute Summary collapse
-
#process_name ⇒ Object
Returns the value of attribute process_name.
-
#schedule ⇒ Object
Returns the value of attribute schedule.
Instance Method Summary collapse
-
#clear_schedule! ⇒ Object
Stops old rufus scheduler and creates a new one.
- #load_default_schedule ⇒ Object
-
#load_schedule! ⇒ Object
Pulls the schedule from Resque.schedule and loads it into the rufus scheduler instance.
-
#publish_from_config(config) ⇒ Object
Publish a job based on a config hash.
-
#rails_env_matches?(config) ⇒ Boolean
Returns true if the given schedule config hash matches the current ENV.
- #rufus_scheduler ⇒ Object
- #setup_job_schedule(name, config) ⇒ Object
-
#work ⇒ Object
Subscribes to channel and working on jobs.
Methods included from MainLoop
#log_daemon_error, #main_loop, #shutdown, #shutdown!, #startup
Instance Attribute Details
#process_name ⇒ Object
Returns the value of attribute process_name.
6 7 8 |
# File 'lib/rabbit_jobs/scheduler.rb', line 6 def process_name @process_name end |
#schedule ⇒ Object
Returns the value of attribute schedule.
6 7 8 |
# File 'lib/rabbit_jobs/scheduler.rb', line 6 def schedule @schedule end |
Instance Method Details
#clear_schedule! ⇒ Object
Stops old rufus scheduler and creates a new one. Returns the new rufus scheduler
57 58 59 60 61 |
# File 'lib/rabbit_jobs/scheduler.rb', line 57 def clear_schedule! rufus_scheduler.stop @rufus_scheduler = nil rufus_scheduler end |
#load_default_schedule ⇒ Object
97 98 99 100 101 102 |
# File 'lib/rabbit_jobs/scheduler.rb', line 97 def load_default_schedule return unless defined?(Rails) file = Rails.root.join('config/schedule.yml') return unless file.file? @schedule = HashWithIndifferentAccess.new(YAML.load_file(file)) end |
#load_schedule! ⇒ Object
Pulls the schedule from Resque.schedule and loads it into the rufus scheduler instance
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rabbit_jobs/scheduler.rb', line 15 def load_schedule! @schedule ||= load_default_schedule fail 'You should setup a schedule or place it in config/schedule.yml' unless schedule schedule.each do |name, config| # If rails_env is set in the config, enforce ENV['RAILS_ENV'] as # required for the jobs to be scheduled. If rails_env is missing, the # job should be scheduled regardless of what ENV['RAILS_ENV'] is set # to. if config['rails_env'].nil? || rails_env_matches?(config) setup_job_schedule(name, config) end end end |
#publish_from_config(config) ⇒ Object
Publish a job based on a config hash
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rabbit_jobs/scheduler.rb', line 39 def publish_from_config(config) args = config[:args] || [] klass_name = config[:class] params = [args].flatten RabbitJobs.publish_to(config[:queue], klass_name, *params) RabbitJobs.logger.info "Published: #{config} at #{Time.now}" rescue RabbitJobs.logger.warn "Failed to publish #{klass_name}:\n #{$!}\n params = #{params.inspect}" RabbitJobs.logger.error $! end |
#rails_env_matches?(config) ⇒ Boolean
Returns true if the given schedule config hash matches the current ENV
32 33 34 35 36 |
# File 'lib/rabbit_jobs/scheduler.rb', line 32 def rails_env_matches?(config) config['rails_env'] && ENV['RAILS_ENV'] && config['rails_env'].gsub(/\s/, '').split(',').include?(ENV['RAILS_ENV']) end |
#rufus_scheduler ⇒ Object
51 52 53 |
# File 'lib/rabbit_jobs/scheduler.rb', line 51 def rufus_scheduler @rufus_scheduler ||= Rufus::Scheduler.new end |
#setup_job_schedule(name, config) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rabbit_jobs/scheduler.rb', line 82 def setup_job_schedule(name, config) interval_defined = false %w(cron every).each do |interval_type| next if config[interval_type].blank? RabbitJobs.logger.info "queueing #{config['class']} (#{name})" rufus_scheduler.send(interval_type, config[interval_type], blocking: true) do publish_from_config(config) end interval_defined = true end return if interval_defined RabbitJobs.logger.warn "no #{interval_types.join(' / ')} found for #{config['class']} (#{name}) - skipping" end |
#work ⇒ Object
Subscribes to channel and working on jobs
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rabbit_jobs/scheduler.rb', line 64 def work begin return false unless startup $0 = process_name || 'rj_scheduler' RabbitJobs.logger.info 'Started.' load_schedule! return main_loop rescue log_daemon_error($!) end true end |