Sha256: 32cd4e8330738c585b4901700fa0ff5e092b6bb7ba4c70d9174a183065d0ea6c

Contents?: true

Size: 1.44 KB

Versions: 10

Compression:

Stored size: 1.44 KB

Contents

# A relation of workers.
#
# Relations are enumerable, so you can use +Enumerable+ methods on them.
# Notice however that using these methods will imply loading all the relation
# in memory, which could introduce performance concerns.
class MissionControl::Jobs::WorkersRelation
  include Enumerable

  attr_accessor :offset_value, :limit_value

  delegate :last, :[], :to_s, :reverse, to: :to_a

  ALL_WORKERS_LIMIT = 100_000_000 # When no limit value it defaults to "all workers"

  def initialize(queue_adapter:)
    @queue_adapter = queue_adapter

    set_defaults
  end

  def offset(offset)
    clone_with offset_value: offset
  end

  def limit(limit)
    clone_with limit_value: limit
  end

  def each(&block)
    workers.each(&block)
  end

  def reload
    @count = @workers = nil
    self
  end

  def count
    if loaded?
      to_a.length
    else
      query_count
    end
  end

  def empty?
    count == 0
  end

  alias length count
  alias size count

  private
    def set_defaults
      self.offset_value = 0
      self.limit_value = ALL_WORKERS_LIMIT
    end

    def workers
      @workers ||= @queue_adapter.fetch_workers(self)
    end

    def query_count
      @count ||= @queue_adapter.count_workers(self)
    end

    def loaded?
      !@workers.nil?
    end

    def clone_with(**properties)
      dup.reload.tap do |relation|
        properties.each do |key, value|
          relation.send("#{key}=", value)
        end
      end
    end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mission_control-jobs-0.6.0 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.5.0 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.4.0 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.3.3 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.3.2 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.3.1 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.3.0 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.2.2 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.2.1 lib/mission_control/jobs/workers_relation.rb
mission_control-jobs-0.2.0 lib/mission_control/jobs/workers_relation.rb