Class: Whenever::JobList

Inherits:
Object
  • Object
show all
Defined in:
lib/whenever/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
25
# File 'lib/whenever/job_list.rb', line 4

def initialize(options)
  @jobs, @env, @set_variables, @pre_set_variables = {}, {}, {}, {}
  
  case options
    when String
      config = options
    when Hash
      config = if options[:string]
        options[:string]
      elsif options[:file]
        File.read(options[:file])
      end
      pre_set(options[:set])
  end
  
  # Load all job type files.
  Dir["#{File.expand_path(File.dirname(__FILE__))}/job_types/*.rb"].each do |file|
    eval(File.read(file))
  end
  
  eval(config)
end

Instance Method Details

#env(variable, value) ⇒ Object



36
37
38
# File 'lib/whenever/job_list.rb', line 36

def env(variable, value)
  @env[variable.to_s] = value
end

#every(frequency, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/whenever/job_list.rb', line 40

def every(frequency, options = {})
  @current_time_scope = frequency
  @options = options
  yield
end

#generate_cron_outputObject



63
64
65
# File 'lib/whenever/job_list.rb', line 63

def generate_cron_output      
  [environment_variables, cron_jobs].compact.join
end

#job_type(name, template) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/whenever/job_list.rb', line 46

def job_type(name, template)
  class_eval do
    define_method(name) do |task, *args|
      options = { :task => task, :template => template }
      options.merge!(args[0]) if args[0].is_a? Hash
      
      # :cron_log was an old option for output redirection, it remains for backwards compatibility
      options[:output] = (options[:cron_log] || @cron_log) if defined?(@cron_log) || options.has_key?(:cron_log)
      # :output is the newer, more flexible option.
      options[:output] = @output if defined?(@output) && !options.has_key?(:output)
      
      @jobs[@current_time_scope] ||= []
      @jobs[@current_time_scope] << Whenever::Job.new(@options.merge(@set_variables).merge(options))
    end
  end
end

#set(variable, value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/whenever/job_list.rb', line 27

def set(variable, value)
  variable = variable.to_sym
  return if @pre_set_variables[variable]
  
  instance_variable_set("@#{variable}".to_sym, value)
  self.class.send(:attr_reader, variable.to_sym)
  @set_variables[variable] = value
end