class Docket::RoundRobin

Attributes

storage[RW]

Public Class Methods

new(args={}) click to toggle source
# File lib/docket/round_robin.rb, line 6
def initialize args={}
  @storage = args[:storage] || Docket.configuration.storage || Docket::Storage::Daybreak.new('/tmp/docket.rb')
end

Public Instance Methods

perform(identifier, action) click to toggle source
# File lib/docket/round_robin.rb, line 14
def perform identifier, action
  robin = next_robin identifier
  action.call(robin)
  robin
end
reset!() click to toggle source
# File lib/docket/round_robin.rb, line 25
def reset!
  storage.clear!
end
set(identifier, robins, options={}) click to toggle source
# File lib/docket/round_robin.rb, line 10
def set identifier, robins, options={}
  save identifier, robins, options
end
unset(identifier) click to toggle source
# File lib/docket/round_robin.rb, line 20
def unset identifier
  unset_key identifier
  unset_from_groups identifier
end

Protected Instance Methods

next_robin(identifier) click to toggle source
# File lib/docket/round_robin.rb, line 45
def next_robin identifier
  list = storage.read(identifier) || []
  robin_list = RobinList.new(list)

  next_robin = robin_list.fetch_next
  save identifier, nil, :list => robin_list.list

  next_robin
end
save(identifier, robins, options={}) click to toggle source
# File lib/docket/round_robin.rb, line 55
def save identifier, robins, options={}
  list = options[:list] || robins

  storage.save(identifier, list)
  storage.append(options[:group], identifier) if options[:group]
  storage.append("#{identifier}_groups", options[:group]) if options[:group]
end
unset_from_groups(identifier) click to toggle source
# File lib/docket/round_robin.rb, line 35
def unset_from_groups identifier
  groups = storage.read("#{identifier}_groups")
  Array(groups).each do |group|
    old_group = storage.read(group)
    storage.save(group, old_group.reject { |item| item == identifier })
  end

  storage.remove "#{identifier}_groups"
end
unset_key(identifier) click to toggle source
# File lib/docket/round_robin.rb, line 31
def unset_key identifier
  storage.remove identifier
end