Class: ConcurrentWorker::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **options, &work_block) ⇒ Worker

Returns a new instance of Worker.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/concurrent_worker/worker.rb', line 22

def initialize(*args, **options, &work_block)
  @args = args
  @options = options
  set_block(:work_block, &work_block) if work_block
  
  @state = :idle
  @result_callbacks = []
  @retired_callbacks = []

  @snd_queue_max = @options[:snd_queue_max] || 2
  @req_counter = RequestCounter.new
  @options[:result_callback_interrupt]  ||= :immediate 
  @options[:retired_callback_interrupt] ||= :immediate
  @undone_requests = []

  case @options[:type]
  when :process
    class << self
      include ConcurrentProcess
    end
  when :thread
    class << self
      include ConcurrentThread
    end
  else
    class << self
      include ConcurrentThread
    end
  end
end

Instance Attribute Details

#req_counterObject (readonly)

Worker : worker class

+cncr_block         : concurrent processing block : thread(as ConcurrentThread)/process(as ConcurrentProcess)
  +base_block       : user defined preparation to exec 'work block'
    +loop_block     : loop of receiving request and exec 'work block'
      +work_block   : user requested work

These blocks are executed with ‘instance_exec’ method of worker, so that they can share instance variables:@xxxx.



13
14
15
# File 'lib/concurrent_worker/worker.rb', line 13

def req_counter
  @req_counter
end

#snd_queue_maxObject (readonly)

Worker : worker class

+cncr_block         : concurrent processing block : thread(as ConcurrentThread)/process(as ConcurrentProcess)
  +base_block       : user defined preparation to exec 'work block'
    +loop_block     : loop of receiving request and exec 'work block'
      +work_block   : user requested work

These blocks are executed with ‘instance_exec’ method of worker, so that they can share instance variables:@xxxx.



13
14
15
# File 'lib/concurrent_worker/worker.rb', line 13

def snd_queue_max
  @snd_queue_max
end

#undone_requestsObject (readonly)

Worker : worker class

+cncr_block         : concurrent processing block : thread(as ConcurrentThread)/process(as ConcurrentProcess)
  +base_block       : user defined preparation to exec 'work block'
    +loop_block     : loop of receiving request and exec 'work block'
      +work_block   : user requested work

These blocks are executed with ‘instance_exec’ method of worker, so that they can share instance variables:@xxxx.



13
14
15
# File 'lib/concurrent_worker/worker.rb', line 13

def undone_requests
  @undone_requests
end

Instance Method Details

#add_callback(&callback) ⇒ Object



53
54
55
56
# File 'lib/concurrent_worker/worker.rb', line 53

def add_callback(&callback)
  raise "block is nil" unless callback
  @result_callbacks.push(callback)
end

#add_retired_callback(&callback) ⇒ Object



72
73
74
75
# File 'lib/concurrent_worker/worker.rb', line 72

def add_retired_callback(&callback)
  raise "block is nil" unless callback
  @retired_callbacks.push(callback)
end

#call_result_callbacks(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/concurrent_worker/worker.rb', line 61

def call_result_callbacks(args)
  Thread.handle_interrupt(Object => :never) do        
    Thread.handle_interrupt(Object => @options[:result_callback_interrupt]) do
      @result_callbacks.each do |callback|
        callback.call(*args)
      end
    end
    @req_counter.pop
  end
end

#call_retired_callbacksObject



80
81
82
83
84
85
86
# File 'lib/concurrent_worker/worker.rb', line 80

def call_retired_callbacks
  Thread.handle_interrupt(Object => @options[:retired_callback_interrupt]) do
    @retired_callbacks.each do |callback|
      callback.call
    end
  end
end

#clear_callbacksObject



57
58
59
# File 'lib/concurrent_worker/worker.rb', line 57

def clear_callbacks
  @result_callbacks.clear
end

#clear_retired_callbacksObject



76
77
78
# File 'lib/concurrent_worker/worker.rb', line 76

def clear_retired_callbacks
  @retired_callbacks.clear
end

#define_block(symbol, &block) ⇒ Object



112
113
114
115
116
117
# File 'lib/concurrent_worker/worker.rb', line 112

def define_block(symbol,&block)
  worker_block = Proc.new do |*args|
    self.instance_exec(*args, &block)
  end
  instance_variable_set("@" + symbol.to_s, worker_block)
end

#define_block_yield(symbol) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/concurrent_worker/worker.rb', line 119

def define_block_yield(symbol)
  define_singleton_method("yield_" + symbol.to_s) do |*args|
    blk = instance_variable_get("@" + symbol.to_s)
    if blk
      blk.call(*args)
    else
      raise "block " + symbol.to_s + " is not defined"
    end
  end
end

#joinObject



191
192
193
194
195
196
197
198
199
# File 'lib/concurrent_worker/worker.rb', line 191

def join
  unless @state == :run
    return true
  end
  @req_counter.wait_until_less_than(1)
  quit
  wait_cncr_proc
  true
end

#queue_closed?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/concurrent_worker/worker.rb', line 15

def queue_closed?
  @req_counter.closed?
end

#queue_empty?Boolean

Returns:

  • (Boolean)


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

def queue_empty?
  !queue_closed? && @req_counter.size == 0
end

#quitObject



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/concurrent_worker/worker.rb', line 179

def quit
  unless @state == :run
    return
  end
  begin 
    send_req(nil)
    true
  rescue ClosedQueueError, IOError
    false
  end
end

#req(*args, &work_block) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/concurrent_worker/worker.rb', line 165

def req(*args, &work_block)
  unless @state == :run
    run
  end
  @req_counter.wait_until_less_than(@snd_queue_max) if @snd_queue_max > 0
  begin 
    @req_counter.push([args, work_block])
    send_req([args, work_block])
    true
  rescue ClosedQueueError, IOError
    false
  end
end

#req_counter_closeObject



88
89
90
91
92
93
# File 'lib/concurrent_worker/worker.rb', line 88

def req_counter_close
  @req_counter.close
  until @req_counter.empty?
    @undone_requests.push(@req_counter.pop)
  end
end

#result_handle_thread(&recv_block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/concurrent_worker/worker.rb', line 95

def result_handle_thread(&recv_block)
  Thread.new do
    Thread.handle_interrupt(Object => :never) do
      begin
        Thread.handle_interrupt(Object => :immediate) do
          recv_block.call
        end
      ensure
        req_counter_close
        channel_close
        call_retired_callbacks
      end
    end
  end
end

#runObject



158
159
160
161
162
163
# File 'lib/concurrent_worker/worker.rb', line 158

def run
  @state = :run
  set_default_loop_block unless defined?(@loop_block) && @loop_block
  set_default_base_block unless defined?(@base_block) && @base_block
  cncr_block
end

#set_block(symbol, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/concurrent_worker/worker.rb', line 130

def set_block(symbol, &block)
  raise "block is nil" unless block
  
  unless [:base_block, :loop_block, :work_block].include?(symbol)
    raise symbol.to_s + " is not used as worker block"
  end
  define_block(symbol,&block)
  define_block_yield(symbol)
end

#set_default_base_blockObject



152
153
154
155
156
# File 'lib/concurrent_worker/worker.rb', line 152

def set_default_base_block
  set_block(:base_block) do
    yield_loop_block
  end
end

#set_default_loop_blockObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/concurrent_worker/worker.rb', line 140

def set_default_loop_block
  set_block(:loop_block) do
    while req = receive_req
      (args, work_block) = req
      if work_block
        set_block(:work_block, &work_block)
      end
      send_res([yield_work_block(*args)])
    end
  end
end