Class: PgqPrometheus::SqlCaller::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/pgq_prometheus/sql_caller/active_record.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class_name) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.

Parameters:

  • model_class_name (Class<Object>, String)

    class or class name



14
15
16
# File 'lib/pgq_prometheus/sql_caller/active_record.rb', line 14

def initialize(model_class_name)
  @model_class_name = model_class_name.to_s
end

Instance Method Details

#consumer_info(queue, consumer = nil) ⇒ Hash<Symbol>+

Returns pgq.get_consumer_info for one or all consumers of queue.

Parameters:

  • queue (String)

    queue name

  • consumer (String, nil) (defaults to: nil)

    consumer name

Returns:

  • (Hash<Symbol>)

    when queue is present.

  • (Array<Hash<Symbol>>)

    when queue is nil.



35
36
37
38
39
40
41
# File 'lib/pgq_prometheus/sql_caller/active_record.rb', line 35

def consumer_info(queue, consumer = nil)
  if consumer
    select_hashes('SELECT * FROM pgq.get_consumer_info(?, ?)', queue.to_s, consumer.to_s).first
  else
    select_hashes('SELECT * FROM pgq.get_consumer_info(?)', queue.to_s)
  end
end

#queue_info(queue = nil) ⇒ Hash<Symbol>+

Returns pgq.get_queue_info for one or all queues.

Parameters:

  • queue (String, nil) (defaults to: nil)

    queue name

Returns:

  • (Hash<Symbol>)

    when queue is present.

  • (Array<Hash<Symbol>>)

    when queue is nil.



22
23
24
25
26
27
28
# File 'lib/pgq_prometheus/sql_caller/active_record.rb', line 22

def queue_info(queue = nil)
  if queue
    select_hashes('SELECT * FROM pgq.get_queue_info(?)', queue.to_s).first
  else
    select_hashes('SELECT * FROM pgq.get_queue_info()')
  end
end

#release_connectionObject

Releases active pg connection in thread. Do nothing if no connection captured.



45
46
47
# File 'lib/pgq_prometheus/sql_caller/active_record.rb', line 45

def release_connection
  model_class.connection_pool.release_connection
end

#with_connection { ... } ⇒ Object

Acquires pg connection during block execution. Release it after block executed.

Yields:



52
53
54
# File 'lib/pgq_prometheus/sql_caller/active_record.rb', line 52

def with_connection
  model_class.connection_pool.with_connection { yield }
end