Sha256: 18dd28d7f589c5c47c32eef2e0bbc0cbdab1dd54f8b6e3bc774bd69b7db8bf09

Contents?: true

Size: 978 Bytes

Versions: 22

Compression:

Stored size: 978 Bytes

Contents

#!/usr/bin/env ruby
# encoding: utf-8

require 'rubygems'
require 'syslogger'

class Runner
  attr_accessor :nextnr, :children

  def initialize
    @terminate = false
    self.nextnr = 1
    self.children = []
  end

  def create_child
    nr = nextnr
    self.nextnr += 1
    pid = fork do
      logger = Syslogger.new("testsv#{nr}", Syslog::LOG_PID, Syslog::LOG_LOCAL1)
      loop do
        logger.info "You from worker #{nr}"
        sleep 5
      end
    end
    children << pid
  end

  def terminate!
    @terminate = true
  end

  def terminate?
    !! @terminate
  end

  def run
    10.times { create_child }
    until terminate?
      child_pid = Process.wait(-1, Process::WNOHANG)
      if child_pid
        children.delete_if { |pid| pid == child_pid }
        create_child
      else
        sleep 1
      end
    end
    children.each { |pid| Process.kill('TERM', pid) }
  end
end

runner = Runner.new

Signal.trap('TERM') do
  runner.terminate!
end

runner.run

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
runit-man-2.3.18 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.3.17 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb