pg_notifier 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/pg_notifier.rb +15 -0
- data/lib/pg_notifier/manager.rb +24 -30
- data/lib/pg_notifier/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MTA3MzYyYTM0ODk3MDlkMmU2NjA1ODEyOGYyMjg4NTNiMmZjNmZlYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MjJiODFhNmUxODk5NDc2NDIyYjRmNWU1NGQzZjg3YjY3YTU2NGQ5MQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGI1ZmZjMDViMzRhM2U2ZTJjZTBiYTk5MTE2Y2VkMWJhOTI0OGNkN2U4MGY5
|
10
|
+
Yjk5MjE5ZDhiNDEzYmU5ZWVhNWI3ODNlMmRmODQ1NzRlMTk1YzhkMGZjMTNi
|
11
|
+
ZTc2OTY4NDgwODJhNTdjNzI0Yzk2MTA3MjU3OWJlYmEyYzk3OWE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ODhkNDk5NzM1YjViYjc2ZTJkYzg5NDI4Y2IzOTkwZWIyMTVlMTQ3ODU5NDFh
|
14
|
+
MzRjYzI5MTAwNzdiNDZmMWZkODU2MDE3MDhmYWU2ZWZiZmRiYmQxNjUxODNm
|
15
|
+
ZGI2MDJmYzBiYmQ4YWZhN2U5NzljM2IzN2ZmMWI5NDgxNjFjYTI=
|
data/lib/pg_notifier.rb
CHANGED
@@ -10,7 +10,22 @@ module PgNotifier
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def run
|
13
|
+
sig_read, sig_write = IO.pipe
|
14
|
+
|
15
|
+
(%w[INT TERM HUP] & Signal.list.keys).each do |sig|
|
16
|
+
trap sig do
|
17
|
+
sig_write.puts(sig)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
manager.run
|
22
|
+
|
23
|
+
while io = IO.select([sig_read])
|
24
|
+
sig = io.first[0].gets.chomp
|
25
|
+
|
26
|
+
manager.logger.debug "Got #{sig} signal"
|
27
|
+
manager.shutdown if %w[INT TERM HUP].include? sig
|
28
|
+
end
|
14
29
|
end
|
15
30
|
|
16
31
|
def notify(channel, options = {}, &block)
|
data/lib/pg_notifier/manager.rb
CHANGED
@@ -4,14 +4,17 @@ require 'logger'
|
|
4
4
|
|
5
5
|
module PgNotifier
|
6
6
|
class Manager
|
7
|
-
attr_accessor :logger, :db_config
|
7
|
+
attr_accessor :logger, :db_config, :timeout
|
8
8
|
|
9
9
|
def initialize(attrs = {})
|
10
10
|
@logger = attrs.fetch :logger, Logger.new(STDOUT)
|
11
11
|
@db_config = attrs.fetch :db_config , {}
|
12
|
+
@timeout = attrs.fetch :timeout, 0.1
|
12
13
|
|
13
14
|
@finish = false
|
14
15
|
Thread.abort_on_exception = true
|
16
|
+
|
17
|
+
@connection_mutex = Mutex.new
|
15
18
|
end
|
16
19
|
|
17
20
|
def notify(channel, options = {}, &block)
|
@@ -34,17 +37,9 @@ module PgNotifier
|
|
34
37
|
def run
|
35
38
|
logger.info "Starting pg_notifier for #{channels.count} channels: [ #{channels.join(' ')} ]"
|
36
39
|
|
37
|
-
sig_read, sig_write = IO.pipe
|
38
|
-
|
39
|
-
(%w[INT TERM HUP] & Signal.list.keys).each do |sig|
|
40
|
-
trap sig do
|
41
|
-
sig_write.puts(sig)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
40
|
Thread.new do
|
46
41
|
channels.each do |channel|
|
47
|
-
pg_result = connection.exec "LISTEN #{channel};"
|
42
|
+
pg_result = @connection_mutex.synchronize { connection.exec "LISTEN #{channel};" }
|
48
43
|
|
49
44
|
unless pg_result.result_status.eql? PG::PGRES_COMMAND_OK
|
50
45
|
raise ChannelNotLaunched, "Channel ##{channel} not launched"
|
@@ -52,40 +47,39 @@ module PgNotifier
|
|
52
47
|
end
|
53
48
|
|
54
49
|
until @finish do
|
55
|
-
|
56
|
-
logger.info "Notifying channel:
|
50
|
+
if notification = wait_notification()
|
51
|
+
logger.info "Notifying channel: %s, pid: %s, payload: %s" % notification
|
57
52
|
|
58
|
-
subscriptions = subscriptions_by_channels.fetch
|
59
|
-
subscriptions.each { |subscription| subscription.notify(
|
53
|
+
subscriptions = subscriptions_by_channels.fetch notification.first, []
|
54
|
+
subscriptions.each { |subscription| subscription.notify(*notification) }
|
60
55
|
end
|
61
56
|
end
|
62
57
|
end
|
63
|
-
|
64
|
-
while io = IO.select([sig_read])
|
65
|
-
sig = io.first[0].gets.chomp
|
66
|
-
handle_signal(sig)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def handle_signal(sig)
|
71
|
-
logger.debug "Got #{sig} signal"
|
72
|
-
|
73
|
-
shutdown if %w[INT TERM HUP].include? sig
|
74
58
|
end
|
75
59
|
|
76
60
|
def shutdown
|
77
61
|
logger.info 'Shutting down'
|
78
62
|
|
79
63
|
@finish = true
|
80
|
-
unless connection.finished?
|
81
|
-
channels.each do |channel|
|
82
|
-
connection.exec "UNLISTEN #{channel};"
|
83
|
-
end
|
84
64
|
|
85
|
-
|
65
|
+
@connection_mutex.synchronize do
|
66
|
+
unless connection.finished?
|
67
|
+
connection.async_exec "UNLISTEN *;"
|
68
|
+
connection.finish
|
69
|
+
end
|
86
70
|
end
|
87
71
|
|
88
72
|
exit(0)
|
89
73
|
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def wait_notification
|
78
|
+
@connection_mutex.synchronize do
|
79
|
+
connection.wait_for_notify(timeout) do |channel, pid, payload|
|
80
|
+
return [channel, pid, payload]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
90
84
|
end
|
91
85
|
end
|
data/lib/pg_notifier/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_notifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- German Antsiferov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|