Class: Bridgetown::Commands::Start

Inherits:
Thor::Group
  • Object
show all
Extended by:
BuildOptions, Summarizable
Includes:
ConfigurationOverridable
Defined in:
lib/bridgetown-core/commands/start.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BuildOptions

extended

Methods included from Summarizable

summary

Methods included from ConfigurationOverridable

#configuration_with_overrides, included

Class Method Details



23
24
25
# File 'lib/bridgetown-core/commands/start.rb', line 23

def self.banner
  "bridgetown start [options]"
end

Instance Method Details

#startObject

rubocop:todo Metrics/PerceivedComplexity



28
29
30
31
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bridgetown-core/commands/start.rb', line 28

def start # rubocop:todo Metrics/PerceivedComplexity
  Bridgetown.logger.writer.enable_prefix
  Bridgetown::Commands::Build.print_startup_message
  sleep 0.25

  begin
    require("puma/detect")
  rescue LoadError
    raise "** Puma server gem not found. Check your Gemfile and Bundler env? **"
  end

  options = Thor::CoreExt::HashWithIndifferentAccess.new(self.options)
  options[:using_puma] = true

  # Load Bridgetown configuration into thread memory
  bt_options = configuration_with_overrides(options)

  # Set a local site URL in the config if one is not available
  if Bridgetown.env.development? && !options["url"]
    scheme = bt_options.bind&.split("://")&.first == "ssl" ? "https" : "http"
    port = bt_options.bind&.split(":")&.last || ENV["BRIDGETOWN_PORT"] || 4000
    bt_options.url = "#{scheme}://localhost:#{port}"
  end

  puma_pid =
    Process.fork do
      require "puma/cli"

      Puma::Runner.class_eval do
        def output_header(mode)
          log "* Puma version: #{Puma::Const::PUMA_VERSION} (#{ruby_engine}) (\"#{Puma::Const::CODE_NAME}\")" # rubocop:disable Layout/LineLength
          if mode == "cluster"
            log "* Cluster Master PID: #{Process.pid}"
          else
            log "* PID: #{Process.pid}"
          end
        end
      end

      puma_args = []
      if bt_options[:bind]
        puma_args << "--bind"
        puma_args << bt_options[:bind]
      end

      cli = Puma::CLI.new puma_args
      cli.launcher.events.on_stopped do
        Bridgetown::Hooks.trigger :site, :server_shutdown
      end
      cli.run
    end

  begin
    Signal.trap("TERM") do
      Process.kill "SIGINT", puma_pid
      sleep 0.5 # let it breathe
      exit 0 # this runs the ensure block below
    end

    Process.setproctitle("bridgetown #{Bridgetown::VERSION} [#{File.basename(Dir.pwd)}]")

    build_args = ["-w"] + ARGV.reject { |arg| arg == "start" }
    Bridgetown::Commands::Build.start(build_args)
  rescue StandardError => e
    Process.kill "SIGINT", puma_pid
    sleep 0.5
    raise e
  ensure
    # Shut down webpack, browsersync, etc. if they're running
    Bridgetown::Utils::Aux.kill_processes
  end

  sleep 0.5 # finish cleaning up
end