#!/usr/bin/env ruby # frozen_string_literal: true $stdout.sync = true $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) require 'optparse' require 'drb' require 'yaml' begin # Save ARGV in case someone wants to use it later ORIGINAL_ARGV = ARGV.dup options = { daemonize: true, port: 17165, syslog: true, events: true } opts = OptionParser.new do |o| o.banner = <<-BANNER Usage: Starting: god [-c ] [-p | -b] [-P ] [-l ] [-D] Querying: god [-p ] god [-p ] god -v god -V (must be run as root to be accurate on Linux) Commands: start start task or group restart restart task or group stop stop task or group monitor monitor task or group unmonitor unmonitor task or group remove remove task or group from god load [action] load a config into a running god log show realtime log for given task status [task or group name] show status signal signal all matching tasks quit stop god terminate stop god and all tasks check run self diagnostic Options: BANNER o.on('-cCONFIG', '--config-file CONFIG', 'Configuration file') do |x| options[:config] = x end o.on('-pPORT', '--port PORT', 'Communications port (default 17165)') do |x| options[:port] = x end o.on('-b', '--auto-bind', 'Auto-bind to an unused port number') do options[:port] = '0' end o.on('-PFILE', '--pid FILE', 'Where to write the PID file') do |x| options[:pid] = x end o.on('-lFILE', '--log FILE', 'Where to write the log file') do |x| options[:log] = x end o.on('-D', '--no-daemonize', "Don't daemonize") do options[:daemonize] = false end o.on('-v', '--version', 'Print the version number and exit') do options[:version] = true end o.on('-V', 'Print extended version and build information') do options[:info] = true end o.on('--log-level LEVEL', 'Log level [debug|info|warn|error|fatal]') do |x| options[:log_level] = x.to_sym end o.on('--no-syslog', 'Disable output to syslog') do options[:syslog] = false end o.on('--attach PID', 'Quit god when the attached process dies') do |x| options[:attach] = x end o.on('--no-events', 'Disable the event system') do options[:events] = false end o.on('--bleakhouse', 'Enable bleakhouse profiling') do options[:bleakhouse] = true end end opts.parse! # validate if options[:log_level] && ![:debug, :info, :warn, :error, :fatal].include?(options[:log_level]) abort("Invalid log level '#{options[:log_level]}'") end # Use this flag to actually load all of the god infrastructure $load_god = true # dispatch if !options[:config] && options[:version] require 'god' God::CLI::Version.version elsif !options[:config] && options[:info] require 'god' God::EventHandler.load God::CLI::Version.version_extended elsif !options[:config] && (command = ARGV[0]) require 'god' God::EventHandler.load God::CLI::Command.new(command, options, ARGV) else require 'god/cli/run' God::CLI::Run.new(options) end rescue Exception => e raise if e.instance_of?(SystemExit) puts 'Uncaught exception' puts e.message puts e.backtrace.join("\n") end