Class: ActionPool::Thread

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Thread

:pool

pool thread is associated with

:t_timeout

max time a thread is allowed to wait for action

:a_timeout

max time thread is allowed to work

:respond_thread

thread to send execptions to

:logger

LogHelper for logging messages

:autostart

Automatically start the thread

Create a new thread

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/actionpool/Thread.rb', line 18

def initialize(args)
    raise ArgumentError.new('Hash required for initialization') unless args.is_a?(Hash)
    raise ArgumentError.new('ActionPool::Thread requires a pool') unless args[:pool]
    raise ArgumentError.new('ActionPool::Thread requries thread to respond') unless args[:respond_thread]
    @pool = args[:pool]
    @respond_to = args[:respond_thread]
    @thread_timeout = args[:t_timeout] ? args[:t_timeout].to_f : 0
    @action_timeout = args[:a_timeout] ? args[:a_timeout].to_f : 0
    args[:autostart] = true unless args.has_key?(:autostart)
    @kill = false
    @logger = args[:logger].is_a?(Logger) ? args[:logger] : Logger.new(nil)
    @lock = Splib::Monitor.new
    @action = nil
    @thread = args[:autostart] ? ::Thread.new{ start_thread } : nil
end

Instance Method Details

#action_timeoutObject

Seconds thread will spend working on a given task



97
98
99
# File 'lib/actionpool/Thread.rb', line 97

def action_timeout
    @action_timeout
end

#action_timeout=(t) ⇒ Object

t

seconds to work on a task (floats allow for values 0 < t < 1)

Set the maximum amount of time to work on a given task Note: Modification of this will not affect actions already in process

Raises:

  • (ArgumentError)


114
115
116
117
118
119
# File 'lib/actionpool/Thread.rb', line 114

def action_timeout=(t)
    t = t.to_f
    raise ArgumentError.new('Value must be great than zero or nil') unless t > 0
    @action_timeout = t
    t
end

#alive?Boolean

Is the thread still alive

Returns:

  • (Boolean)


68
69
70
# File 'lib/actionpool/Thread.rb', line 68

def alive?
    @thread.alive?
end

#joinObject

Join internal thread



78
79
80
81
82
83
84
# File 'lib/actionpool/Thread.rb', line 78

def join
    @thread.join(@action_timeout)
    if(@thread.alive?)
        @thread.kill
        @thread.join
    end
end

#killObject

Kill internal thread



87
88
89
# File 'lib/actionpool/Thread.rb', line 87

def kill
    @thread.kill
end

#running?Boolean

Currently running

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/actionpool/Thread.rb', line 62

def running?
    !@action.nil?
#             @status == :run
end

#startObject



34
35
36
# File 'lib/actionpool/Thread.rb', line 34

def start
    @thread = ::Thread.new{ start_thread } if @thread.nil?
end

#statusObject

Current thread status



73
74
75
# File 'lib/actionpool/Thread.rb', line 73

def status
    @action
end

#stop(*args) ⇒ Object

:force

force the thread to stop

:wait

wait for the thread to stop

Stop the thread



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/actionpool/Thread.rb', line 41

def stop(*args)
    @kill = true
    if(args.include?(:force) || waiting?)
        begin
            @thread.raise Wakeup.new
        rescue Wakeup
            #ignore since we are the caller
        end
        sleep(0.01)
        @thread.kill if @thread.alive?
    end
    nil
end

#thread_timeoutObject

Seconds thread will wait for input



92
93
94
# File 'lib/actionpool/Thread.rb', line 92

def thread_timeout
    @thread_timeout
end

#thread_timeout=(t) ⇒ Object

t

seconds to wait for input (floats allow for values 0 < t < 1)

Set the maximum amount of time to wait for a task

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
# File 'lib/actionpool/Thread.rb', line 103

def thread_timeout=(t)
    t = t.to_f
    raise ArgumentError.new('Value must be great than zero or nil') unless t > 0
    @thread_timeout = t
    @thread.raise Retimeout.new if waiting?
    t
end

#waiting?Boolean

Currently waiting

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/actionpool/Thread.rb', line 56

def waiting?
    @action.nil?
#             @status == :wait
end