Class: ModSpox::Timer

Inherits:
Pool
  • Object
show all
Defined in:
lib/mod_spox/Timer.rb

Instance Attribute Summary

Attributes inherited from Pool

#proc, #queue

Instance Method Summary collapse

Methods inherited from Pool

#destroy, #start_pool

Constructor Details

#initialize(pipeline) ⇒ Timer

pipeline

message pipeline

Create a new Timer



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mod_spox/Timer.rb', line 7

def initialize(pipeline)
    super()
    @pipeline = pipeline
    @timers = Array.new
    Logger.log("Created queue: #{@queue} in timer", 10)
    @monitor = Monitors::Timer.new
    @thread = nil
    @stop_timer = false
    {:Internal_TimerAdd => :add_message,
     :Internal_TimerRemove => :remove_message}.each_pair{|type,method|
        @pipeline.hook(self, method, type)
    }
    start_pool
end

Instance Method Details

#add(period, once = false, data = nil, &func) ⇒ Object

period

seconds between running action

once

only run action once

data

data to be available

&func

data block to run

Adds a new action to the timer



55
56
57
58
59
60
# File 'lib/mod_spox/Timer.rb', line 55

def add(period, once=false, data=nil, &func)
    action = Action.new(self, period, data, once, &func)
    @timers << action
    wakeup
    return action
end

#add_action(action) ⇒ Object

action

Action to add to timer’s queue

Adds a new action to the timer



64
65
66
67
68
# File 'lib/mod_spox/Timer.rb', line 64

def add_action(action)
    raise Exceptions::InvalidType.new('An Action object must be supplied') unless action.is_a?(Action)
    @timers << action
    wakeup
end

#add_message(message) ⇒ Object

message

TimerAdd message

Add a recurring code block



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mod_spox/Timer.rb', line 30

def add_message(message)
    Logger.log("New block is being added to the timer", 15)
    action = add(message.period, message.once, message.data, &message.block)
    begin
        @pipeline << Messages::Internal::TimerResponse.new(message.requester, action, true, message.id)
        Logger.log("New block was successfully added to the timer", 15)
    rescue Object => boom
        Logger.log("Failed to add block to timer: #{boom}", 10)
        @pipeline << Messages::Internal::TimerResponse.new(message.requester, action, false, message.id)
    end
end

#clearObject

Clears all actions in the timer’s queue



105
106
107
108
# File 'lib/mod_spox/Timer.rb', line 105

def clear
    @queue.clear
    @timers.clear
end

#remove(action) ⇒ Object

action

Action to remove from timer’s queue

Removes and action from the timer



72
73
74
75
76
# File 'lib/mod_spox/Timer.rb', line 72

def remove(action)
    raise Exceptions::InvalidType.new('An Action object must be supplied') unless action.is_a?(Action)
    @timers.delete(action)
    wakeup
end

#remove_message(message) ⇒ Object

message

TimerRemove message

Remove an action from the timer



44
45
46
47
48
# File 'lib/mod_spox/Timer.rb', line 44

def remove_message(message)
    remove(message.action)
    Logger.log("Action has been removed from the Timer", 15)
    @pipeline << Messages::Internal::TimerResponse.new(nil, message.action, false, message.id)
end

#startObject

Starts the timer



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mod_spox/Timer.rb', line 79

def start
    raise Exceptions::AlreadyRunning.new('Timer is already running') unless @thread.nil?
    @thread = Thread.new{
        until @stop_timer do
            to_sleep = nil
            @timers.each do |a|
                to_sleep = a.remaining if to_sleep.nil?
                to_sleep = a.remaining if !a.remaining.nil? && a.remaining < to_sleep
            end
            Logger.log("Timer is set to sleep for #{to_sleep.nil? ? 'forever' : "#{to_sleep} seconds"}", 15)
            actual_sleep = @monitor.wait(to_sleep)
            tick(actual_sleep)
            Logger.log("Timer was set to sleep for #{to_sleep.nil? ? 'forever' : "#{to_sleep} seconds"} seconds. Actual sleep time: #{actual_sleep} seconds", 15)
        end
    }
end

#stopObject

Stops the timer



97
98
99
100
101
102
# File 'lib/mod_spox/Timer.rb', line 97

def stop
    raise Exceptions::NotRunning.new('Timer is not running') if @thread.nil?
    @stop_timer = true
    wakeup
    @thread.join
end

#wakeupObject

Wakes the timer up early



23
24
25
26
# File 'lib/mod_spox/Timer.rb', line 23

def wakeup
    Logger.log("Timer has been explicitly told to wakeup", 15)
    @monitor.wakeup unless @thread.nil?
end