Class: Scheddy::Scheduler
- Inherits:
-
Object
- Object
- Scheddy::Scheduler
- Defined in:
- lib/scheddy/scheduler.rb
Overview
must be > 2x the renewal interval
Instance Method Summary collapse
- #hostname ⇒ Object
- #logger ⇒ Object
- #pid ⇒ Object
- #run ⇒ Object
-
#run_once ⇒ Object
return : Time of next cycle.
- #scheduler_id ⇒ Object
- #stepdown? ⇒ Boolean
- #stop? ⇒ Boolean
Instance Method Details
#hostname ⇒ Object
64 65 66 |
# File 'lib/scheddy/scheduler.rb', line 64 def hostname @hostname ||= Socket.gethostname.force_encoding(Encoding::UTF_8) end |
#logger ⇒ Object
76 77 78 |
# File 'lib/scheddy/scheduler.rb', line 76 def logger @logger ||= Scheddy.logger.tagged "scheddy-#{scheduler_id}" end |
#pid ⇒ Object
68 69 70 |
# File 'lib/scheddy/scheduler.rb', line 68 def pid @pid ||= Process.pid end |
#run ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/scheddy/scheduler.rb', line 8 def run puts "[scheddy] Hello. This is Scheddy v#{VERSION}." puts "[scheddy] hostname=#{hostname}, pid=#{pid}, id=#{scheduler_id}" trap_signals! puts "[scheddy] Starting scheduler with #{tasks.size} #{'task'.pluralize tasks.size}" unless register_process puts '[scheddy] No scheddy_task_schedulers table found; disabling cluster support' end until stop? with_leader do |new_leader| reset_tasks if new_leader cleanup_task_history cleanup_task_scheduler next_cycle = run_once if tasks.any? && scheduler_record next_cycle = [next_cycle, LEASE_RENEWAL_INTERVAL.from_now].compact.min end wait_until next_cycle unless stop? end end stepdown_as_leader running = tasks.select(&:running?).count if running > 0 puts "[scheddy] Waiting for #{running} tasks to complete" wait_for(45.seconds, skip_stop: true) do tasks.none?(&:running?) end tasks.select(&:running?).each do |task| $stderr.puts "[scheddy] Killing task #{task.name}" task.kill end end ensure unregister_process puts '[scheddy] Goodbye' end |
#run_once ⇒ Object
return : Time of next cycle
51 52 53 54 55 56 57 58 59 |
# File 'lib/scheddy/scheduler.rb', line 51 def run_once if tasks.empty? logger.warn 'No tasks found; doing nothing' return 1.hour.from_now end tasks.filter_map do |task| task.perform(self) unless stop? end.min end |
#scheduler_id ⇒ Object
72 73 74 |
# File 'lib/scheddy/scheduler.rb', line 72 def scheduler_id @scheduler_id ||= SecureRandom.alphanumeric 12 end |
#stepdown? ⇒ Boolean
61 |
# File 'lib/scheddy/scheduler.rb', line 61 def stepdown? ; @stepdown ; end |
#stop? ⇒ Boolean
62 |
# File 'lib/scheddy/scheduler.rb', line 62 def stop? ; @stop ; end |