Class: Whenever::JobList

Inherits:
Object
  • Object
show all
Defined in:
lib/job_list.rb

Instance Method Summary collapse

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(options)
  @jobs = Hash.new
  @env  = Hash.new

  config = case options
    when String then options
    when Hash
      if options[:string]
        options[:string]
      elsif options[:file]
        @filename = options[: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, options = {})
  options[:cron_log] ||= @cron_log unless options[:cron_log] === false
  options[:class]    ||= Whenever::Job::Default
  @jobs[@current_time_scope] ||= []
  @jobs[@current_time_scope] << options[:class].new(@options.merge(:task => task).merge(options))
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, options = {})
  @current_time_scope = frequency
  @options = options
  yield
end

#generate_cron_outputObject



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, options = {})
  options.reverse_merge!(:environment => @environment, :path => @path)
  options[:class] = Whenever::Job::RakeTask
  command(task, options)
end

#runner(task, options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/job_list.rb', line 48

def runner(task, options = {})
  options.reverse_merge!(:environment => @environment, :path => @path)
  options[:class] = Whenever::Job::Runner
  command(task, options)
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_jobsObject



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