Class: Localeapp::CLI::Daemon

Inherits:
Command
  • Object
show all
Defined in:
lib/localeapp/cli/daemon.rb

Instance Method Summary collapse

Methods inherited from Command

#initialize, #initialize_config, #load_config_file, #set_command_line_arguments

Constructor Details

This class inherits a constructor from Localeapp::CLI::Command

Instance Method Details

#do_updateObject



25
26
27
# File 'lib/localeapp/cli/daemon.rb', line 25

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

#execute(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/localeapp/cli/daemon.rb', line 4

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

#kill_existingObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/localeapp/cli/daemon.rb', line 42

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

#run_in_background(interval) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/localeapp/cli/daemon.rb', line 29

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

#update_loop(interval) ⇒ Object



18
19
20
21
22
23
# File 'lib/localeapp/cli/daemon.rb', line 18

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