Class: EventMachine::Timers::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/em-timers/em-timers.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, block) ⇒ Timer

Returns a new instance of Timer.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/em-timers/em-timers.rb', line 15

def initialize(options, block)
  @options = options
  if options[:tag]
    @tag = options[:tag]
  end
  if options[:name]
    @name = options[:name]
  end
  @block = block
  @repeats = true
  Timer.list << self
end

Class Attribute Details

.listObject (readonly)

Returns the value of attribute list.



8
9
10
# File 'lib/em-timers/em-timers.rb', line 8

def list
  @list
end

Instance Attribute Details

#kickoff_timerObject (readonly)

Returns the value of attribute kickoff_timer.



14
15
16
# File 'lib/em-timers/em-timers.rb', line 14

def kickoff_timer
  @kickoff_timer
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/em-timers/em-timers.rb', line 11

def name
  @name
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/em-timers/em-timers.rb', line 12

def options
  @options
end

#reschedule_timerObject (readonly)

Returns the value of attribute reschedule_timer.



13
14
15
# File 'lib/em-timers/em-timers.rb', line 13

def reschedule_timer
  @reschedule_timer
end

#tagObject (readonly)

Returns the value of attribute tag.



10
11
12
# File 'lib/em-timers/em-timers.rb', line 10

def tag
  @tag
end

Instance Method Details

#cancelObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/em-timers/em-timers.rb', line 27

def cancel
  if @kickoff_timer
    EM::cancel_timer(@kickoff_timer)
    @kickoff_timer = nil
  end
  if @reschedule_timer
    EM::cancel_timer(@reschedule_timer)
    @reschedule_timer = nil
  end
  Timer.list.delete(self)
end

#scheduleObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/em-timers/em-timers.rb', line 38

def schedule
  increment = @options[:every] || 1
  starting = @options[:starting]

  if options[:cron]
    starting =
      EventMachine::Timers::CronLine.new(options[:cron]).next_time
    @kickoff_timer = EM.add_timer(calculate_delta_time(starting)) {
      @kickoff_timer = nil
      run_and_reschedule_cron
    }
    return self
  elsif @options[:at] || @options[:in]
    @repeats = false
    if @options[:at]
      starting = parse_time(@options[:at])
    else
      starting = Time.now + parse_time(@options[:in])
    end
  end
  
  if starting == :now
    run_and_reschedule(increment)
    return self
  else
    starting = parse_time(starting)
  end

  time = calculate_delta_time(starting)
  
  @kickoff_timer = EM.add_timer(time) {
    @kickoff_timer = nil
    run_and_reschedule(increment)
  }
  self
end