Sha256: 586baccf9feaaaa0405e2e49bfbf953c0c020036257b7441860700fa0b3d89d6

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require "wsdirector/client"
require "wsdirector/protocols"

module WSDirector
  # Single client operator
  class Task
    attr_reader :global_holder, :client

    def initialize(config, id:, global_holder:, result:, scale:, logger:, colorize: false)
      @id = id
      @logger = logger
      @ignore = config.fetch("ignore")
      @steps = config.fetch("steps")
      @connection_options = config.fetch("connection_options").transform_keys(&:to_sym)
      @global_holder = global_holder
      @result = result

      protocol_class = Protocols.get(config.fetch("protocol", "base"))
      @protocol = protocol_class.new(self, scale:, logger:, id:, color: color_for_id(id, colorize))
    end

    def run(url)
      connect!(url, **@connection_options)

      steps.each(&protocol)

      result.succeed
    rescue Error => e
      result.failed(e.message)
    end

    def sampled?(step)
      return true unless step["sample"]

      id, max = step["id"], step["sample"]

      result.track_sample(id, max)
    end

    private

    attr_reader :steps, :result, :protocol, :id, :logger, :ignore

    def connect!(url, **options)
      protocol.init_client(url:, id:, ignore:, **options)
    end

    def color_for_id(id, colorize)
      return unless colorize

      String.colors[id.hash % String.colors.size]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
wsdirector-core-1.1.0 lib/wsdirector/task.rb
wsdirector-core-1.0.3 lib/wsdirector/task.rb
wsdirector-core-1.0.2 lib/wsdirector/task.rb
wsdirector-core-1.0.1 lib/wsdirector/task.rb
wsdirector-core-1.0.0 lib/wsdirector/task.rb