pg_notifier 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmZlMmY5YjE5MzVhMjI1NjljZGVkZTU2NGUwYjA2MGFmMGRmZDcxNg==
4
+ MTA3MzYyYTM0ODk3MDlkMmU2NjA1ODEyOGYyMjg4NTNiMmZjNmZlYg==
5
5
  data.tar.gz: !binary |-
6
- ZjRkYjU1MjM4Zjk4ZTUzOGYxZjg4YmFkMTAyYmIyNWFhZDYyZDM5Yw==
6
+ MjJiODFhNmUxODk5NDc2NDIyYjRmNWU1NGQzZjg3YjY3YTU2NGQ5MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmM5MTA3M2ZjZWU3NjJmZDkyMWMyNmM0OTZiYmRhMzBlNzM3NGVlYWM1OWNl
10
- ZWRjZGNjZTljNDA3Zjk0YTdkODhkOGIyNWY4Y2M2MDBkMmI5ZmNiYjQyNzU2
11
- NTZiNzI0YjY2ZDI4NDUxMjk1YzdmMmQxYjBmODgxYjczODUwYWU=
9
+ ZGI1ZmZjMDViMzRhM2U2ZTJjZTBiYTk5MTE2Y2VkMWJhOTI0OGNkN2U4MGY5
10
+ Yjk5MjE5ZDhiNDEzYmU5ZWVhNWI3ODNlMmRmODQ1NzRlMTk1YzhkMGZjMTNi
11
+ ZTc2OTY4NDgwODJhNTdjNzI0Yzk2MTA3MjU3OWJlYmEyYzk3OWE=
12
12
  data.tar.gz: !binary |-
13
- Yzg3NjE3YmM3MjRmYTgyNTUzMDkyOTczY2VkMjYyYTM4NzQ2NDUzYWI2YWYy
14
- MmJiZWFjNzg5OWE0ZGJkOWNlMDZmMTAzOTA1MWU4NTlhOTQ4OTg3ZmNhYTNl
15
- ZTlhMjcyODZhYWZmNzE0N2FkMTExMzBjNmFiM2QyYmFlOTcyZDU=
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)
@@ -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
- connection.wait_for_notify do |channel, pid, payload|
56
- logger.info "Notifying channel: #{channel}, pid: #{pid}, payload: #{payload}"
50
+ if notification = wait_notification()
51
+ logger.info "Notifying channel: %s, pid: %s, payload: %s" % notification
57
52
 
58
- subscriptions = subscriptions_by_channels.fetch channel, []
59
- subscriptions.each { |subscription| subscription.notify(channel, pid, payload) }
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
- connection.finish
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
@@ -1,3 +1,3 @@
1
1
  module PgNotifier
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
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.2
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-23 00:00:00.000000000 Z
11
+ date: 2017-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg