Class: Observed::Application::Oneshot

Inherits:
Object
  • Object
show all
Defined in:
lib/observed/application/oneshot.rb

Overview

The application which is usually ran from CLI to run health-checks and write the results to a log file, and then exit. An “Oneshot” application is the opposite of a “Daemon” or “Resident” application.

Defined Under Namespace

Classes: InvalidArgumentError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, sys) ⇒ Oneshot

Returns a new instance of Oneshot.

Parameters:



18
19
20
21
# File 'lib/observed/application/oneshot.rb', line 18

def initialize(config, sys)
  @config = config
  @system = sys
end

Class Method Details

.create(args) ⇒ Object

Parameters:

  • args (Hash<Symbol,String>)

Options Hash (args):

  • :argv (Array<String>)

    The Ruby’s ‘ARGV` like object which is treated as intialization parameters for Oneshoft application.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/observed/application/oneshot.rb', line 66

def create(args)
  ctx = Observed::Context.new(args)
  sys = ctx.system
  config = if args[:yaml_file]
             YAML.load_file(args[:yaml_file])
           elsif args[:config_file]
             sys.config
           elsif args[:config]
             c = args[:config]
             c
           else
             fail 'No configuration provided'
           end
  config = if config.is_a? Hash
             Observed::Config.create(config)
           else
             config
           end
  sys.config = config
  new(config, sys)
end

.from_argv(argv) ⇒ Object



59
60
61
62
# File 'lib/observed/application/oneshot.rb', line 59

def from_argv(argv)
  args = parse_argv!(argv.dup)
  create(args)
end

.parse_argv!(argv) ⇒ Object



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
# File 'lib/observed/application/oneshot.rb', line 32

def parse_argv!(argv)
  command_line_args = argv

  args = {}

  opts = OptionParser.new
  opts.accept(Pathname) do |s,|
    Pathname.new(s)
  end
  opts.on('-d', '--debug') do
    args[:debug] = true
  end
  opts.on('-l LOG_FILE', '--l LOG_FILE', Pathname) do |log_file|
    args[:log_file] = log_file
  end

  opts.parse!(command_line_args)

  unless command_line_args.size == 1 || command_line_args.size == 2
    fail InvalidArgumentError, "Invalid number of arguments #{command_line_args.size} where arguments are #{command_line_args}"
  end

  args[:config_file] = command_line_args.shift

  args
end

Instance Method Details

#configObject



23
24
25
# File 'lib/observed/application/oneshot.rb', line 23

def config
  @config || fail('Missing configuration for Application::Oneshot')
end

#run(observation_name = nil) ⇒ Object



27
28
29
# File 'lib/observed/application/oneshot.rb', line 27

def run(observation_name=nil)
  @system.run(observation_name)
end