Class: ThreadMan

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor = nil) ⇒ ThreadMan

Returns a new instance of ThreadMan.



8
9
10
11
12
13
14
15
16
# File 'lib/thread_man.rb', line 8

def initialize(actor = nil)
  raise 'Not a valid actor object' if actor && !actor.is_a?(Celluloid)

  @init_actor = actor
  @futures = []
  @actors = []
  @response = []
  @next_response_ctr = 0
end

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#any_nil_response?Boolean

Returns:

  • (Boolean)


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

def any_nil_response?
  response.include? nil
end

#each_responseObject



86
87
88
89
90
# File 'lib/thread_man.rb', line 86

def each_response
  while resp = next_response
    yield resp
  end
end

#next_responseObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/thread_man.rb', line 47

def next_response
  if @futures.size <= 0
    raise 'No actors/requests submitted, cannot process response'
  elsif @next_response_ctr >= @futures.size
    nil
  else
    value = process_request(@next_response_ctr)
    @response << value
    @next_response_ctr += 1
    value
  end
end

#responseObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thread_man.rb', line 32

def response
  if @futures.size <= 0
    raise 'No actors/requests submitted, cannot process response'
  elsif @response.size == @futures.size
    @response
  else
    (@next_response_ctr..(@futures.size - 1)).each do |index|
      @response << process_request(index)
      @next_response_ctr += 1
    end

    @response
  end
end

#submit(method, *params) ⇒ Object



18
19
20
21
22
23
# File 'lib/thread_man.rb', line 18

def submit(method, *params)
  raise 'Not initialized with a valid actor object' if !@init_actor

  @futures << @init_actor.future(method.to_sym, *params)
  @actors << @init_actor
end

#submit_actor(actor, method, *params) ⇒ Object



25
26
27
28
29
30
# File 'lib/thread_man.rb', line 25

def submit_actor(actor, method, *params)
  raise 'Not a valid actor object' if !actor || !actor.is_a?(Celluloid)

  @futures << actor.future(method.to_sym, *params)
  @actors << actor
end

#tasks_running?Boolean

Returns:

  • (Boolean)


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

def tasks_running?
  @actors.uniq.each do |actor|
    actor.tasks.each { |task| return true if task.running? }
  end

  false
end

#terminate!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/thread_man.rb', line 60

def terminate!
  actor_arr = @actors.uniq

  if actor_arr.size > 0
    actor_arr.each do |actor|
      begin
        actor.terminate if actor.alive?
      rescue => e
        log "Terminating actor #{actor.inspect}: #{e.message}"
      end
    end

    @actors.clear
    @futures.clear
    @next_response_ctr = 0
  end
end