Sha256: 75740a5de002f9f4533c71661490cf3820ff7575e817b886a32f2b3f48655c14

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 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
        Thread.abort_on_exception = true
        @slackthread = Thread.new do
          bot = Bot.new @config
          @bot_class.new bot
          bot.run
        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

11 entries across 11 versions & 1 rubygems

Version Path
slacky-0.3.0 lib/slacky/daemon.rb
slacky-0.2.9 lib/slacky/daemon.rb
slacky-0.2.8 lib/slacky/daemon.rb
slacky-0.2.7 lib/slacky/daemon.rb
slacky-0.2.6 lib/slacky/daemon.rb
slacky-0.2.5 lib/slacky/daemon.rb
slacky-0.2.4 lib/slacky/daemon.rb
slacky-0.2.3 lib/slacky/daemon.rb
slacky-0.2.2 lib/slacky/daemon.rb
slacky-0.2.1 lib/slacky/daemon.rb
slacky-0.2 lib/slacky/daemon.rb