Class: God::CLI::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/god/cli/run.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Run

Returns a new instance of Run.



6
7
8
9
10
# File 'lib/god/cli/run.rb', line 6

def initialize(options)
  @options = options

  dispatch
end

Instance Method Details

#attachObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/god/cli/run.rb', line 26

def attach
  process = System::Process.new(@options[:attach])
  Thread.new do
    loop do
      unless process.exists?
        applog(nil, :info, "Going down because attached process #{@options[:attach]} exited")
        exit!
      end
      sleep 5
    end
  end
end

#default_runObject



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
66
# File 'lib/god/cli/run.rb', line 39

def default_run
  # make sure we have STDIN/STDOUT redirected immediately
  setup_logging

  # start attached pid watcher if necessary
  attach if @options[:attach]

  God.port = @options[:port] if @options[:port]

  God::EventHandler.load if @options[:events]

  # set log level, defaults to WARN
  God.log_level = if @options[:log_level]
                    @options[:log_level]
                  else
                    @options[:daemonize] ? :warn : :info
                  end

  if @options[:config]
    abort "File not found: #{@options[:config]}" if !@options[:config].include?('*') && !File.exist?(@options[:config])

    # start the event handler
    God::EventHandler.start if God::EventHandler.loaded?

    load_config @options[:config]
  end
  setup_logging
end

#dispatchObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/god/cli/run.rb', line 12

def dispatch
  # have at_exit start god
  $run = true

  require 'god/sys_logger' if @options[:syslog]

  # run
  if @options[:daemonize]
    run_daemonized
  else
    run_in_front
  end
end

#load_config(config) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/god/cli/run.rb', line 126

def load_config(config)
  files = File.directory?(config) ? Dir['**/*.god'] : Dir[config]
  abort 'No files could be found' if files.empty?
  files.each do |god_file|
    abort "File '#{god_file}' could not be loaded" unless load_god_file(god_file)
  end
end

#load_god_file(god_file) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/god/cli/run.rb', line 134

def load_god_file(god_file)
  applog(nil, :info, "Loading #{god_file}")
  load File.expand_path(god_file)
  true
rescue Exception => e
  raise if e.instance_of?(SystemExit)

  puts "There was an error in #{god_file}"
  puts "\t#{e.message}"
  puts "\t#{e.backtrace.join("\n\t")}"
  false
end

#run_daemonizedObject



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
# File 'lib/god/cli/run.rb', line 74

def run_daemonized
  # trap and ignore SIGHUP
  Signal.trap('HUP') {} # block must be passed
  # trap and log-reopen SIGUSR1
  Signal.trap('USR1') { setup_logging }

  pid = fork do
    require 'god'

    # set pid if requested
    God.pid = @options[:pid] if @options[:pid] # and as daemon

    default_run

    unless God::EventHandler.loaded?
      puts
      puts '***********************************************************************'
      puts '*'
      puts '* Event conditions are not available for your installation of god.'
      puts '* You may still use and write custom conditions using the poll system'
      puts '*'
      puts '***********************************************************************'
      puts
    end
  rescue => e
    puts e.message
    puts e.backtrace.join("\n")
    abort 'There was a fatal system error while starting god (see above)'
  end

  File.write(@options[:pid], pid) if @options[:pid]

  ::Process.detach pid

  exit
end

#run_in_frontObject



68
69
70
71
72
# File 'lib/god/cli/run.rb', line 68

def run_in_front
  require 'god'

  default_run
end

#setup_loggingObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/god/cli/run.rb', line 111

def setup_logging
  log_file = God.log_file
  log_file = File.expand_path(@options[:log]) if @options[:log]
  log_file = '/dev/null' if !log_file && @options[:daemonize]
  return unless log_file

  puts "Sending output to log file: #{log_file}" unless @options[:daemonize]

  # reset file descriptors
  $stdin.reopen '/dev/null'
  $stdout.reopen(log_file, 'a')
  $stderr.reopen $stdout
  $stdout.sync = true
end