Sha256: 1a107b130ac5d292b21db8d17ab9f3986e7753c09902c71eba03a622495ab25b

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module Slacky
  class Daemon

    def initialize(config, bot_class)
      @config = config
      @bot_class = bot_class
      @active = true
      @running = false
    end

    def start(daemonize = true)
      Process.daemon if daemonize
      write_pid

      [ 'HUP', 'INT', 'QUIT', 'TERM' ].each do |sig|
        Signal.trap(sig) do
          @config.log "Interrupted with signal: #{sig}"
          kill
        end
      end

      begin
        @slackthread = Thread.new do
          bot = Bot.new @config
          bot.run
          @bot_class.new bot
        end
        run
      rescue => e
        @config.log "Unexpected error", e
      ensure
        cleanup
      end
    end

    def cleanup
      delete_pid
    end

    private

    def run
      @config.log "#{@config.name} is running."
      while active? do
        sleep 0.5
      end
      @config.log "#{@config.name} got killed"
      @slackthread.kill
    end

    def active?
      @active
    end

    def kill
      @active = false
    end

    def write_pid
      File.open @config.pid_file, 'w' do |f|
        f.write Process.pid.to_s
      end
    end

    def delete_pid
      File.delete @config.pid_file if File.exists? @config.pid_file
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slacky-0.1.2 lib/slacky/daemon.rb
slacky-0.1.1 lib/slacky/daemon.rb