module DummyLogGenerator::Worker

Constants

BIN_NUM

Public Class Methods

new() click to toggle source
# File lib/dummy_log_generator/worker.rb, line 7
def initialize
  reload
end

Public Instance Methods

reload() click to toggle source
# 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
run() click to toggle source
# 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
stop() click to toggle source
# File lib/dummy_log_generator/worker.rb, line 43
def stop
  @stop = true
end

Private Instance Methods

wait(time) { || ... } click to toggle source
# File lib/dummy_log_generator/worker.rb, line 53
def wait(time)
  start_time = Time.now
  yield
  sleep_time = time - (Time.now - start_time)
  sleep sleep_time if sleep_time > 0
end
write(num) click to toggle source
# File lib/dummy_log_generator/worker.rb, line 49
def write(num)
  num.times { @output.write @generator.generate }
end