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, default_value, &block) ⇒ Execution

:nodoc:



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

def initialize(args, default_value, &block) #:nodoc:
  @args = args
  @value = default_value
  @block = block
  @start_time = nil
  @finish_time = nil
  @exception = nil
  @timed_out = false
  @thread = nil
  @lock = Monitor.new
  @cond = @lock.new_cond
end

Instance Attribute Details

#argsObject (readonly)

:nodoc:



7
8
9
# File 'lib/thread_storm/execution.rb', line 7

def args
  @args
end

#blockObject (readonly)

:nodoc:



7
8
9
# File 'lib/thread_storm/execution.rb', line 7

def block
  @block
end

#exceptionObject

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



81
82
83
# File 'lib/thread_storm/execution.rb', line 81

def exception
  @exception
end

#threadObject (readonly)

:nodoc:



7
8
9
# File 'lib/thread_storm/execution.rb', line 7

def thread
  @thread
end

#valueObject

The value returned by the execution’s code block.



86
87
88
# File 'lib/thread_storm/execution.rb', line 86

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.



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

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

#finish!Object

:nodoc:



37
38
39
40
41
42
# File 'lib/thread_storm/execution.rb', line 37

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

#finish_timeObject

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



50
51
52
# File 'lib/thread_storm/execution.rb', line 50

def finish_time
  @finish_time
end

#finished?Boolean

True if this execution has finished running.

Returns:

  • (Boolean)


45
46
47
# File 'lib/thread_storm/execution.rb', line 45

def finished?
  !!finish_time
end

#joinObject

Block until this execution has finished running.



74
75
76
77
78
# File 'lib/thread_storm/execution.rb', line 74

def join
  @lock.synchronize do
    @cond.wait_until{ finished? }
  end
end

#start!Object

:nodoc:



22
23
24
25
# File 'lib/thread_storm/execution.rb', line 22

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

#start_timeObject

When this execution began to run.



33
34
35
# File 'lib/thread_storm/execution.rb', line 33

def start_time
  @start_time
end

#started?Boolean

True if this execution has started running.

Returns:

  • (Boolean)


28
29
30
# File 'lib/thread_storm/execution.rb', line 28

def started?
  !!start_time
end

#timed_out!Object

:nodoc:



64
65
66
# File 'lib/thread_storm/execution.rb', line 64

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

#timed_out?Boolean

True if the execution went over the timeout limit.

Returns:

  • (Boolean)


69
70
71
# File 'lib/thread_storm/execution.rb', line 69

def timed_out?
  !!@timed_out
end