Class: ActionTimer::Timer

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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, extra = nil) ⇒ Timer

pool

ActionPool for processing actions

Creates a new timer Argument hash: :logger, :auto_start



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/actiontimer/Timer.rb', line 9

def initialize(args={}, extra=nil)
    auto_start = true
    if(args.is_a?(Hash))
        @pool = args[:pool] ? args[:pool] : ActionPool::Pool.new
        @logger = args[:logger] && args[:logger].is_a?(Logger) ? args[:logger] : Logger.new(nil)
        auto_start = args.has_key?(:auto_start) ? args[:auto_start] : true
    else
        @pool = args.is_a?(ActionPool::Pool) ? args : ActionPool::Pool.new
        @logger = extra && extra.is_a?(Logger) ? extra : Logger.new(nil)
    end
    @actions = []
    @new_actions = []
    @timer_thread = nil
    @stop_timer = false
    @add_lock = Mutex.new
    @awake_lock = Mutex.new
    start if auto_start
end

Instance Method Details

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

period

amount of time between runs

once

only run this action once

data

data to pass to block

owner

owner of Action

func

block to be executed

Add a new action to block



42
43
44
45
46
47
48
# File 'lib/actiontimer/Timer.rb', line 42

def add(period, once=false, data=nil, owner=nil, &func)
    action = Action.new(self, period, once, data, &func)
    action.owner = owner unless owner.nil?
    @add_lock.synchronize{ @new_actions << action }
    wakeup if running?
    return action
end

#clear(owner = nil) ⇒ Object

owner

owner actions to remove

Clears timer of actions. If an owner is supplied only actions owned by owner will be removed



120
121
122
123
124
125
126
127
128
# File 'lib/actiontimer/Timer.rb', line 120

def clear(owner=nil)
    if(owner.nil?)
        @actions.clear
        @new_actions.clear
    else
        @actions.each{|a| @actions.delete(a) if a.owner == owner}
    end
    wakeup
end

#mass_add(actions) ⇒ Object

actions

Array of actions

Add multiple Actions to the timer at once

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
# File 'lib/actiontimer/Timer.rb', line 52

def mass_add(actions)
    raise ArgumentError.new('Expecting an array') unless actions.is_a?(Array)
    actions.each do |action|
        raise ArgumentError.new('Expecting an Action') unless action.is_a?(Action)
    end
    @add_lock.synchronize{ @new_actions = @new_actions + actions }
    wakeup if running?
end

#pauseObject

Pause the timer in its current state.



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

def pause
    @stop_timer = true
    if(running?)
        wakeup
        @timer_thread.join
    end
    @timer_thread = nil
end

#remove(action) ⇒ Object

action

Action to remove from timer

Remove given action from timer

Raises:

  • (ArgumentError)


63
64
65
66
67
# File 'lib/actiontimer/Timer.rb', line 63

def remove(action)
    raise ArgumentError.new('Expecting an action') unless action.is_a?(Action)
    @actions.delete(action)
    wakeup if running?
end

#running?Boolean

Is timer currently running?

Returns:

  • (Boolean)


131
132
133
# File 'lib/actiontimer/Timer.rb', line 131

def running?
    return !@timer_thread.nil?
end

#startObject

Start the timer

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/actiontimer/Timer.rb', line 70

def start
    raise AlreadyRunning.new unless @timer_thread.nil?
    @stop_timer = false
    @timer_thread = Thread.new do
        begin
            until @stop_timer do
                to_sleep = get_min_sleep
                if((to_sleep.nil? || to_sleep > 0) && @new_actions.empty?)
                    @awake_lock.unlock if @awake_lock.locked?
                    actual_sleep = to_sleep.nil? ? sleep : sleep(to_sleep)
                    @awake_lock.lock
                else
                    actual_sleep = 0
                end
                tick(actual_sleep)
                add_waiting_actions
            end
        rescue Object => boom
            @timer_thread = nil
            clean_actions
            @logger.fatal("Timer encountered an unexpected error: #{boom}\n#{boom.backtrace.join("\n")}")
        end
    end
end

#stopObject

Stop the timer. Unlike pause, this will completely stop the timer and remove all actions from the timer



107
108
109
110
111
112
113
114
115
# File 'lib/actiontimer/Timer.rb', line 107

def stop
    @stop_timer = true
    if(running?)
        wakeup
        clean_actions
        @timer_thread.join
    end
    @timer_thread = nil
end

#wakeupObject

Forcibly wakes the timer early

Raises:



29
30
31
32
33
34
# File 'lib/actiontimer/Timer.rb', line 29

def wakeup
    raise NotRunning.new unless running?
    return unless @awake_lock.try_lock
    @timer_thread.wakeup if @timer_thread.status == 'sleep'
    @awake_lock.unlock
end