Class: ThreadPool::Task

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

Constant Summary collapse

Timeout =
Class.new(Exception)
Asked =
Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, *args, &block) ⇒ Task

Returns a new instance of Task.



20
21
22
23
24
# File 'lib/threadpool.rb', line 20

def initialize (pool, *args, &block)
	@pool      = pool
	@arguments = args
	@block     = block
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



18
19
20
# File 'lib/threadpool.rb', line 18

def exception
  @exception
end

#poolObject (readonly)

Returns the value of attribute pool.



18
19
20
# File 'lib/threadpool.rb', line 18

def pool
  @pool
end

#started_atObject (readonly)

Returns the value of attribute started_at.



18
19
20
# File 'lib/threadpool.rb', line 18

def started_at
  @started_at
end

#threadObject (readonly)

Returns the value of attribute thread.



18
19
20
# File 'lib/threadpool.rb', line 18

def thread
  @thread
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



18
19
20
# File 'lib/threadpool.rb', line 18

def timeout
  @timeout
end

Instance Method Details

#execute(thread) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/threadpool.rb', line 31

def execute (thread)
	return if terminated? || running? || finished?

	@thread     = thread
	@running    = true
	@started_at = Time.now

	pool.wake_up_timeout

	begin
		@block.call(*@arguments)
	rescue Exception => e
		if e.is_a? Timeout
			@timedout = true
		elsif e.is_a? Asked
			return
		else
			@exception = reason
		end
	end

	@running  = false
	@finished = true
	@thread   = nil
end

#finished?Boolean

Returns:

  • (Boolean)


27
# File 'lib/threadpool.rb', line 27

def finished?;   @finished;   end

#running?Boolean

Returns:

  • (Boolean)


26
# File 'lib/threadpool.rb', line 26

def running?;    @running;   end

#terminate!(exception = Asked) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/threadpool.rb', line 57

def terminate! (exception = Asked)
	return if terminated? || finished? || timeout?

	@terminated = true

	return unless running?

	@thread.raise exception
end

#terminated?Boolean

Returns:

  • (Boolean)


29
# File 'lib/threadpool.rb', line 29

def terminated?; @terminated; end

#timeout!Object



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

def timeout!
	terminate! Timeout
end

#timeout?Boolean

Returns:

  • (Boolean)


28
# File 'lib/threadpool.rb', line 28

def timeout?;    @timedout;   end

#timeout_after(time) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/threadpool.rb', line 71

def timeout_after (time)
	@timeout = time

	pool.timeout_for self, time

	self
end