Sha256: bcb08ca4b78baa437e1305e5f46d8b7a7cb69078bbca5c7af8c623c0bbecc5b1

Contents?: true

Size: 1.28 KB

Versions: 24

Compression:

Stored size: 1.28 KB

Contents

module Localeapp
  module CLI
    class Daemon < Command
      def execute(options)
        interval = options[:interval].to_i

        if interval <= 0
          exit_now! "interval must be a positive integer greater than 0", 1
        end

        if options[:background]
          run_in_background(interval)
        else
          update_loop(interval)
        end
      end

      def update_loop(interval)
        loop do
          do_update
          sleep interval
        end
      end

      def do_update
        Localeapp::CLI::Update.new.execute
      end

      def run_in_background(interval)
        kill_existing

        STDOUT.reopen(File.open(Localeapp.configuration.daemon_log_file, 'a'))
        pid = fork do
          Signal.trap('HUP', 'IGNORE')
          update_loop(interval)
        end
        Process.detach(pid)

        File.open(Localeapp.configuration.daemon_pid_file, 'w') {|f| f << pid}
      end

      def kill_existing
        if File.exist? Localeapp.configuration.daemon_pid_file
          begin
            daemon_pid = File.read(Localeapp.configuration.daemon_pid_file)
            Process.kill("QUIT", daemon_pid.to_i)
          rescue Errno::ESRCH
            File.delete(Localeapp.configuration.daemon_pid_file)
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
localeapp-3.3.0 lib/localeapp/cli/daemon.rb
localeapp-3.2.0 lib/localeapp/cli/daemon.rb
localeapp-3.1.3 lib/localeapp/cli/daemon.rb
localeapp-3.1.2 lib/localeapp/cli/daemon.rb
localeapp-3.1.1 lib/localeapp/cli/daemon.rb
localeapp-3.1.0 lib/localeapp/cli/daemon.rb
localeapp-3.0.1 lib/localeapp/cli/daemon.rb
localeapp-3.0.0 lib/localeapp/cli/daemon.rb
localeapp-2.5.0 lib/localeapp/cli/daemon.rb
localeapp-2.4.0 lib/localeapp/cli/daemon.rb
localeapp-2.3.0 lib/localeapp/cli/daemon.rb
localeapp-2.2.0 lib/localeapp/cli/daemon.rb
localeapp-2.1.1 lib/localeapp/cli/daemon.rb
localeapp-2.1.0 lib/localeapp/cli/daemon.rb
localeapp-2.0.0 lib/localeapp/cli/daemon.rb
localeapp-1.0.2 lib/localeapp/cli/daemon.rb
localeapp-1.0.1 lib/localeapp/cli/daemon.rb
localeapp-1.0.0 lib/localeapp/cli/daemon.rb
localeapp-0.9.3 lib/localeapp/cli/daemon.rb
localeapp-0.9.2 lib/localeapp/cli/daemon.rb