lib/splash/commands.rb in prometheus-splash-0.1.0 vs lib/splash/commands.rb in prometheus-splash-0.1.1
- old
+ new
@@ -1,22 +1,19 @@
-require 'open3'
-require 'date'
-require 'socket'
-
+# coding: utf-8
module Splash
class CommandWrapper
include Splash::Templates
include Splash::Config
include Splash::Helpers
include Splash::Backends
+ include Splash::Exiter
def initialize(name)
@config = get_config
@name = name
unless @config.commands.keys.include? @name.to_sym then
- $stderr.puts "Splash : command #{@name} is not defined in configuration"
- exit 40
+ splash_exit case: :not_found, more: "command #{@name} is not defined in configuration"
end
@registry = Prometheus::Client.registry
@url = "http://#{@config.prometheus_pushgateway_host}:#{@config.prometheus_pushgateway_port}"
@metric_exitcode = Prometheus::Client::Gauge.new(:errorcode, docstring: 'SPLASH metric batch errorcode')
@metric_time = Prometheus::Client::Gauge.new(:exectime, docstring: 'SPLASH metric batch execution time')
@@ -24,30 +21,29 @@
@registry.register(@metric_time)
end
def ack
puts "Sending ack for command : '#{@name}'"
- notify(0)
- exit 0
+ notify(0,0)
end
def notify(value,time)
unless verify_service host: @config.prometheus_pushgateway_host ,port: @config.prometheus_pushgateway_port then
- $stderr.puts "Prometheus PushGateway Service IS NOT running"
- $stderr.puts "Exit without notification."
- exit 30
+ return { :case => :service_dependence_missing, :more => "Prometheus Notification not send."}
end
@metric_exitcode.set(value)
@metric_time.set(time)
hostname = Socket.gethostname
Prometheus::Client::Push.new(@name, hostname, @url).add(@registry)
puts " * Prometheus Gateway notified."
+ return { :case => :quiet_exit}
end
def call_and_notify(options)
puts "Executing command : '#{@name}' "
+ acase = { :case => :quiet_exit }
start = Time.now
start_date = DateTime.now.to_s
unless options[:trace] then
puts " * Traceless execution"
if @config.commands[@name.to_sym][:user] then
@@ -87,11 +83,11 @@
end
puts " => exitcode #{exit_code}"
if options[:notify] then
- notify(exit_code,time.to_i)
+ acase = notify(exit_code,time.to_i)
else
puts " * Without Prometheus notification"
end
if options[:callback] then
on_failure = (@config.commands[@name.to_sym][:on_failure])? @config.commands[@name.to_sym][:on_failure] : false
@@ -101,11 +97,11 @@
puts " * On failure callback : #{on_failure}"
if @config.commands.keys.include? on_failure then
@name = on_failure.to_s
call_and_notify options
else
- $stderr.puts "on_failure call error : configuration mistake : #{on_failure} command inexistant."
+ acase = { :case => :configuration_error , :more => "on_failure call error : #{on_failure} command inexistant."}
end
end
if on_success and exit_code == 0 then
puts " * On success callback : #{on_success}"
if @config.commands.keys.include? on_success then
@@ -116,10 +112,9 @@
end
end
else
puts " * Without callbacks sequences"
end
-
- exit exit_code
+ return acase
end
end
end