Class: CronJob

Inherits:
Object
  • Object
show all
Defined in:
lib/khronotab/cron_job.rb

Constant Summary collapse

JOB_REGEX =
/([0-9\*\/\-,]+)\s+([0-9\*\/\-,]+)\s+([0-9\*\/\-,]+)\s+([0-9\*\/\-,]+)\s+([0-9\*\/\-,]+)\s+(\S+)\s+(.+)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ CronJob

Returns a new instance of CronJob.



57
58
59
60
61
62
63
64
65
# File 'lib/khronotab/cron_job.rb', line 57

def initialize(data = {})
  @minutes = CronUnit.new(data[:minutes], 0, 59)
  @hours = CronUnit.new(data[:hours], 0, 23)
  @days_of_month = CronUnit.new(data[:days_of_month], 1, 31)
  @month = CronUnit.new(data[:month], 1, 12)
  @days_of_week = CronUnit.new(data[:days_of_week], 0, 7)
  @user = data[:user]
  @command = data[:command]
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def command
  @command
end

#daysObject

Returns the value of attribute days.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def days
  @days
end

#days_of_monthObject

Returns the value of attribute days_of_month.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def days_of_month
  @days_of_month
end

#days_of_weekObject

Returns the value of attribute days_of_week.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def days_of_week
  @days_of_week
end

#hoursObject

Returns the value of attribute hours.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def minutes
  @minutes
end

#monthObject

Returns the value of attribute month.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def month
  @month
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/khronotab/cron_job.rb', line 8

def user
  @user
end

Class Method Details

.add_new(cron_entry) ⇒ Object



16
17
18
19
# File 'lib/khronotab/cron_job.rb', line 16

def self.add_new(cron_entry)
  STDERR.puts("DEPRECATION WARNING: add_new is deprecated. See CronJob.add_new")
  self.parse_new(cron_entry)
end

.matches?(cron_entry) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/khronotab/cron_job.rb', line 12

def self.matches?(cron_entry)
  !!JOB_REGEX.match(cron_entry)
end

.parse_new(cron_entry) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/khronotab/cron_job.rb', line 21

def self.parse_new(cron_entry)
  return nil unless JOB_REGEX.match(cron_entry)
  minute, hour, dom, month, dow, user, command =
       cron_entry.scan(JOB_REGEX).shift
  self.new(
            :minutes => minute,
            :hours => hour,
            :days_of_month => dom,
            :month => month,
            :days_of_week => dow,
            :user => user,
            :command => command
          )
end

Instance Method Details

#[](ac) ⇒ Object



36
37
38
39
# File 'lib/khronotab/cron_job.rb', line 36

def [](ac)
  return self.send(ac) if self.respond_to?(ac)
  raise 'Unknown key!'
end

#[]=(key, value) ⇒ Object



41
42
43
44
# File 'lib/khronotab/cron_job.rb', line 41

def []=(key,value)
  return self.send("#{key}=", value) if self.respond_to?(key)
  raise 'Unknown key!'
end

#expand_times(date = Date.today) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/khronotab/cron_job.rb', line 46

def expand_times(date = Date.today)
  return [] unless runs_on?(date)
  time_list = []
  hours.expanded_form.map do |hour|
    minutes.expanded_form.map do |minute|
      time_list << "%02i-%02i-%02i %02i:%02i:%02i" % [date.year, date.month, date.day, hour, minute, 0]
    end
  end
  return time_list
end

#runs_on?(date_to_check) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/khronotab/cron_job.rb', line 67

def runs_on?(date_to_check)
  month.contains?(date_to_check.month) &&
  days_of_month.contains?(date_to_check.day) &&
  days_of_week.contains?(date_to_check.wday)
end

#runs_today?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/khronotab/cron_job.rb', line 73

def runs_today?
  runs_on?(Time.now)
end

#to_lineObject



83
84
85
86
87
# File 'lib/khronotab/cron_job.rb', line 83

def to_line
  puts "%s %s %s %s %s %s %s" %
          [ minutes, hours, days_of_month, month, days_of_week, user,
            command ]
end

#to_sObject



77
78
79
80
81
# File 'lib/khronotab/cron_job.rb', line 77

def to_s
  puts "<Job minutes:%s hours:%s dom:%s month:%s dow:%s user:%s cmd:%s>" %
          [ minutes, hours, days_of_month, month, days_of_week, user,
            command ]
end