Sha256: 27b46071a91a701320aa9a671153a746bda89db109aef5d3fb07f26afc4884af

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

module Houston

  def self.daemonize(name)
    unless Houston.running_as_web_server? or ENV["HOUSTON_DAEMONS"].to_s.split(",").member?(name)
      puts "\e[94m[daemon:#{name}] Skipping daemon since we're not running as a server\e[0m" if Rails.env.development?
      Rails.logger.info "\e[94m[daemon:#{name}] Skipping daemon since we're not running as a server\e[0m"
      return
    end

    puts "\e[94m[daemon:#{name}] Connecting\e[0m" if Rails.env.development?
    Rails.logger.info "\e[94m[daemon:#{name}] Connecting\e[0m"
    Thread.new do
      begin
        connected_at = Time.now
        Houston.observer.fire "daemon:#{name}:start"
        yield

      rescue Exception
        puts "\e[91m#{$!.class}: #{$!.message}\e[0m" if Rails.env.development?
        Houston.report_exception $!
        unless (Time.now - connected_at) < 60
          Houston.observer.fire "daemon:#{name}:restart"
          retry
        end
      end

      # This should never happen
      puts "\e[31m[daemon:#{name}] Disconnected\e[0m" if Rails.env.development?
      Rails.logger.error "\e[31m[daemon:#{name}] Disconnected\e[0m"

      # http://stackoverflow.com/a/3516003/731300
      Rails.logger.flush
      Houston.observer.fire "daemon:#{name}:stop"
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
houston-core-0.9.2 lib/houston/boot/daemonize.rb
houston-core-0.9.1 lib/houston/boot/daemonize.rb
houston-core-0.9.0 lib/houston/boot/daemonize.rb
houston-core-0.9.0.rc1 lib/houston/boot/daemonize.rb
houston-core-0.8.4 lib/houston/boot/daemonize.rb
houston-core-0.8.3 lib/houston/boot/daemonize.rb
houston-core-0.8.2 lib/houston/boot/daemonize.rb
houston-core-0.8.1 lib/houston/boot/daemonize.rb
houston-core-0.8.0 lib/houston/boot/daemonize.rb
houston-core-0.8.0.pre2 lib/houston/boot/daemonize.rb