Class: Cronman::Entry
- Inherits:
-
Object
- Object
- Cronman::Entry
- Defined in:
- lib/cronman/entry.rb
Overview
A class which represents a job line in crontab(5).
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#cron_definition ⇒ Object
readonly
Returns the value of attribute cron_definition.
-
#schedule ⇒ Object
readonly
Returns the value of attribute schedule.
-
#sha1 ⇒ Object
readonly
Returns the value of attribute sha1.
-
#translation ⇒ Object
readonly
Returns the value of attribute translation.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
Class Method Summary collapse
-
.parse(line, options = {}) ⇒ Object
Parses a string line in crontab(5) job format.
Instance Method Summary collapse
-
#initialize(schedule, command, cron_definition, uid = nil) ⇒ Entry
constructor
Creates a crontab(5) entry.
- #last_execution ⇒ Object
- #next_execution ⇒ Object
Constructor Details
#initialize(schedule, command, cron_definition, uid = nil) ⇒ Entry
Creates a crontab(5) entry.
-
schedule
A Cronman::Schedule instance. -
command
-
uid
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cronman/entry.rb', line 12 def initialize(schedule, command, cron_definition, uid=nil) raise ArgumentError, 'invalid schedule' unless schedule.is_a? Schedule raise ArgumentError, 'invalid command' unless command.is_a? String @schedule = schedule.freeze @command = command.freeze @cron_definition = cron_definition.freeze @sha1 = Digest::SHA1.hexdigest(@cron_definition + ' ' + @command) @translation = Cron2English.parse(@cron_definition).freeze @uid = case uid when String Etc.getpwnam(uid).uid when Integer uid when nil Process.uid else raise ArgumentError, 'invalid uid' end end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
58 59 60 |
# File 'lib/cronman/entry.rb', line 58 def command @command end |
#cron_definition ⇒ Object (readonly)
Returns the value of attribute cron_definition.
58 59 60 |
# File 'lib/cronman/entry.rb', line 58 def cron_definition @cron_definition end |
#schedule ⇒ Object (readonly)
Returns the value of attribute schedule.
58 59 60 |
# File 'lib/cronman/entry.rb', line 58 def schedule @schedule end |
#sha1 ⇒ Object (readonly)
Returns the value of attribute sha1.
58 59 60 |
# File 'lib/cronman/entry.rb', line 58 def sha1 @sha1 end |
#translation ⇒ Object (readonly)
Returns the value of attribute translation.
58 59 60 |
# File 'lib/cronman/entry.rb', line 58 def translation @translation end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
58 59 60 |
# File 'lib/cronman/entry.rb', line 58 def uid @uid end |
Class Method Details
.parse(line, options = {}) ⇒ Object
Parses a string line in crontab(5) job format.
-
options[:system]
when true system wide crontab is assumed and@uid
is extracted from line.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cronman/entry.rb', line 64 def parse(line, ={}) = { :system => false }.merge() line = line.strip number_of_fields = 1 number_of_fields += line.start_with?('@') ? 1 : 5 number_of_fields += 1 if [:system] words = line.split(/\s+/, number_of_fields) command = words.pop uid = [:system] ? words.pop : Process.uid cron_definition = words.first(5).join(' ') spec = words.join(' ') schedule = Cronman::Schedule.new(spec) new(schedule, command, cron_definition, uid) end |
Instance Method Details
#last_execution ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cronman/entry.rb', line 46 def last_execution now = Time.now if @schedule.day_of_months_given? # Project a year. @schedule.from(now - 1.year).until(now).select { |t| t < now }.last else # Project a month. @schedule.from(now - 1.month).until(now).select { |t| t < now }.last end end |
#next_execution ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cronman/entry.rb', line 34 def next_execution now = Time.now if @schedule.day_of_months_given? # Project a year. @schedule.from(now).until(now + 1.year).select { |t| t > now }.first else # Project a month. @schedule.from(now).until(now + 1.month).select { |t| t > now }.first end end |