Class: ThreadStorm::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/thread_storm/execution.rb

Overview

Encapsulates a unit of work to be sent to the thread pool.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, &block) ⇒ Execution

:nodoc:



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/thread_storm/execution.rb', line 7

def initialize(args, &block) #:nodoc:
  @args = args
  @block = block
  @start_time = nil
  @finish_time = nil
  @value = nil
  @exception = nil
  @timed_out = false
  @thread = nil
  @mutex = Mutex.new
  @join = ConditionVariable.new
end

Instance Attribute Details

#argsObject (readonly)

:nodoc:



5
6
7
# File 'lib/thread_storm/execution.rb', line 5

def args
  @args
end

#blockObject (readonly)

:nodoc:



5
6
7
# File 'lib/thread_storm/execution.rb', line 5

def block
  @block
end

#exceptionObject

If this execution finished with an exception, it is stored here.



79
80
81
# File 'lib/thread_storm/execution.rb', line 79

def exception
  @exception
end

#threadObject (readonly)

:nodoc:



5
6
7
# File 'lib/thread_storm/execution.rb', line 5

def thread
  @thread
end

#valueObject

The value returned by the execution’s code block.



84
85
86
# File 'lib/thread_storm/execution.rb', line 84

def value
  @value
end

Instance Method Details

#durationObject

How long this this execution ran for (i.e. finish_time - start_time) or if it hasn’t finished, how long it has been running for.



54
55
56
57
58
59
60
# File 'lib/thread_storm/execution.rb', line 54

def duration
  if finished?
    finish_time - start_time
  else
    Time.now - start_time
  end
end

#finish!Object

:nodoc:



35
36
37
38
39
40
# File 'lib/thread_storm/execution.rb', line 35

def finish! #:nodoc:
  @mutex.synchronize do
    @finish_time = Time.now
    @join.signal
  end
end

#finish_timeObject

When this execution finished running (either cleanly or with error).



48
49
50
# File 'lib/thread_storm/execution.rb', line 48

def finish_time
  @finish_time
end

#finished?Boolean

True if this execution has finished running.

Returns:

  • (Boolean)


43
44
45
# File 'lib/thread_storm/execution.rb', line 43

def finished?
  !!finish_time
end

#joinObject

Block until this execution has finished running.



72
73
74
75
76
# File 'lib/thread_storm/execution.rb', line 72

def join
  @mutex.synchronize do
    @join.wait(@mutex) unless finished?
  end
end

#start!Object

:nodoc:



20
21
22
23
# File 'lib/thread_storm/execution.rb', line 20

def start! #:nodoc:
  @thread = Thread.current
  @start_time = Time.now
end

#start_timeObject

When this execution began to run.



31
32
33
# File 'lib/thread_storm/execution.rb', line 31

def start_time
  @start_time
end

#started?Boolean

True if this execution has started running.

Returns:

  • (Boolean)


26
27
28
# File 'lib/thread_storm/execution.rb', line 26

def started?
  !!start_time
end

#timed_out!Object

:nodoc:



62
63
64
# File 'lib/thread_storm/execution.rb', line 62

def timed_out! #:nodoc:
  @timed_out = true
end

#timed_out?Boolean

True if the execution went over the timeout limit.

Returns:

  • (Boolean)


67
68
69
# File 'lib/thread_storm/execution.rb', line 67

def timed_out?
  !!@timed_out
end