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.4.14 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.12 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.11 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.10 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.9 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.8 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.7 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.6 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.5 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.4 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.3 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.2 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.1 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.1a1 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.0 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.0a2 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.4.0a1 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.3.21 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.3.20 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb
runit-man-2.3.19 goodies/test-service-for-logger-with-multiple-logs/rubyscript.rb