Sha256: 5d838fbd7968df872e50c0b0a47b85721f6f6114f804216b0e6d3fffac199a73

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "tty-spinner"
require "concurrent-ruby"

module CobraCommander
  module Executor
    # Execute a command on multiple components concurrently
    class Concurrent
      def initialize(components, concurrency:, spin_output:)
        @components = components
        @multi = TTY::Spinner::Multi.new(":spinner :task", output: spin_output)
        @semaphore = ::Concurrent::Semaphore.new(concurrency)
      end

      def exec(command)
        @multi.top_spinner.update(task: "Running #{command}")
        @results = []
        @components.each do |component|
          register_job(component, command)
        end
        @multi.auto_spin
        @results
      end

    private

      def pastel
        @pastel ||= Pastel.new
      end

      def spinner_options
        @spinner_options ||= {
          format: :bouncing,
          success_mark: pastel.green("[DONE]"),
          error_mark: pastel.red("[ERROR]"),
        }
      end

      def register_job(component, command)
        @multi.register(":spinner #{component.name}", **spinner_options) do |spinner|
          @semaphore.acquire
          context = Context.new(component, command)
          context.success? ? spinner.success : spinner.error
          @results << context
          @semaphore.release
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cobra_commander-0.15.1 lib/cobra_commander/executor/concurrent.rb
cobra_commander-0.15.0 lib/cobra_commander/executor/concurrent.rb
cobra_commander-0.12.0 lib/cobra_commander/executor/concurrent.rb