Class: Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cron, block) ⇒ Job

Returns a new instance of Job.



4
5
6
7
8
# File 'lib/job.rb', line 4

def initialize (name, cron, block) 
  @name = name
  @cron = cron
  @block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



2
3
4
# File 'lib/job.rb', line 2

def block
  @block
end

#cronObject

Returns the value of attribute cron.



2
3
4
# File 'lib/job.rb', line 2

def cron
  @cron
end

#last_executedObject

Returns the value of attribute last_executed.



2
3
4
# File 'lib/job.rb', line 2

def last_executed
  @last_executed
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/job.rb', line 2

def name
  @name
end

Instance Method Details

#executeObject



40
41
42
43
# File 'lib/job.rb', line 40

def execute
  @last_executed = Time.new
  @block.call
end

#should_execute?Boolean

Returns:

  • (Boolean)


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
37
38
# File 'lib/job.rb', line 10

def should_execute?
  min, hour, day_of_month, month, day_of_week = cron.split("\s")
  if (day_of_week == "7")
    day_of_week = 0
  end
  now = Time.now
  
  if !field_check? min, now.min 
    return false 
  end
  
  if !field_check? hour, now.hour
    return false
  end

  if !field_check? day_of_month, now.day
    return false
  end
  
  if !field_check? month, now.month
    return false
  end
  
  if !field_check? day_of_week, now.wday
    return false
  end
  
  return true;
end