Class: DLogReader::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_logreader/distributer/pandemic_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler, num_threads = 10) ⇒ Processor

Returns a new instance of Processor.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/distributed_logreader/distributer/pandemic_processor.rb', line 5

def initialize(handler, num_threads = 10)
  read_from_parent, write_to_child = IO.pipe
  read_from_child, write_to_parent = IO.pipe
  
  @child_process_id = fork
  if @child_process_id
    # I'm the parent
    write_to_parent.close
    read_from_parent.close
    @out = write_to_child
    @in = read_from_child
    @max_queue_size = 100
    @counter = MutexCounter.new
    @job_mutex = Mutex.new
    wait_for_responses
  else
    $dlog_logger.debug("Forked")
    # I'm the child
    write_to_child.close
    read_from_child.close
    @out = write_to_parent
    @in = read_from_parent
    @handler = handler
    @job_queue = Queue.new
    @response_queue = Queue.new
    wait_for_job_completion
    num_threads.times do
      create_thread
    end
    wait_for_jobs
  end
end

Instance Method Details

#close(status = 0) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/distributed_logreader/distributer/pandemic_processor.rb', line 62

def close(status = 0)
  if parent? && child_alive?
    Process.detach(@child_process_id)
    @out.puts(status.to_s)
    @out.close
    @in.close
  else
    Process.exit!(status)
  end
end

#closed?Boolean

Returns:



73
74
75
# File 'lib/distributed_logreader/distributer/pandemic_processor.rb', line 73

def closed?
  !child_alive?
end

#num_jobsObject



38
39
40
41
42
# File 'lib/distributed_logreader/distributer/pandemic_processor.rb', line 38

def num_jobs
  if parent?
    @counter.real_total
  end
end

#process(body) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/distributed_logreader/distributer/pandemic_processor.rb', line 44

def process(body)
  if parent?
    while(@counter.real_total > @max_queue_size)
      $dlog_logger.debug("Max process queue size: #{@counter.real_total}")
      sleep(0.01)
    end
    body = (body.chomp + "\n")
    @job_mutex.synchronize do
      @out.write(body)
      in_queue = @counter.inc
      $dlog_logger.debug("Parent: writing #{body.inspect} - #{in_queue}")
    end
  else
    $dlog_logger.debug("Child Processing #{body}")
    return @handler.call(body)
  end
end