add --no-syslog
[god.git] / lib / god / cli / run.rb
blobd4e838c8e1176289001d75569a1f9efed8ac4376
1 module God
2   module CLI
3     
4     class Run
5       def initialize(options)
6         @options = options
7         
8         dispatch
9       end
10       
11       def dispatch
12         # have at_exit start god
13         $run = true
14         
15         if @options[:daemonize]
16           run_daemonized
17         else
18           run_in_front
19         end
20       end
21       
22       def run_daemonized
23         # trap and ignore SIGHUP
24         Signal.trap('HUP') {}
25         
26         pid = fork do
27           begin
28             require 'god'
29             
30             log_file = @options[:log] || "/dev/null"
31             
32             unless God::EventHandler.loaded?
33               puts
34               puts "***********************************************************************"
35               puts "*"
36               puts "* Event conditions are not available for your installation of god."
37               puts "* You may still use and write custom conditions using the poll system"
38               puts "*"
39               puts "***********************************************************************"
40               puts
41             end
42             
43             # set port if requested
44             if @options[:port]
45               God.port = @options[:port]
46             end
47             
48             # set pid if requested
49             if @options[:pid]
50               God.pid = @options[:pid] 
51             end
52             
53             unless @options[:syslog]
54               Logger.syslog = false
55             end
56             
57             # load config
58             if @options[:config]
59               # set log level, defaults to WARN
60               if @options[:log_level]
61                 God.log_level = @options[:log_level]
62               else
63                 God.log_level = :warn
64               end
65               
66               unless File.exist?(@options[:config])
67                 abort "File not found: #{@options[:config]}"
68               end
69               
70               begin
71                 load File.expand_path(@options[:config])
72               rescue Exception => e
73                 if e.instance_of?(SystemExit)
74                   raise
75                 else
76                   puts e.message
77                   puts e.backtrace.join("\n")
78                   abort "There was an error in your configuration file (see above)"
79                 end
80               end
81             end
82             
83             # reset file descriptors
84             STDIN.reopen "/dev/null"
85             STDOUT.reopen(log_file, "a")
86             STDERR.reopen STDOUT
87           rescue => e
88             puts e.message
89             puts e.backtrace.join("\n")
90             abort "There was a fatal system error while starting god (see above)"
91           end
92         end
93         
94         if @options[:pid]
95           File.open(@options[:pid], 'w') { |f| f.write pid }
96         end
97         
98         ::Process.detach pid
99         
100         exit
101       end
102       
103       def run_in_front
104         require 'god'
105         
106         if @options[:port]
107           God.port = @options[:port]
108         end
109         
110         # set log level if requested
111         if @options[:log_level]
112           God.log_level = @options[:log_level]
113         end
114         
115         if @options[:config]
116           unless File.exist?(@options[:config])
117             abort "File not found: #{@options[:config]}"
118           end
119           
120           begin
121             load File.expand_path(@options[:config])
122           rescue Exception => e
123             if e.instance_of?(SystemExit)
124               raise
125             else
126               puts e.message
127               puts e.backtrace.join("\n")
128               abort "There was an error in your configuration file (see above)"
129             end
130           end
131         end
132       end
133     end # Run
134     
135   end