Class: MessageBus::Backends::Postgres::Client
- Inherits:
-
Object
- Object
- MessageBus::Backends::Postgres::Client
- Defined in:
- lib/message_bus/backends/postgres.rb
Defined Under Namespace
Classes: Listener
Constant Summary collapse
- INHERITED_CONNECTIONS =
[]
Instance Method Summary collapse
- #add(channel, value) ⇒ Object
- #after_fork ⇒ Object
- #backlog(channel, backlog_id) ⇒ Object
- #clear_channel_backlog(channel, backlog_id, num_to_keep) ⇒ Object
- #clear_global_backlog(backlog_id, num_to_keep) ⇒ Object
- #destroy ⇒ Object
- #expire(max_backlog_age) ⇒ Object
-
#expire_all_backlogs! ⇒ Object
use with extreme care, will nuke all of the data.
- #get_value(channel, id) ⇒ Object
- #global_backlog(backlog_id) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #max_id(channel = nil) ⇒ Object
- #publish(channel, data) ⇒ Object
-
#reset! ⇒ Object
Dangerous, drops the message_bus table containing the backlog if it exists.
- #subscribe(channel) ⇒ Object
- #unsubscribe ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
42 43 44 45 46 47 48 49 50 |
# File 'lib/message_bus/backends/postgres.rb', line 42 def initialize(config) @config = config @listening_on = {} @available = [] @allocated = {} @subscribe_connection = nil @mutex = Mutex.new @pid = Process.pid end |
Instance Method Details
#add(channel, value) ⇒ Object
52 53 54 |
# File 'lib/message_bus/backends/postgres.rb', line 52 def add(channel, value) hold { |conn| exec_prepared(conn, 'insert_message', [channel, value]) { |r| r.getvalue(0, 0).to_i } } end |
#after_fork ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/message_bus/backends/postgres.rb', line 89 def after_fork sync do @pid = Process.pid INHERITED_CONNECTIONS.concat(@available) @available.clear @listening_on.clear end end |
#backlog(channel, backlog_id) ⇒ Object
73 74 75 76 77 |
# File 'lib/message_bus/backends/postgres.rb', line 73 def backlog(channel, backlog_id) hold do |conn| exec_prepared(conn, 'channel_backlog', [channel, backlog_id]) { |r| r.values.each { |a| a[0] = a[0].to_i } } end || [] end |
#clear_channel_backlog(channel, backlog_id, num_to_keep) ⇒ Object
63 64 65 66 |
# File 'lib/message_bus/backends/postgres.rb', line 63 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
56 57 58 59 60 61 |
# File 'lib/message_bus/backends/postgres.rb', line 56 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 |
#destroy ⇒ Object
106 107 108 109 110 111 |
# File 'lib/message_bus/backends/postgres.rb', line 106 def destroy sync do @available.each(&:close) @available.clear end end |
#expire(max_backlog_age) ⇒ Object
68 69 70 71 |
# File 'lib/message_bus/backends/postgres.rb', line 68 def expire(max_backlog_age) hold { |conn| exec_prepared(conn, 'expire', [max_backlog_age]) } nil end |
#expire_all_backlogs! ⇒ Object
use with extreme care, will nuke all of the data
114 115 116 |
# File 'lib/message_bus/backends/postgres.rb', line 114 def expire_all_backlogs! reset! end |
#get_value(channel, id) ⇒ Object
85 86 87 |
# File 'lib/message_bus/backends/postgres.rb', line 85 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
79 80 81 82 83 |
# File 'lib/message_bus/backends/postgres.rb', line 79 def global_backlog(backlog_id) hold do |conn| exec_prepared(conn, 'global_backlog', [backlog_id]) { |r| r.values.each { |a| a[0] = a[0].to_i } } end || [] end |
#max_id(channel = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/message_bus/backends/postgres.rb', line 118 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
134 135 136 |
# File 'lib/message_bus/backends/postgres.rb', line 134 def publish(channel, data) hold { |conn| exec_prepared(conn, 'publish', [channel, data]) } end |
#reset! ⇒ Object
Dangerous, drops the message_bus table containing the backlog if it exists.
99 100 101 102 103 104 |
# File 'lib/message_bus/backends/postgres.rb', line 99 def reset! hold do |conn| conn.exec 'DROP TABLE IF EXISTS message_bus' create_table(conn) end end |
#subscribe(channel) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/message_bus/backends/postgres.rb', line 138 def subscribe(channel) obj = Object.new sync { @listening_on[channel] = obj } listener = Listener.new yield listener conn = @subscribe_connection = 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..call(nil, payload) end end listener.do_unsub.call conn.exec "UNLISTEN #{channel}" nil ensure @subscribe_connection&.close @subscribe_connection = nil end |
#unsubscribe ⇒ Object
163 164 165 |
# File 'lib/message_bus/backends/postgres.rb', line 163 def unsubscribe sync { @listening_on.clear } end |