Sha256: 05775c410c13c4241f2523768810d7028f9aa579705f05cbf27d0898499bebce

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Slacky
  class Daemon

    def initialize(config, bot)
      @config = config
      @bot = bot
      @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 { @bot.run }
        run @bot
      rescue => e
        @config.log "Unexpected error", e
      ensure
        cleanup
      end
    end

    def cleanup
      delete_pid
    end

    private

    def run(slackbot)
      @config.log "#{@config.name} is running."
      while active? do
        # TODO: handle timed tasks
        #slackbot.ask_all if time.min % 10 == 0  # every 10 minutes
        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

1 entries across 1 versions & 1 rubygems

Version Path
slacky-0.1 lib/slacky/daemon.rb