Class: MessageBus::Postgres::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/message_bus/backends/postgres.rb

Defined Under Namespace

Classes: Listener

Constant Summary collapse

INHERITED_CONNECTIONS =
[]

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
30
31
# File 'lib/message_bus/backends/postgres.rb', line 24

def initialize(config)
  @config = config
  @listening_on = {}
  @available = []
  @allocated = {}
  @mutex = Mutex.new
  @pid = Process.pid
end

Instance Method Details

#add(channel, value) ⇒ Object



33
34
35
# File 'lib/message_bus/backends/postgres.rb', line 33

def add(channel, value)
  hold{|conn| exec_prepared(conn, 'insert_message', [channel, value]){|r| r.getvalue(0,0).to_i}}
end

#backlog(channel, backlog_id) ⇒ Object



54
55
56
# File 'lib/message_bus/backends/postgres.rb', line 54

def backlog(channel, backlog_id)
  hold{|conn| exec_prepared(conn, 'channel_backlog', [channel, backlog_id]){|r| r.values.each{|a| a[0] = a[0].to_i}}} || []
end

#clear_channel_backlog(channel, backlog_id, num_to_keep) ⇒ Object



44
45
46
47
# File 'lib/message_bus/backends/postgres.rb', line 44

def clear_channel_backlog(channel, backlog_id, num_to_keep)
  hold{|conn| exec_prepared(conn, 'clear_channel_backlog', [channel, backlog_id, num_to_keep])}
  nil
end

#clear_global_backlog(backlog_id, num_to_keep) ⇒ Object



37
38
39
40
41
42
# File 'lib/message_bus/backends/postgres.rb', line 37

def clear_global_backlog(backlog_id, num_to_keep)
  if backlog_id > num_to_keep
    hold{|conn| exec_prepared(conn, 'clear_global_backlog', [backlog_id - num_to_keep])}
    nil
  end
end

#expire(max_backlog_age) ⇒ Object



49
50
51
52
# File 'lib/message_bus/backends/postgres.rb', line 49

def expire(max_backlog_age)
  hold{|conn| exec_prepared(conn, 'expire', [max_backlog_age])}
  nil
end

#get_value(channel, id) ⇒ Object



62
63
64
# File 'lib/message_bus/backends/postgres.rb', line 62

def get_value(channel, id)
  hold{|conn| exec_prepared(conn, 'get_message', [channel, id]){|r| r.getvalue(0,0)}}
end

#global_backlog(backlog_id) ⇒ Object



58
59
60
# File 'lib/message_bus/backends/postgres.rb', line 58

def global_backlog(backlog_id)
  hold{|conn| exec_prepared(conn, 'global_backlog', [backlog_id]){|r| r.values.each{|a| a[0] = a[0].to_i}}} || []
end

#max_id(channel = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/message_bus/backends/postgres.rb', line 81

def max_id(channel=nil)
  block = proc do |r|
    if r.ntuples > 0
      r.getvalue(0,0).to_i
    else
      0
    end
  end

  if channel
    hold{|conn| exec_prepared(conn, 'max_channel_id', [channel], &block)}
  else
    hold{|conn| exec_prepared(conn, 'max_id', &block)}
  end
end

#publish(channel, data) ⇒ Object



97
98
99
# File 'lib/message_bus/backends/postgres.rb', line 97

def publish(channel, data)
  hold{|conn| exec_prepared(conn, 'publish', [channel, data])}
end

#reconnectObject



66
67
68
69
70
71
# File 'lib/message_bus/backends/postgres.rb', line 66

def reconnect
  sync do
    @listening_on.clear
    @available.clear
  end
end

#reset!Object

Dangerous, drops the message_bus table containing the backlog if it exists.



74
75
76
77
78
79
# File 'lib/message_bus/backends/postgres.rb', line 74

def reset!
  hold do |conn|
    conn.exec 'DROP TABLE IF EXISTS message_bus'
    create_table(conn)
  end
end

#subscribe(channel) {|listener| ... } ⇒ Object

Yields:

  • (listener)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/message_bus/backends/postgres.rb', line 101

def subscribe(channel)
  obj = Object.new
  sync{@listening_on[channel] = obj}
  listener = Listener.new
  yield listener
  
  conn = raw_pg_connection
  conn.exec "LISTEN #{channel}"
  listener.do_sub.call
  while listening_on?(channel, obj)
    conn.wait_for_notify(10) do |_,_,payload|
      break unless listening_on?(channel, obj)
      listener.do_message.call(nil, payload)
    end
  end
  listener.do_unsub.call

  conn.exec "UNLISTEN #{channel}"
  nil
end

#unsubscribeObject



122
123
124
# File 'lib/message_bus/backends/postgres.rb', line 122

def unsubscribe
  sync{@listening_on.clear}
end