Class: WheneverSystemd::Job
- Inherits:
-
Object
- Object
- WheneverSystemd::Job
- Defined in:
- lib/whenever_systemd/job.rb
Instance Attribute Summary collapse
-
#at ⇒ Object
readonly
Returns the value of attribute at.
-
#mailto ⇒ Object
readonly
Returns the value of attribute mailto.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
- #compose_on_calendar(*args) ⇒ Object
- #has_role?(role) ⇒ Boolean
-
#initialize(name, options = {}) ⇒ Job
constructor
A new instance of Job.
- #output ⇒ Object
- #service_name ⇒ Object
- #systemd_service ⇒ Object
- #systemd_timer ⇒ Object
- #systemd_units(path) ⇒ Object
- #timer_name ⇒ Object
- #unit_expansion ⇒ Object
- #unprefixed_name(prefix = nil) ⇒ Object
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, = {}) @name = name @options = @at = .delete(:at) @template = .delete(:template) @mailto = .fetch(:mailto, :default_mailto) @job_template = .delete(:job_template) || ":job" @roles = Array(.delete(:roles)) @options[:output] = .has_key?(:output) ? Output::Redirection.new([:output]).to_s : '' @options[:environment_variable] ||= "RAILS_ENV" @options[:environment] ||= :production @options[:path] = Shellwords.shellescape(@options[:path] || WheneverSystemd.path) description = .delete(:description) install = .delete(:install) { { wanted_by: "timers.target" } } timer = .delete(:timer).to_h timer[:on_calendar] ||= compose_on_calendar(.delete(:interval), @at) unit = .delete(:unit).to_h unit[:description] = description service = .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
#at ⇒ Object (readonly)
Returns the value of attribute at.
6 7 8 |
# File 'lib/whenever_systemd/job.rb', line 6 def at @at end |
#mailto ⇒ Object (readonly)
Returns the value of attribute mailto.
6 7 8 |
# File 'lib/whenever_systemd/job.rb', line 6 def mailto @mailto end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/whenever_systemd/job.rb', line 6 def name @name end |
#roles ⇒ Object (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
89 90 91 |
# File 'lib/whenever_systemd/job.rb', line 89 def has_role?(role) roles.empty? || roles.include?(role) end |
#output ⇒ Object
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_name ⇒ Object
62 63 64 |
# File 'lib/whenever_systemd/job.rb', line 62 def service_name "#{@name}.service" end |
#systemd_service ⇒ Object
81 82 83 |
# File 'lib/whenever_systemd/job.rb', line 81 def systemd_service Formatters::Service[@service_options] end |
#systemd_timer ⇒ Object
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_name ⇒ Object
66 67 68 |
# File 'lib/whenever_systemd/job.rb', line 66 def timer_name "#{@name}.timer" end |
#unit_expansion ⇒ Object
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 |