Class: Jah::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/jah/agent.rb

Constant Summary collapse

PID_FILE =
File.join("/tmp", "jah.pid")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Agent

, config)



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jah/agent.rb', line 15

def initialize(options=nil)#, config)
  if Opt.daemon?
    puts "Jah starting in background.."
    fork do
      daemonize
      run_agent_run
    end
    exit
  end
  run_agent_run
end

Class Method Details

.configObject Also known as: install



64
65
66
# File 'lib/jah/agent.rb', line 64

def self.config
  Install.new
end

.restartObject



58
59
60
61
62
# File 'lib/jah/agent.rb', line 58

def self.restart
  stop
  sleep 2
  start
end

.startObject



39
40
41
42
43
44
45
# File 'lib/jah/agent.rb', line 39

def self.start
  if File.exists?(PID_FILE)
    puts "Jah already running...#{PID_FILE}"
  else
    new
  end
end

.stopObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/jah/agent.rb', line 47

def self.stop
  if File.exists?(PID_FILE)
    pid = File.read(PID_FILE)
    puts "Stopping #{pid}"
    `kill #{pid}`
  else
    puts "Jah not running..."
    exit
  end
end

Instance Method Details

#daemonizeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jah/agent.rb', line 72

def daemonize
  begin
    File.open(pid_file, File::CREAT|File::EXCL|File::WRONLY) do |pid|
      pid.puts $$
    end
    at_exit do
      begin
        File.unlink(pid_file)
      rescue
        Log.error "Unable to unlink pid file: #{$!.message}"
      end
    end
  rescue
    pid = File.read(pid_file).strip.to_i rescue "unknown"
    running = true
    begin
      Process.kill(0, pid)
      if stat = File.stat(pid_file)
        if mtime = stat.mtime
          if Time.now - mtime > 25 * 60 # assume process is hung after 25m
            Log.info "Trying to KILL an old process..."
            Process.kill("KILL", pid)
            running = false
          end
        end
      end
    rescue Errno::ESRCH
      running = false
    rescue
      # do nothing, we didn't have permission to check the running process
    end
    if running
      if pid == "unknown"
        Log.warn "Could not create or read PID file. " +
                 "You may need to the path to the config directory. " +
                 "See: http://scoutapp.com/help#data_file"
      else
        Log.warn "Process #{pid} was already running"
      end
      exit
    else
      Log.info "Stale PID file found. Clearing it and reloading..."
      File.unlink(pid_file) rescue nil
      retry
    end
  end

  self
end

#pid_fileObject



27
28
29
# File 'lib/jah/agent.rb', line 27

def pid_file
  @pidfile ||= PID_FILE
end

#run_agent_runObject



31
32
33
34
35
36
37
# File 'lib/jah/agent.rb', line 31

def run_agent_run
  case Opt.mode # @mode
  when "xmpp" then  XmppAgent.new.run
  when "post" then  PostAgent.new.run
  else DumpAgent.new
  end
end