Class: Recurrent::Scheduler
- Inherits:
-
Object
- Object
- Recurrent::Scheduler
- Defined in:
- lib/recurrent/scheduler.rb
Instance Attribute Summary collapse
-
#executing_tasks ⇒ Object
Returns the value of attribute executing_tasks.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#tasks ⇒ Object
Returns the value of attribute tasks.
Class Method Summary collapse
Instance Method Summary collapse
- #configure ⇒ Object
- #create_rule_from_frequency(frequency) ⇒ Object
- #create_schedule(name, frequency, start_time = nil) ⇒ Object
- #create_schedule_from_frequency(frequency, start_time = nil) ⇒ Object
- #decrement_executing_tasks ⇒ Object
- #derive_start_time_from_frequency(frequency) ⇒ Object
- #every(frequency, key, options = {}, &block) ⇒ Object
- #increment_executing_tasks ⇒ Object
-
#initialize(task_file = nil) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #use_saved_schedule_if_rules_match(saved_schedule, new_schedule) ⇒ Object
Constructor Details
#initialize(task_file = nil) ⇒ Scheduler
Returns a new instance of Scheduler.
6 7 8 9 10 11 12 13 |
# File 'lib/recurrent/scheduler.rb', line 6 def initialize(task_file=nil) @tasks = TaskCollection.new identifier = "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}" @logger = Logger.new(identifier) @mutex = Mutex.new @executing_tasks = 0 eval(File.read(task_file)) if task_file end |
Instance Attribute Details
#executing_tasks ⇒ Object
Returns the value of attribute executing_tasks.
4 5 6 |
# File 'lib/recurrent/scheduler.rb', line 4 def executing_tasks @executing_tasks end |
#logger ⇒ Object
Returns the value of attribute logger.
4 5 6 |
# File 'lib/recurrent/scheduler.rb', line 4 def logger @logger end |
#mutex ⇒ Object
Returns the value of attribute mutex.
4 5 6 |
# File 'lib/recurrent/scheduler.rb', line 4 def mutex @mutex end |
#tasks ⇒ Object
Returns the value of attribute tasks.
4 5 6 |
# File 'lib/recurrent/scheduler.rb', line 4 def tasks @tasks end |
Class Method Details
.define_frequencies(*frequencies) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/recurrent/scheduler.rb', line 125 def self.define_frequencies(*frequencies) frequencies.each do |frequency| method_name = frequency == :day ? :daily? : :"#{frequency}ly?" define_method(method_name) do |number| (number % 1.send(frequency)) == 0 end end end |
Instance Method Details
#configure ⇒ Object
15 16 17 |
# File 'lib/recurrent/scheduler.rb', line 15 def configure Configuration end |
#create_rule_from_frequency(frequency) ⇒ Object
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 |
# File 'lib/recurrent/scheduler.rb', line 19 def create_rule_from_frequency(frequency) logger.info "| Creating an IceCube Rule" if yearly?(frequency) logger.info "| Creating a yearly rule" IceCube::Rule.yearly(frequency / 1.year) elsif monthly?(frequency) logger.info "| Creating a monthly rule" IceCube::Rule.monthly(frequency / 1.month) elsif weekly?(frequency) logger.info "| Creating a weekly rule" IceCube::Rule.weekly(frequency / 1.week) elsif daily?(frequency) logger.info "| Creating a daily rule" IceCube::Rule.daily(frequency / 1.day) elsif hourly?(frequency) logger.info "| Creating an hourly rule" IceCube::Rule.hourly(frequency / 1.hour) elsif minutely?(frequency) logger.info "| Creating a minutely rule" IceCube::Rule.minutely(frequency / 1.minute) else logger.info "| Creating a secondly rule" IceCube::Rule.secondly(frequency) end end |
#create_schedule(name, frequency, start_time = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/recurrent/scheduler.rb', line 45 def create_schedule(name, frequency, start_time=nil) saved_schedule = Configuration.load_task_schedule.call(name) if Configuration.load_task_schedule new_schedule = frequency.is_a?(IceCube::Schedule) ? frequency : create_schedule_from_frequency(frequency, start_time) if saved_schedule use_saved_schedule_if_rules_match(saved_schedule, new_schedule) else new_schedule end end |
#create_schedule_from_frequency(frequency, start_time = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/recurrent/scheduler.rb', line 55 def create_schedule_from_frequency(frequency, start_time=nil) logger.info "| Frequency is an integer: #{frequency}" rule = create_rule_from_frequency(frequency) logger.info "| IceCube Rule created: #{rule.to_s}" frequency_in_seconds = frequency start_time ||= derive_start_time_from_frequency(frequency_in_seconds) schedule = IceCube::Schedule.new(start_time) schedule.add_recurrence_rule rule schedule end |
#decrement_executing_tasks ⇒ Object
119 120 121 122 123 |
# File 'lib/recurrent/scheduler.rb', line 119 def decrement_executing_tasks mutex.synchronize do @executing_tasks -= 1 end end |
#derive_start_time_from_frequency(frequency) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/recurrent/scheduler.rb', line 66 def derive_start_time_from_frequency(frequency) logger.info "| Deriving start time from frequency" current_time = Time.now if frequency < 1.minute logger.info "| Setting start time to beginning of current minute" current_time.change(:sec => 0, :usec => 0) elsif frequency < 1.hour logger.info "| Setting start time to beginning of current hour" current_time.change(:min => 0, :sec => 0, :usec => 0) elsif frequency < 1.day logger.info "| Setting start time to beginning of current day" current_time.beginning_of_day elsif frequency < 1.week logger.info "| Setting start time to beginning of current week" current_time.beginning_of_week elsif frequency < 1.month logger.info "| Setting start time to beginning of current month" current_time.beginning_of_month elsif frequency < 1.year logger.info "| Setting start time to beginning of current year" current_time.beginning_of_year end end |
#every(frequency, key, options = {}, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/recurrent/scheduler.rb', line 90 def every(frequency, key, ={}, &block) logger.info "Adding Task: #{key}" task = Task.new(:name => key, :schedule => create_schedule(key, frequency, [:start_time]), :action => block, :save => [:save], :logger => logger, :scheduler => self, :disable_task_locking => [:disable_task_locking]) @tasks.add_or_update(task) logger.info "| #{key} added to Scheduler" end |
#increment_executing_tasks ⇒ Object
113 114 115 116 117 |
# File 'lib/recurrent/scheduler.rb', line 113 def increment_executing_tasks mutex.synchronize do @executing_tasks += 1 end end |
#use_saved_schedule_if_rules_match(saved_schedule, new_schedule) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/recurrent/scheduler.rb', line 103 def use_saved_schedule_if_rules_match(saved_schedule, new_schedule) if new_schedule.has_same_rules? saved_schedule logger.info "| Schedule matches a saved schedule, using saved schedule." saved_schedule.start_date = saved_schedule.next_occurrence saved_schedule else new_schedule end end |