activerecord-postgres_pub_sub 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbcb38025bb4b4992bb6a8d7ed1b2c1288fcc9b3df9c3bc4459a9be8ce7e96bd
4
- data.tar.gz: fefad9359ecce30c1198a3c34e2c64ebb9993b797e2ff7320dd47ae23e6dfa2f
3
+ metadata.gz: 1b886594148cd3420c38dee50e9baeba2d73f01901cd682704efb624ee999b51
4
+ data.tar.gz: ee644f50f830c05f60efc505f50b73ffefbbb4971bbbca256e63d4d00d16c9f2
5
5
  SHA512:
6
- metadata.gz: ce692f2b1d176e51767ca21d679d8b618a84e9c029cd5f70e4430e32dec6ba6d9d9c226945c25530125d4e7f7692f7ab049673117aa0c86f3ed8bfdae08ef3d6
7
- data.tar.gz: 7166ae680431461ea81a0e44e0d3f8b0b26607255d23869417c2c59c98a74a04ba75c783e7682c576a8c4de9b305d0d9906e36fe433e11fe6ff8c979adc57d03
6
+ metadata.gz: 02d2b855c61be06e21b9acedb849a0ada4a99f7345f5be171ac33fcf4ca67ceced2ffd9fa023cce5851a0cb3eadc8a344192f8d35ab15ac947c45a9d0ccf7ce6
7
+ data.tar.gz: c23a5d3df02df37aad3e136924926428a366721d1da68e854e6968c2f728db43cb6059f5258e276d0bbc10008ab2bec319908f4c1babc280ead7952d5d102d6d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # activerecord-postgres_pub_sub
2
2
 
3
+ ## v2.2.0
4
+ - Add support for listening to multiple channels.
5
+
3
6
  ## v2.1.0
4
7
  - Set required ruby version to 2.7.0
