module Postqueue

Constants

Timing
VERSION

Public Class Methods

default_logger() click to toggle source
# File lib/postqueue/logger.rb, line 10
def self.default_logger
  defined?(Rails) ? Rails.logger : stdout_logger
end
logger() click to toggle source
# File lib/postqueue/logger.rb, line 6
def self.logger
  @logger || default_logger
end
logger=(logger) click to toggle source
# File lib/postqueue/logger.rb, line 2
def self.logger=(logger)
  @logger ||= logger
end
migrate!(table_name = "postqueue") click to toggle source
# File lib/postqueue/item.rb, line 36
  def self.migrate!(table_name = "postqueue")
    connection = Item.connection

    if connection.tables.include?(table_name)
      upgrade_table!(table_name)
      return
    end

    connection.execute <<-SQL
    CREATE TABLE #{table_name} (
      id          BIGSERIAL PRIMARY KEY,
      op          VARCHAR,
      entity_id   INTEGER NOT NULL DEFAULT 0,
      created_at  timestamp without time zone NOT NULL DEFAULT (now() at time zone 'utc'),
      next_run_at timestamp without time zone NOT NULL DEFAULT (now() at time zone 'utc'),
      failed_attempts INTEGER NOT NULL DEFAULT 0
    );

    -- This index should be usable to find duplicate duplicates in the table. While
    -- we search for entries with matching op and entity_id, we assume that entity_id
    -- has a much higher cardinality.
    CREATE INDEX #{table_name}_idx1 ON #{table_name}(entity_id);

    -- This index should help picking the next entries to run. Otherwise a full tablescan
    -- would be necessary whenevr we check out items.
    CREATE INDEX #{table_name}_idx2 ON #{table_name}(next_run_at);
    SQL
  end
new(*args, &block) click to toggle source
# File lib/postqueue.rb, line 9
def new(*args, &block)
  ::Postqueue::Queue.new(*args, &block)
end
stdout_logger() click to toggle source
# File lib/postqueue/logger.rb, line 14
def self.stdout_logger
  @stdout_logger ||= Logger.new(STDOUT)
end
unmigrate!(table_name = "postqueue") click to toggle source
# File lib/postqueue/item.rb, line 19
  def self.unmigrate!(table_name = "postqueue")
    Item.connection.execute <<-SQL
      DROP TABLE IF EXISTS #{table_name};
    SQL
  end
upgrade_table!(table_name) click to toggle source
# File lib/postqueue/item.rb, line 25
def self.upgrade_table!(table_name)
  # upgrade id column to use BIGINT if necessary
  id_max = Item.column_types['id'].send(:range).end
  if id_max <= 2147483648
    STDERR.puts "Changing type of #{table_name}.id column to BIGINT"
    Item.connection.execute "ALTER TABLE #{table_name} ALTER COLUMN id TYPE BIGINT"
    Item.connection.execute "ALTER SEQUENCE #{table_name}_id_seq RESTART WITH 2147483649"
    Item.reset_column_information
  end
end