Class: HandlingQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/handling_queue.rb,
lib/handling_queue/request.rb,
lib/handling_queue/version.rb

Overview

Execute handling process from time to time.

Defined Under Namespace

Classes: Request

Constant Summary collapse

VERSION =

Library version.

'1.1.0'

Instance Method Summary collapse

Constructor Details

#initialize(slice: 1, interval: 1) {|obj| ... } ⇒ HandlingQueue

Returns a new instance of HandlingQueue.

Parameters:

  • slice (Integer) (defaults to: 1)

    amount of handled objects in single handler call.

  • interval (Integer) (defaults to: 1)

    interval between handler calls in seconds.

Yield Parameters:

  • obj (Array<Request>)

    array of handling requests.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/handling_queue.rb', line 11

def initialize(slice: 1, interval: 1, &handler)
  @slice = slice
  @interval = interval
  @handler = handler

  @requests_queue = [] # Use Array with #push and #shift methods to have queue.
  # NOTE: effeciency is pretty good, btw. O(1) if no new memory required.

  @mutex = Mutex.new # Queue and requests accessing mutex
  @cv = ConditionVariable.new # Condition variable to signal when some of results are ready.

  @working = false
  @thread = nil
  start_handler_thread
end

Instance Method Details

#handle(obj) ⇒ void

This method returns an undefined value.

Request handling of obj and wait for it to be handled.

Parameters:

  • obj (void)

    object to be handled.



30
31
32
33
34
35
36
37
# File 'lib/handling_queue.rb', line 30

def handle(obj)
  request = Request.new(obj)
  @mutex.synchronize do
    @requests_queue.push(request)
    @cv.wait(@mutex) until request.re?
    request.re
  end
end