Class: Sunshine::Crontab
- Inherits:
-
Object
- Object
- Sunshine::Crontab
- Defined in:
- lib/sunshine/crontab.rb
Overview
A simple namespaced grouping of cron jobs that can be read and written to a shell.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the jobs hash.
-
#[]=(key, value) ⇒ Object
Set a value in the jobs hash.Equivalent to: crontab.jobs = value.
-
#build(crontab = "") ⇒ Object
Build the crontab by replacing preexisting cron jobs and adding new ones.
-
#delete! ⇒ Object
Remove all cron jobs that reference crontab.name.
-
#initialize(name, shell) ⇒ Crontab
constructor
A new instance of Crontab.
-
#jobs ⇒ Object
Get the jobs matching this crontab.
-
#modified? ⇒ Boolean
Checks if the crontab was modified for crontab.name.
-
#parse(string) ⇒ Object
Load a crontab string and parse out jobs related to crontab.name.
-
#read_crontab ⇒ Object
Returns the shell’s crontab as a string.
-
#write! ⇒ Object
Write the crontab on the given shell.
Constructor Details
#initialize(name, shell) ⇒ Crontab
Returns a new instance of Crontab.
11 12 13 14 15 |
# File 'lib/sunshine/crontab.rb', line 11 def initialize name, shell @name = name @shell = shell @jobs = nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/sunshine/crontab.rb', line 9 def name @name end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
9 10 11 |
# File 'lib/sunshine/crontab.rb', line 9 def shell @shell end |
Instance Method Details
#[](key) ⇒ Object
Access the jobs hash. Equivalent to:
crontab.jobs[key]
22 23 24 |
# File 'lib/sunshine/crontab.rb', line 22 def [] key self.jobs[key] end |
#[]=(key, value) ⇒ Object
Set a value in the jobs hash.Equivalent to:
crontab.jobs[key] = value
31 32 33 |
# File 'lib/sunshine/crontab.rb', line 31 def []= key, value self.jobs[key] = value end |
#build(crontab = "") ⇒ Object
Build the crontab by replacing preexisting cron jobs and adding new ones.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sunshine/crontab.rb', line 48 def build crontab="" crontab.strip! jobs.each do |namespace, cron_job| cron_job = cron_job.join("\n") if Array === cron_job crontab = delete_jobs crontab, namespace next if cron_job.nil? || cron_job.empty? start_id, end_id = get_job_ids namespace cron_str = "\n#{start_id}\n#{cron_job.chomp}\n#{end_id}\n\n" crontab << cron_str end crontab end |
#delete! ⇒ Object
Remove all cron jobs that reference crontab.name.
70 71 72 73 74 75 76 77 |
# File 'lib/sunshine/crontab.rb', line 70 def delete! crontab = read_crontab crontab = delete_jobs crontab write_crontab crontab crontab end |
#jobs ⇒ Object
Get the jobs matching this crontab. Loads them from the crontab if @jobs hasn’t been set yet.
40 41 42 |
# File 'lib/sunshine/crontab.rb', line 40 def jobs @jobs ||= parse read_crontab end |
#modified? ⇒ Boolean
Checks if the crontab was modified for crontab.name.
99 100 101 |
# File 'lib/sunshine/crontab.rb', line 99 def modified? !@jobs.nil? end |
#parse(string) ⇒ Object
Load a crontab string and parse out jobs related to crontab.name. Returns a hash of namespace/jobs pairs.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sunshine/crontab.rb', line 108 def parse string jobs = Hash.new{|hash, key| hash[key] = Array.new} namespace = nil string.each_line do |line| if line =~ /^# sunshine #{@name}:(.*):begin/ namespace = $1 next elsif line =~ /^# sunshine #{@name}:#{namespace}:end/ namespace = nil end if namespace line = line.strip jobs[namespace] << line unless line.empty? end end jobs unless jobs.empty? end |
#read_crontab ⇒ Object
Returns the shell’s crontab as a string
134 135 136 |
# File 'lib/sunshine/crontab.rb', line 134 def read_crontab @shell.call("crontab -l") rescue "" end |
#write! ⇒ Object
Write the crontab on the given shell
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sunshine/crontab.rb', line 83 def write! return unless modified? crontab = read_crontab crontab = delete_jobs crontab crontab = build crontab write_crontab crontab crontab end |