Class: WheneverSystemd::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Job

Returns a new instance of Job.



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
# File 'lib/whenever_systemd/job.rb', line 8

def initialize(name, options = {})
  @name = name
  @options = options
  @at = options.delete(:at)
  @template = options.delete(:template)
  @mailto                           = options.fetch(:mailto, :default_mailto)
  @job_template                     = options.delete(:job_template) || ":job"
  @roles                            = Array(options.delete(:roles))
  @options[:output]                 = options.has_key?(:output) ? Output::Redirection.new(options[:output]).to_s : ''
  @options[:environment_variable] ||= "RAILS_ENV"
  @options[:environment]          ||= :production
  @options[:path]                   = Shellwords.shellescape(@options[:path] || WheneverSystemd.path)

  description = options.delete(:description)
  install = options.delete(:install) { { wanted_by: "timers.target" } }

  timer = options.delete(:timer).to_h
  timer[:on_calendar] ||= compose_on_calendar(options.delete(:interval), @at)

  unit = options.delete(:unit).to_h
  unit[:description] = description

  service = options.delete(:service).to_h
  service[:type] ||= "oneshot"
  service[:exec_start] = output

  @service_options = { unit: unit, service: service }
  @timer_options = { unit: { description: description }, timer: timer, install: install }
end

Instance Attribute Details

#atObject (readonly)

Returns the value of attribute at.



6
7
8
# File 'lib/whenever_systemd/job.rb', line 6

def at
  @at
end

#mailtoObject (readonly)

Returns the value of attribute mailto.



6
7
8
# File 'lib/whenever_systemd/job.rb', line 6

def mailto
  @mailto
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/whenever_systemd/job.rb', line 6

def name
  @name
end

#rolesObject (readonly)

Returns the value of attribute roles.



6
7
8
# File 'lib/whenever_systemd/job.rb', line 6

def roles
  @roles
end

Instance Method Details

#compose_on_calendar(*args) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/whenever_systemd/job.rb', line 38

def compose_on_calendar(*args)
  args.compact!
  if args.size > 1 && Formatters::NormalizeInterval.key?(args[0])
    args[0] = Formatters::NormalizeInterval[args[0]]
  end
  args.join(" ")
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/whenever_systemd/job.rb', line 89

def has_role?(role)
  roles.empty? || roles.include?(role)
end

#outputObject



46
47
48
49
50
# File 'lib/whenever_systemd/job.rb', line 46

def output
  job = process_template(@template, @options)
  out = process_template(@job_template, @options.merge(:job => job))
  out.gsub(/%/, '\%')
end

#service_nameObject



62
63
64
# File 'lib/whenever_systemd/job.rb', line 62

def service_name
  "#{@name}.service"
end

#systemd_serviceObject



81
82
83
# File 'lib/whenever_systemd/job.rb', line 81

def systemd_service
  Formatters::Service[@service_options]
end

#systemd_timerObject



85
86
87
# File 'lib/whenever_systemd/job.rb', line 85

def systemd_timer
  Formatters::Timer[@timer_options]
end

#systemd_units(path) ⇒ Object



74
75
76
77
78
79
# File 'lib/whenever_systemd/job.rb', line 74

def systemd_units(path)
  [
    { path: path, filename: service_name, content: systemd_service },
    { path: path, filename: timer_name,   content: systemd_timer }
  ]
end

#timer_nameObject



66
67
68
# File 'lib/whenever_systemd/job.rb', line 66

def timer_name
  "#{@name}.timer"
end

#unit_expansionObject



70
71
72
# File 'lib/whenever_systemd/job.rb', line 70

def unit_expansion
  "#{@name}.{service,timer}"
end

#unprefixed_name(prefix = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/whenever_systemd/job.rb', line 52

def unprefixed_name(prefix = nil)
  prefix ||= @options[:prefix]
  if @name.rindex(prefix) == 0
    prefix_end = prefix.size.next
    @name[prefix_end, @name.size - prefix_end]
  else
    @name
  end
end