5
8
  - Add support for Rails 7.0
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![CircleCI](https://circleci.com/gh/ezcater/activerecord-postgres_pub_sub.svg?style=svg)](https://circleci.com/gh/ezcater/activerecord-postgres_pub_sub)
4
4
 
5
- This gem contains support for PostgreSQL LISTEN and NOTIFY functionality:
5
+ This gem contains support for PostgreSQL LISTEN and NOTIFY functionality:
6
6
  [doc](https://www.postgresql.org/docs/9.6/static/libpq-notify.html).
7
7
 
8
8
  ## Installation
@@ -25,7 +25,8 @@ Or install it yourself as:
25
25
 
26
26
  ### Listener
27
27
 
28
- The `Listener` class is used to handle notification messages.
28
+ The `Listener` class is used to handle notification messages on one or more
29
+ channels.
29
30
 
30
31
  The listener can be configured with three blocks:
31
32
 
@@ -33,13 +34,13 @@ The listener can be configured with three blocks:
33
34
  * **on_start**: called before receiving any notifications.
34
35
  * **on_timeout**: called based on a configurable timeout, when no notifications
35
36
  have been received.
36
-
37
+
37
38
  When creating a listener, the following configuration is supported:
38
39
 
39
- * **listen_timeout**: If set, the `on_timeout` block will be called if
40
- no notifications are received within this period. (Default `nil`).
40
+ * **listen_timeout**: If set, the `on_timeout` block will be called if
41
+ no notifications are received within this period. (Default `nil`).
41
42
  * **notify_only**: A payload string can be included in notifications. By default
42
- the listener ignores the payload and coalesces multiple notifications into a
43
+ the listener ignores the payload and coalesces multiple notifications into a
43
44
  single call. When this option is `false`, the `on_notify` block is called with
44
45
  the payload for each notification. (Default `true`).
45
46
  * **exclusive_lock**: Acquire a lock using
@@ -51,16 +52,16 @@ Example:
51
52
  ```ruby
52
53
  ActiveRecord::PostgresPubSub::Listener.listen("notify_channel", listen_timeout: 30) do |listener|
53
54
  listener.on_start do
54
- # when starting assume we missed something and perform regular activity
55
+ # when starting assume we missed something and perform regular activity
55
56
  handle_notification
56
57
  end
57
-
58
+
58
59
  listener.on_notify do
59
60
  handle_notification
60
61
  end
61
-
62
+
62
63
  listener.on_timeout do
63
- perform_regular_maintenance
64
+ perform_regular_maintenance
64
65
  end
65
66
  end
66
67
  ```
@@ -9,10 +9,10 @@ module ActiveRecord
9
9
  extend PrivateAttr
10
10
 
11
11
  private_attr_reader :on_notify_blk, :on_start_blk, :on_timeout_blk,
12
- :channel, :listen_timeout, :exclusive_lock, :notify_only
12
+ :channels, :listen_timeout, :exclusive_lock, :notify_only
13
13
 
14
- def self.listen(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true)
15
- listener = new(channel,
14
+ def self.listen(*channels, listen_timeout: nil, exclusive_lock: true, notify_only: true)
15
+ listener = new(*channels,
16
16
  listen_timeout: listen_timeout,
17
17
  exclusive_lock: exclusive_lock,
18
18
  notify_only: notify_only)
@@ -20,8 +20,8 @@ module ActiveRecord
20
20
  listener.listen
21
21
  end
22
22
 
23
- def initialize(channel, listen_timeout: nil, exclusive_lock: true, notify_only: true)
24
- @channel = channel
23
+ def initialize(*channels, listen_timeout: nil, exclusive_lock: true, notify_only: true)
24
+ @channels = channels
25
25
  @listen_timeout = listen_timeout
26
26
  @exclusive_lock = exclusive_lock
27
27
  @notify_only = notify_only
@@ -44,8 +44,8 @@ module ActiveRecord
44
44
  on_start_blk&.call
45
45
 
46
46
  loop do
47
- wait_for_notify(connection) do |payload|
48
- notify_only ? on_notify_blk.call : on_notify_blk.call(payload)
47
+ wait_for_notify(connection) do |payload, channel|
48
+ notify_only ? on_notify_blk.call : on_notify_blk.call(payload, channel)
49
49
  end
50
50
  end
51
51
  end
@@ -56,12 +56,16 @@ module ActiveRecord
56
56
  def with_connection
57
57
  ActiveRecord::Base.connection_pool.with_connection do |connection|
58
58
  with_optional_lock do
59
- connection.execute("LISTEN #{channel}")
59
+ channels.each do |channel|
60
+ connection.execute("LISTEN #{channel};")
61
+ end
60
62
 
61
63
  begin
62
64
  yield(connection)
63
65
  ensure
64
- connection.execute("UNLISTEN #{channel}")
66
+ channels.each do |channel|
67
+ connection.execute("UNLISTEN #{channel}")
68
+ end
65
69
  end
66
70
  end
67
71
  end
@@ -76,7 +80,7 @@ module ActiveRecord
76
80
  end
77
81
 
78
82
  def lock_name
79
- "#{channel}-listener"
83
+ "#{channels.join('-')}-listener"
80
84
  end
81
85
 
82
86
  def empty_channel(connection)
@@ -87,10 +91,11 @@ module ActiveRecord
87
91
 
88
92
  def wait_for_notify(connection)
89
93
  connection_pid = connection.raw_connection.backend_pid
90
- event_result = connection.raw_connection.wait_for_notify(listen_timeout) do |_event, pid, payload|
94
+ event_result = connection.raw_connection.wait_for_notify(listen_timeout) do |notify_channel, pid, payload|
91
95
  if pid != connection_pid
92
96
  empty_channel(connection.raw_connection) if notify_only
93
- yield(payload)
97
+
98
+ yield(payload, notify_channel)
94
99
  end
95
100
  end
96
101
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresPubSub
5
- VERSION = "2.1.0"
5
+ VERSION = "2.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgres_pub_sub
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ezCater, Inc
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-23 00:00:00.000000000 Z
11
+ date: 2022-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -246,7 +246,7 @@ licenses:
246
246
  - MIT
247
247
  metadata:
248
248
  allowed_push_host: https://rubygems.org
249
- post_install_message:
249
+ post_install_message:
250
250
  rdoc_options: []
251
251
  require_paths:
252
252
  - lib
@@ -261,8 +261,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  - !ruby/object:Gem::Version
262
262
  version: '0'
263
263
  requirements: []
264
- rubygems_version: 3.1.6
265
- signing_key:
264
+ rubygems_version: 3.1.4
265
+ signing_key:
266
266
  specification_version: 4
267
267
  summary: Support for Postgres Notify/Listen
268
268
  test_files: []