aboutsummaryrefslogtreecommitdiff
path: root/bin/jmauveserver
blob: 9cdd26fe1b274f67c4f553951da8a219f426a716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env jruby

begin
  eval "Proc.new { |a,&b| }"
rescue SyntaxError => no_blocks_with_procs
  STDERR.print "mauveserver must have Ruby 1.8.7 or later, sorry (if you "+
    "try, you'll get a SyntaxError trying to load one of its libraries)\n"
  exit 1
end
  
require 'mauve/configuration'
include Mauve

configuration_file = ARGV[0]
if !configuration_file
  if File.exists?("/etc/mauvealert/mauveserver.conf")
    configuration_file = "/etc/mauvealert/mauveserver.conf"
  else
    STDERR.print "Syntax: #{$0} <configuration filename>\n"
    exit 1
  end
end
Configuration.current = ConfigurationBuilder.load(configuration_file)

class RestartSignalReceived < Exception; end
trap("HUP") do
  # this blows up if you do it twice in quick succession, but don't really 
  # care about that case as it's only for log rotation.
  Thread.main.raise(RestartSignalReceived.new) 
end

# Start canary thread.
#require 'canary'
#Canary.start(1, Log)

# Main loop
@logger = Log4r::Logger.new "mauve::server<#{Process.pid}>"
loop do
  begin
    Configuration.current.server.run
  rescue Interrupt
    @logger.info "Interrupted by user, exiting"
    Configuration.current.close
    exit 0
  rescue RestartSignalReceived => ex
    @logger.info "Restart signal received, reloading the configuration"
    begin
      new_configuration = ConfigurationBuilder.load(configuration_file)
    rescue Exception => ex
      @logger.error "Error reloading configuration, reusing old one: #{ex}"
      @logger.debug ex.backtrace.join("\n")
    end
    
    Configuration.current.close      
    Configuration.current = new_configuration
  rescue Exception => ex
    @logger.fatal("Uncaught #{ex.class} exception, will try to log")
    @logger.fatal("Class: #{ex.class} "+
                  "Message: #{ex.message} "+
                  "Backtrace: #{ex.backtrace.join("\n")}")
    exit 9
  end
end