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

Create a new thread

Raises:

  • (ArgumentError)


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

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
    @kill = false
    @logger = args[:logger].is_a?(LogHelper) ? args[:logger] : LogHelper.new(args[:logger])
    @lock = Mutex.new
    @thread = ::Thread.new{ start_thread }
end

Instance Method Details

#action_timeoutObject

Seconds thread will spend working on a given task



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

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)


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

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)


46
47
48
# File 'lib/actionpool/Thread.rb', line 46

def alive?
    @thread.alive?
end

#status(arg) ⇒ Object

arg

:wait or :run

Set current status

Raises:

  • (InvalidType)


51
52
53
# File 'lib/actionpool/Thread.rb', line 51

def status
    @lock.synchronize{ return @status }
end

#stop(*args) ⇒ Object

:force

force the thread to stop

:wait

wait for the thread to stop

Stop the thread



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

def stop(*args)
    @kill = true
    @thread.raise Wakeup.new if args.include?(:force) || waiting?
    nil
end

#thread_timeoutObject

Seconds thread will wait for input



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

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)


74
75
76
77
78
79
80
# File 'lib/actionpool/Thread.rb', line 74

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)


41
42
43
# File 'lib/actionpool/Thread.rb', line 41

def waiting?
    @lock.synchronize{@status} == :wait
end