Sha256: 42d7d4989fab77dff6aa37d9214d280ac3262c96f6c0ebcb3f8bdc9562a6181b

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Utils
    module Aux
      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

      def self.running_pids
        @running_pids ||= []
      end

      def self.add_pid(pid)
        running_pids << pid
      end

      def self.run_process(name, color, cmd)
        Thread.new do
          rd, wr = IO.pipe("BINARY")
          pid = Process.spawn(cmd, out: wr, err: wr, pgroup: true)
          @mutex.synchronize do
            add_pid pid
          end

          loop do
            line = rd.gets
            line.to_s.lines.map(&:chomp).each do |message|
              output = +""
              output << with_color(color, "[#{name}] ") if color
              output << message
              @mutex.synchronize do
                $stdout.puts output
                $stdout.flush
              end
            end
          end
        end
      end

      def self.group(&block)
        @mutex = Thread::Mutex.new
        instance_exec(&block)
      end

      def self.kill_processes
        Bridgetown.logger.info "Stopping auxiliary processes..."
        running_pids.each do |pid|
          Process.kill("SIGTERM", -Process.getpgid(pid))
        rescue Errno::ESRCH, Errno::EPERM, Errno::ECHILD # rubocop:disable Lint/SuppressedException
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
bridgetown-core-1.0.0 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.beta3 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.beta2 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.beta1 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha11 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha10 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha9 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha8 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha7 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha6 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha5 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha4 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha3 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha2 lib/bridgetown-core/utils/aux.rb
bridgetown-core-1.0.0.alpha1 lib/bridgetown-core/utils/aux.rb