Module: Bridgetown::Utils::Aux

Extended by:
PidTracker
Defined in:
bridgetown-core/lib/bridgetown-core/utils/aux.rb

Class Method Summary collapse

Methods included from PidTracker

add_pid, create_pid_dir, read_pidfile, remove_pidfile

Class Method Details

.kill_processesObject



40
41
42
43
44
45
46
47
48
49
# File 'bridgetown-core/lib/bridgetown-core/utils/aux.rb', line 40

def self.kill_processes
  Bridgetown.logger.info "Stopping auxiliary processes..."

  read_pidfile(:aux).each do |pid|
    Process.kill("SIGTERM", -Process.getpgid(pid.to_i))
  rescue Errno::ESRCH, Errno::EPERM, Errno::ECHILD # rubocop:disable Lint/SuppressedException
  ensure
    remove_pidfile :aux
  end
end

.run_process(name, color, cmd, env: {}) ⇒ Object



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
# File 'bridgetown-core/lib/bridgetown-core/utils/aux.rb', line 14

def self.run_process(name, color, cmd, env: {})
  @mutex ||= Thread::Mutex.new

  Thread.new do
    rd, wr = IO.pipe("BINARY")
    pid = Process.spawn({ "BRIDGETOWN_NO_BUNDLER_REQUIRE" => nil }.merge(env),
                        cmd, out: wr, err: wr, pgroup: true)
    @mutex.synchronize { add_pid(pid, file: :aux) }

    loop do
      line = rd.gets
      line.to_s.lines.map(&:chomp).each do |message|
        next if name == "Frontend" && %r{ELIFECYCLE.*?Command failed}.match?(message)

        output = +""
        output << with_color(color, "[#{name}] ") if color
        output << message
        @mutex.synchronize do
          $stdout.puts output
          $stdout.flush
        end
      end
    end
  end
end

.with_color(name, message) ⇒ Object



8
9
10
11
12
# File 'bridgetown-core/lib/bridgetown-core/utils/aux.rb', line 8

def self.with_color(name, message)
  return message unless !name.nil? && Bridgetown::Utils::Ansi::COLORS[name.to_sym]

  Bridgetown::Utils::Ansi.send(name, message)
end