Class: Postqueue::Item

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
RawInserter
Defined in:
lib/postqueue/item.rb,
lib/postqueue/item/enqueue.rb,
lib/postqueue/item/inserter.rb

Overview

Postqueue::Item inserter modules.

This source file provides multiple implementations to insert Postqueue::Items. Which one will be used depends on the “extend XXXInserter” line below.

Defined Under Namespace

Modules: ActiveRecordInserter, PreparedRawInserter, RawInserter

Class Method Summary collapse

Methods included from RawInserter

insert_item, insert_sql

Class Method Details

.enqueue(op:, entity_id:, ignore_duplicates: false) ⇒ Object

Enqueues an queue item. If the operation is duplicate, and an entry with the same combination of op and entity_id exists already, no new entry will be added to the queue.

Returns the number of items that have been enqueued.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/postqueue/item/enqueue.rb', line 8

def self.enqueue(op:, entity_id:, ignore_duplicates: false)
  if entity_id.is_a?(Enumerable)
    return enqueue_many(op: op, entity_ids: entity_id, ignore_duplicates: ignore_duplicates)
  end

  if ignore_duplicates && where(op: op, entity_id: entity_id).present?
    return 0
  end

  insert_item op: op, entity_id: entity_id
  1
end

.enqueue_many(op:, entity_ids:, ignore_duplicates:) ⇒ Object

:nodoc:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/postqueue/item/enqueue.rb', line 21

def self.enqueue_many(op:, entity_ids:, ignore_duplicates:) #:nodoc:
  entity_ids = Array(entity_ids)
  entity_ids.uniq! if ignore_duplicates

  transaction do
    entity_ids.each do |entity_id|
      enqueue(op: op, entity_id: entity_id, ignore_duplicates: ignore_duplicates)
    end
  end

  entity_ids.count
end

.postpone(ids) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/postqueue/item.rb', line 9

def self.postpone(ids)
  connection.exec_query <<-SQL
    UPDATE #{table_name}
      SET failed_attempts = failed_attempts+1,
          next_run_at = next_run_at + power(failed_attempts + 1, 1.5) * interval '10 second'
      WHERE id IN (#{ids.join(',')})
  SQL
end