Class: Guard::Puma

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :pumactl => false,
  :port => 4000,
  :environment => default_env,
  :start_on_start => true,
  :force_run => false,
  :timeout => 20,
  :restart_timeout => 1,
  :debugger => false,
  :notifications => %i[restarting restarted not_restarted stopped]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Puma

Returns a new instance of Puma.



25
26
27
28
29
30
31
# File 'lib/guard/puma.rb', line 25

def initialize(options = {})
  super
  @options = DEFAULT_OPTIONS.merge(options)
  @options[:port] = nil if @options.key?(:config)
  @runner = ::Guard::PumaRunner.new(@options)
  @last_restarted = Time.now
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/guard/puma.rb', line 7

def options
  @options
end

#runnerObject (readonly)

Returns the value of attribute runner.



7
8
9
# File 'lib/guard/puma.rb', line 7

def runner
  @runner
end

Class Method Details

.default_envObject



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

def self.default_env
  ENV.fetch('RACK_ENV', 'development')
end

Instance Method Details

#reloadObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/guard/puma.rb', line 42

def reload
  return if (Time.now - @last_restarted) < options[:restart_timeout]
  @last_restarted = Time.now
  Compat::UI.info "Restarting Puma..."
  if options[:notifications].include?(:restarting)
    Compat::UI.notify(
      "Puma restarting#{port_text} in #{options[:environment]} environment...",
      title: "Restarting Puma...", image: :pending
    )
  end
  if runner.restart
    Compat::UI.info "Puma restarted"
    if options[:notifications].include?(:restarted)
      Compat::UI.notify(
        "Puma restarted#{port_text}.",
        title: "Puma restarted!", image: :success
      )
    end
  else
    Compat::UI.info "Puma NOT restarted, check your log files."
    if options[:notifications].include?(:not_restarted)
      Compat::UI.notify(
        "Puma NOT restarted, check your log files.",
        title: "Puma NOT restarted!", image: :failed
      )
    end
  end
end

#run_on_changes(paths) ⇒ Object



82
83
84
# File 'lib/guard/puma.rb', line 82

def run_on_changes(paths)
  reload
end

#startObject



33
34
35
36
37
38
39
40
# File 'lib/guard/puma.rb', line 33

def start
  return unless options[:start_on_start]
  server = options[:server] ? "#{options[:server]} and " : ""
  Compat::UI.info(
    "Puma starting#{port_text} in #{server}#{options[:environment]} environment."
  )
  runner.start
end

#stopObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/guard/puma.rb', line 71

def stop
  return unless options[:start_on_start]
  if options[:notifications].include?(:stopped)
    Compat::UI.notify(
      "Until next time...",
      title: "Puma shutting down.", image: :pending
    )
  end
  runner.halt
end