Class: CronR::CronJob

Inherits:
Hash
  • Object
show all
Defined in:
lib/CronR/CronJob.rb

Overview

An instance (cp) of this class (a subclass of hash) represents the standard cron parameters along with an id and something representing the job (eg a lambda).

cp = CronJob.new(‘id1’)

=> '* * * * *' cron job id1 with no job

cp = CronJob.new(‘id2’) a proc…

=> '* * * * *' cron job id1 with a proc
=> #job => will 'call' the proc

cp = CronJob.new(‘id2’) cp.job = … => a non-proc job

CRON PARAMETERS:

CronR parameters are represented as either:

i) booleans = ‘*’ ii) Fixnums = ‘n’ where n is an integer iii) Array = ‘n-m’ ‘n,m’ ‘n-m/k’ ‘n-m,o-p’

cp where s can be :minute,:hour,:day,:dow,:month .

cp = true

ie '*' in a crontab

cp = i (i.kind_of?(Fixnum))

=> for minutes we have 0-5, hours 0-23, etc

cp = [i,…] (array)

=> cp[:minute] =

cp = (0..58).step(2)

=> equivalent to */2 in cron

For each component: [:minute,:hour,:day,:dow,:month] we can then interpret the settings as:

true,true,true,true,true

> run every minute of every hour of every month of every day of

the week
5,true,true,true,true

> run 5 minutes past the hour

(0..55).step(5),true,true,true,true

> run every 5 minutes

[10,30,50],true,true,true,true

> run on 10th, 30th and 50th minute of the hour

Instance Method Summary collapse

Constructor Details

#initialize(id, minute = true, hour = true, day = true, month = true, dow = true, &block) ⇒ CronJob

Returns a new instance of CronJob.



55
56
57
58
59
60
61
62
# File 'lib/CronR/CronJob.rb', line 55

def initialize id,minute=true,hour=true,day=true,month=true,dow=true,&block
  super() {nil}
  self.set(minute,hour,day,month,dow)
  self[:id] = id
  if block_given? then
    self.job &block
  end
end

Instance Method Details

#job(&block) ⇒ Object

Get job or set job via block.

cj.job cj.job {|cj|…}



77
78
79
80
81
82
83
84
# File 'lib/CronR/CronJob.rb', line 77

def job &block
  if block_given? then
    #self[:job] = block.call(self)
    self[:job] = block
  else
    self[:job]
  end
end

#job=(thing) ⇒ Object



86
87
88
# File 'lib/CronR/CronJob.rb', line 86

def job= thing
  self[:job] = thing
end

#once?Boolean

Return true if the job is intended to only be run as a one-off.

Returns:

  • (Boolean)


140
141
142
# File 'lib/CronR/CronJob.rb', line 140

def once?
  self[:once] || false
end

#runObject

Run the job.

This is a convenience method to handle calling proc based :job’s.



95
96
97
98
99
100
101
102
# File 'lib/CronR/CronJob.rb', line 95

def run
  case self[:job]
  when Proc
    self[:job].call
  else
    self[:job]
  end
end

#runnable?(time) ⇒ Boolean

Return true if job is runnable at the given time.

Note we expect an instance of Time. ActiveSupport can be used to give us time zones in Time.

Example

ok,details = runnable?(Time.now)

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/CronR/CronJob.rb', line 112

def runnable? time
  result = [:minute,:hour,:day,:dow,:month].map{|ct|
    if self[ct] == true then
      true
    else
      case ct
      when :month,:day,:hour
        val = time.send(ct)
      when :dow
        val = time.wday
      when :minute
        val = time.min
      end

      case self[ct]
      when Numeric # Should be Fixnum
        self[ct] == val
      else # Assume array-like thing...
        self[ct].include?(val)
      end
    end
  }
  # Everything should be true to make us eligible for running:
  [result.inject(true){|s,v| s && v},result]
end

#set(minute = true, hour = true, day = true, month = true, dow = true) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/CronR/CronJob.rb', line 64

def set minute=true,hour=true,day=true,month=true,dow=true
  self[:minute] = minute
  self[:hour] = hour
  self[:day] = day
  self[:month] = month
  self[:dow] = dow # 0=sunday,...,6=Saturday
end