Module: DummyLogGenerator::Worker

Defined in:
lib/dummy_log_generator/worker.rb

Constant Summary collapse

BIN_NUM =
10

Instance Method Summary collapse

Instance Method Details

#initializeObject



7
8
9
# File 'lib/dummy_log_generator/worker.rb', line 7

def initialize
  reload
end

#reloadObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dummy_log_generator/worker.rb', line 11

def reload
  @generator = Generator.new(config[:setting])
  @rate = config[:setting].rate

  output = config[:setting].output
  if output.respond_to?(:write) and output.respond_to?(:close)
    @output = output
  else
    @output = open(output, (File::WRONLY | File::APPEND | File::CREAT))
    @output.sync = true
  end
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dummy_log_generator/worker.rb', line 24

def run
  batch_num    = (@rate / BIN_NUM).to_i
  residual_num = (@rate % BIN_NUM)
  while !@stop
    current_time = Time.now.to_i
    BIN_NUM.times do
      break unless (!@stop && Time.now.to_i <= current_time)
      wait(0.1) { write(batch_num) }
    end
    write(residual_num)
    # wait for next second
    while !@stop && Time.now.to_i <= current_time
      sleep 0.01
    end
  end
ensure
  @output.close
end

#stopObject



43
44
45
# File 'lib/dummy_log_generator/worker.rb', line 43

def stop
  @stop = true
end