Class: DummyLogGenerator::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/dummy_log_generator/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = [], opts = [], config = {}) ⇒ CLI

Returns a new instance of CLI.



12
13
14
# File 'lib/dummy_log_generator/cli.rb', line 12

def initialize(args = [], opts = [], config = {})
  super(args, opts, config)
end

Instance Method Details

#graceful_restartObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/dummy_log_generator/cli.rb', line 82

def graceful_restart
  pid = File.read(@options["pid_path"]).to_i

  begin
    Process.kill("USR1", pid)
    puts "Gracefully restarted #{pid}"
  rescue Errno::ESRCH
    puts "DummyLogGenerator #{pid} not running"
  end
end

#graceful_stopObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/dummy_log_generator/cli.rb', line 58

def graceful_stop
  pid = File.read(@options["pid_path"]).to_i

  begin
    Process.kill("TERM", pid)
    puts "Gracefully stopped #{pid}"
  rescue Errno::ESRCH
    puts "DummyLogGenerator #{pid} not running"
  end
end

#restartObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/dummy_log_generator/cli.rb', line 70

def restart
  pid = File.read(@options["pid_path"]).to_i

  begin
    Process.kill("HUP", pid)
    puts "Restarted #{pid}"
  rescue Errno::ESRCH
    puts "DummyLogGenerator #{pid} not running"
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dummy_log_generator/cli.rb', line 25

def start
  @options = @options.dup # avoid frozen
  dsl =
    if options[:config] && File.exists?(options[:config])
      instance_eval(File.read(options[:config]), options[:config])
    else
      DummyLogGenerator::Dsl.new
    end
  @options[:setting] = dsl.setting
  dsl.setting.rate = options[:rate] if options[:rate]
  dsl.setting.output = options[:output] if options[:output]
  dsl.setting.message = options[:message] if options[:message]
  # options for serverengine
  @options[:workers] ||= dsl.setting.workers

  opts = @options.symbolize_keys.except(:config)
  se = ServerEngine.create(nil, DummyLogGenerator::Worker, opts)
  se.run
end

#stopObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/dummy_log_generator/cli.rb', line 46

def stop
  pid = File.read(@options["pid_path"]).to_i

  begin
    Process.kill("QUIT", pid)
    puts "Stopped #{pid}"
  rescue Errno::ESRCH
    puts "DummyLogGenerator #{pid} not running"
  end
end