module DBQ::OrderedQueue::ClassMethods

Public Instance Methods

enforces_order_on(*attrs) click to toggle source
# File lib/dbq/ordered_queue.rb, line 9
def enforces_order_on(*attrs)
  @check_out_query = nil
  @enforce_order_on = attrs
  # check indexes and warn if they do not exist?
end

Private Instance Methods

check_out_item() click to toggle source
# File lib/dbq/ordered_queue.rb, line 17
def check_out_item
  # a separate thread means a separate connection/transaction/commit
  thread = Thread.new do
    item = nil
    connection_pool.with_connection do
      transaction do
        item = find_by_sql(check_out_query).first
        item.try(:check_out!)
      end
    end
    item
  end
  thread.abort_on_exception = true
  thread.value
end
check_out_query() click to toggle source
# File lib/dbq/ordered_queue.rb, line 33
      def check_out_query
        @check_out_query ||= <<-SQL
          select * from #{table_name}
          where #{table_name}.id not in (
            select #{table_name}.id
            from #{table_name}
            inner join #{table_name} as self_join on
              #{join_clause}
            where self_join.checked_out_at is not null
          )
          and #{table_name}.checked_out_at is null
          order by #{table_name}.id asc
          limit 1
          for update;
        SQL
      end
join_clause() click to toggle source

possibly add check/warn for recommended indexes def indexes

connection.indexes(self)

end

# File lib/dbq/ordered_queue.rb, line 55
def join_clause
  @enforce_order_on.inject([]) do |items, ordered_attr|
    items << "#{table_name}.#{ordered_attr} = self_join.#{ordered_attr}"
    items
  end.join(' and ')
end