Class: Guard::PumaRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/puma/runner.rb

Constant Summary collapse

MAX_WAIT_COUNT =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PumaRunner

Returns a new instance of PumaRunner.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/guard/puma/runner.rb', line 12

def initialize(options)
  @control_token = options.delete(:control_token) { |_| ::Puma::Configuration.random_token }
  @control_port = (options.delete(:control_port) || '9293')
  @control_url = "localhost:#{@control_port}"
  @quiet = options.delete(:quiet) { true }
  @pumactl = options.delete(:pumactl) { false }
  @options = options

  puma_options = {
    puma_options_key(:config) => options.fetch(:config, "-"),
    puma_options_key(:control_token) => @control_token,
    puma_options_key(:control_url) => "tcp://#{@control_url}"
  }
  if options[:config]
    puma_options['--config'] = options[:config]
  elsif default_config_file_exists?
    puma_options['--config'] = DEFAULT_CONFIG_FILE_PATH
  else
    puma_options['--port'] = options[:port]
  end
  %i[bind threads environment]
    .select { |opt| options[opt] }
    .each do |opt|
      if pumactl
        Compat::UI.warning(
          "`#{opt}` option is not compatible with `pumactl` option"
        )
      else
        puma_options["--#{opt}"] = options[opt]
      end
    end
  puma_options = puma_options.to_a.flatten
  puma_options << '--quiet' if @quiet
  @cmd_opts = puma_options.join ' '
end

Instance Attribute Details

#cmd_optsObject (readonly)

Returns the value of attribute cmd_opts.



10
11
12
# File 'lib/guard/puma/runner.rb', line 10

def cmd_opts
  @cmd_opts
end

#control_tokenObject (readonly)

Returns the value of attribute control_token.



10
11
12
# File 'lib/guard/puma/runner.rb', line 10

def control_token
  @control_token
end

#control_urlObject (readonly)

Returns the value of attribute control_url.



10
11
12
# File 'lib/guard/puma/runner.rb', line 10

def control_url
  @control_url
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/guard/puma/runner.rb', line 10

def options
  @options
end

#pumactlObject (readonly)

Returns the value of attribute pumactl.



10
11
12
# File 'lib/guard/puma/runner.rb', line 10

def pumactl
  @pumactl
end

Instance Method Details

#haltObject



52
53
54
55
56
# File 'lib/guard/puma/runner.rb', line 52

def halt
  run_puma_command!('halt')
  # server may not have been stopped correctly, but we are halting so who cares.
  return true
end

#restartObject



58
59
60
61
62
63
64
65
# File 'lib/guard/puma/runner.rb', line 58

def restart
  if run_puma_command!('restart')
    return true
  else
    # server may not have been started correctly, or crashed. Let's try to start it.
    return start
  end
end

#sleep_timeObject



67
68
69
# File 'lib/guard/puma/runner.rb', line 67

def sleep_time
  options[:timeout].to_f / MAX_WAIT_COUNT.to_f
end

#startObject



48
49
50
# File 'lib/guard/puma/runner.rb', line 48

def start
  Kernel.system build_command('start')
end