pg_notifier 0.1.0 → 0.1.1

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
- MTRmMzM3NjRjMTE2YjljYmYzZWQ2ODFmMmRiNTc0YWNjMDFmZDQ5Mg==
4
+ MDQ3YjdhYWExNjMzZjNhZTBhMWE5ZmQ1ZTU0NGU3YTNhMzY2MmQyZA==
5
5
  data.tar.gz: !binary |-
6
- YTQ5MWUxYzI0MzM0MjMzMTk0NGZlZWVlNGFjNDRiODI4M2Y4OWExOA==
6
+ MWQ4MzkzNzNkOGJjZDQ2MmMzYzc4ZTcxZWJjZmFjOTBiMzA3MjMzNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGU4MWNlNWFlNTY3NTI0ODI2OWQ4MzAwZjVmMDg2NjNmYzU0MzBmMzQ1ODY1
10
- YWQ1MTZiMjZkZTQxMzM3MGQxZTRlZTQ5YWYyYWNmY2JhOWViYTIxY2M4YTFh
11
- YWI1MDQ0MmE1MmI3Nzg1NjE3OGM1ZmU2NjVmYjExNzdiNTU1OGE=
9
+ YWU5ZGNiNDBhMmMzN2U1YTdiN2E1YTg4MDE5NGQ5MTQ5NjRiM2I4MWJiM2M0
10
+ YTgzMzdlYjBjYjA3M2RmMzAwZTEzZmY3N2ExZjQzYjQ5ODFiNjQ3MDQxZTVl
11
+ YTUyNjQxOWI4NDM3NzIwYjgzN2JiMjAwODU1MzRlOWFkNDNlMDU=
12
12
  data.tar.gz: !binary |-
13
- NzczNDUzMTRkOWZjNWM4OGNlNzhkZDA3MDIzYTU4NzNmMmI4NTQ2OGMyNTI5
14
- MDA4NjQyMjMxNjExNTVlMWNlNGQ4MTRhZDA0YTRlOWMzZTVhMjkzZmI3ZDY3
15
- MGQzNGNmMTgzODg1N2EzMjU1MWRiZWE2NTQ1NDY4OGU3ZDM0OGY=
13
+ YjkwNDUzN2E4ODg2N2U4ZWEwYTRiMDI1M2U1NzQ3MDJkZTRhYjcwZjY0NGZh
14
+ MjkwMTNkNTllMDY0NDE4ZDMwOGUzY2E3N2M5ODVhZjM4NWY2NGE3MTgwZjUz
15
+ NDJjODUyODI1ZGNmNzIxYWE0ZThiNmY3ZTQ0NDMzZWQ0OTZjOTI=
data/lib/pg_notifier.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "pg_notifier/version"
2
+ require "pg_notifier/errors"
2
3
  require "pg_notifier/subscription"
3
4
  require "pg_notifier/manager"
4
5
 
@@ -0,0 +1,4 @@
1
+ module PgNotifier
2
+ class PgNotifierError < StandardError; end
3
+ class ChannelNotLaunched < PgNotifierError; end
4
+ end
@@ -14,6 +14,8 @@ module PgNotifier
14
14
  @finish = false
15
15
  @mutex = Mutex.new
16
16
  @resource = ConditionVariable.new
17
+
18
+ Thread.abort_on_exception = true
17
19
  end
18
20
 
19
21
  def notify(channel, options = {}, &block)
@@ -47,11 +49,17 @@ module PgNotifier
47
49
  Thread.new do
48
50
  channels.each do |channel|
49
51
  pg_result = connection.exec "LISTEN #{channel};"
52
+
53
+ unless pg_result.result_status.eql? PG::PGRES_COMMAND_OK
54
+ raise ChannelNotLaunched, "Channel ##{channel} not launched"
55
+ end
50
56
  end
51
57
 
52
58
  @mutex.synchronize do
53
59
  until @finish do
54
60
  connection.wait_for_notify do |channel, pid, payload|
61
+ logger.info "Notifying channel: #{channel}, pid: #{pid}, payload: #{payload}"
62
+
55
63
  subscriptions = subscriptions_by_channels.fetch channel, []
56
64
  subscriptions.each { |subscription| subscription.notify(channel, pid, payload) }
57
65
  end
@@ -84,18 +92,28 @@ module PgNotifier
84
92
  logger.info 'Shutting down'
85
93
 
86
94
  @finish = true
87
- connection.finish unless connection.finished?
95
+ close_connection
88
96
 
89
97
  exit(0)
90
98
  end
91
99
 
100
+ def close_connection
101
+ unless connection.finished?
102
+ channels.each do |channel|
103
+ connection.exec "UNLISTEN #{channel};"
104
+ end
105
+
106
+ connection.finish
107
+ end
108
+ end
109
+
92
110
  def graceful_shutdown
93
111
  logger.info 'Gracefully shutting down'
94
112
 
95
113
  @finish = true
96
114
 
97
115
  @mutex.synchronize do
98
- connection.finish unless connection.finished?
116
+ close_connection
99
117
  @resource.signal
100
118
  end
101
119
 
@@ -1,3 +1,3 @@
1
1
  module PgNotifier
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.0
4
+ version: 0.1.1
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-22 00:00:00.000000000 Z
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -86,6 +86,7 @@ files:
86
86
  - bin/pg_notifier
87
87
  - bin/setup
88
88
  - lib/pg_notifier.rb
89
+ - lib/pg_notifier/errors.rb
89
90
  - lib/pg_notifier/manager.rb
90
91
  - lib/pg_notifier/subscription.rb
91
92
  - lib/pg_notifier/version.rb