Class: Que::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/que/adapters/base.rb

Direct Known Subclasses

ActiveRecord, ConnectionPool, PG, Pond, Sequel

Instance Method Summary collapse

Constructor Details

#initialize(thing = nil) ⇒ Base

Returns a new instance of Base.



12
13
14
# File 'lib/que/adapters/base.rb', line 12

def initialize(thing = nil)
  @prepared_statements = {}
end

Instance Method Details

#checkout(&block) ⇒ Object

The only method that adapters really need to implement. Should lock a PG::Connection (or something that acts like a PG::Connection) so that no other threads are using it and yield it to the block. Should also be re-entrant.

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/que/adapters/base.rb', line 20

def checkout(&block)
  raise NotImplementedError
end

#execute(command, params = []) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/que/adapters/base.rb', line 31

def execute(command, params = [])
  params = params.map do |param|
    case param
      # The pg gem unfortunately doesn't convert fractions of time instances, so cast them to a string.
      when Time then param.strftime("%Y-%m-%d %H:%M:%S.%6N %z")
      when Array, Hash then JSON_MODULE.dump(param)
      else param
    end
  end

  cast_result \
    case command
      when Symbol then execute_prepared(command, params)
      when String then execute_sql(command, params)
    end
end

#in_transaction?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/que/adapters/base.rb', line 48

def in_transaction?
  checkout { |conn| conn.transaction_status != ::PG::PQTRANS_IDLE }
end

#wake_worker_after_commitObject

Called after a job is queued in async mode, to prompt a worker to wake up after the current transaction commits. Not all adapters will implement this.



27
28
29
# File 'lib/que/adapters/base.rb', line 27

def wake_worker_after_commit
  false
end