Class: Whenever::JobList
- Inherits:
-
Object
- Object
- Whenever::JobList
- Defined in:
- lib/job_list.rb
Instance Method Summary collapse
- #command(task, options = {}) ⇒ Object
- #env(variable, value) ⇒ Object
- #every(frequency, options = {}) ⇒ Object
- #generate_cron_output ⇒ Object
-
#initialize(options) ⇒ JobList
constructor
A new instance of JobList.
- #rake(task, options = {}) ⇒ Object
- #runner(task, options = {}) ⇒ Object
- #schedule_data_for_task(task) ⇒ Object
- #schedule_for_task(task) ⇒ Object
- #scheduled_job_for_task(task) ⇒ Object
- #scheduled_jobs ⇒ Object
- #set(variable, value) ⇒ Object
-
#update_schedule_for_task(task, schedule) ⇒ Object
Expect arguments in form: - task: “Otl::Eirb::ProtocolJob” # => class name as string - schedule: “frequency”=>“2”, “at”=>“10:00pm”.
Constructor Details
#initialize(options) ⇒ JobList
Returns a new instance of JobList.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/job_list.rb', line 4 def initialize() @jobs = Hash.new @env = Hash.new config = case when String then when Hash if [:string] [:string] elsif [:file] @filename = [:file] file_read = File.read(@filename) end end if file_read eval(ERB.new(file_read).result(binding)) else eval(config) end end |
Instance Method Details
#command(task, options = {}) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/job_list.rb', line 41 def command(task, = {}) [:cron_log] ||= @cron_log unless [:cron_log] === false [:class] ||= Whenever::Job::Default @jobs[@current_time_scope] ||= [] @jobs[@current_time_scope] << [:class].new(@options.merge(:task => task).merge()) end |
#env(variable, value) ⇒ Object
31 32 33 |
# File 'lib/job_list.rb', line 31 def env(variable, value) @env[variable.to_s] = value end |
#every(frequency, options = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/job_list.rb', line 35 def every(frequency, = {}) @current_time_scope = frequency @options = yield end |
#generate_cron_output ⇒ Object
60 61 62 |
# File 'lib/job_list.rb', line 60 def generate_cron_output [environment_variables, cron_jobs].compact.join end |
#rake(task, options = {}) ⇒ Object
54 55 56 57 58 |
# File 'lib/job_list.rb', line 54 def rake(task, = {}) .reverse_merge!(:environment => @environment, :path => @path) [:class] = Whenever::Job::RakeTask command(task, ) end |
#runner(task, options = {}) ⇒ Object
48 49 50 51 52 |
# File 'lib/job_list.rb', line 48 def runner(task, = {}) .reverse_merge!(:environment => @environment, :path => @path) [:class] = Whenever::Job::Runner command(task, ) end |
#schedule_data_for_task(task) ⇒ Object
81 82 83 84 85 |
# File 'lib/job_list.rb', line 81 def schedule_data_for_task(task) if scheduled_job = scheduled_job_for_task(task) scheduled_job.schedule_data end end |
#schedule_for_task(task) ⇒ Object
76 77 78 79 |
# File 'lib/job_list.rb', line 76 def schedule_for_task(task) scheduled_job = scheduled_job_for_task(task) scheduled_job ? scheduled_job.schedule : "Schedule for #{task} not found" end |
#scheduled_job_for_task(task) ⇒ Object
109 110 111 |
# File 'lib/job_list.rb', line 109 def scheduled_job_for_task(task) scheduled_jobs.detect { |sj| sj.task =~ Regexp.new(task) } end |
#scheduled_jobs ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/job_list.rb', line 64 def scheduled_jobs @scheduled_jobs ||= begin returning scheduled = [] do @jobs.each do |time, jobs| jobs.each do |j| scheduled << ScheduledJob.new(j, time) end end end end end |
#set(variable, value) ⇒ Object
26 27 28 29 |
# File 'lib/job_list.rb', line 26 def set(variable, value) instance_variable_set("@#{variable}".to_sym, value) self.class.send(:attr_reader, variable.to_sym) end |
#update_schedule_for_task(task, schedule) ⇒ Object
Expect arguments in form:
-
task: “Otl::Eirb::ProtocolJob” # => class name as string
-
schedule: “frequency”=>“2”, “at”=>“10:00pm”
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/job_list.rb', line 90 def update_schedule_for_task(task, schedule) lines = File.readlines(@filename) lines.each_with_index do |line, i| if line.include?(task) j = i - 1 until lines[j] =~ /^every/ j -= 1 end lines[j] = "every #{schedule['frequency']}.#{schedule['interval']}, :at => '#{schedule['at']}' do\n" File.open(@filename, 'w') do |f| lines.each do |line| f.write(line) end end end end scheduled_job_for_task(task) end |