pg_notifier 0.0.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzdhN2Q4MTRlMzM4MjVjYWEwY2NhYmM0Y2UyY2EyNzQ2MGJmY2UzOQ==
5
+ data.tar.gz: !binary |-
6
+ ZDI1NjA0ZWNhMDBmMWQwYTMwMjQyNTY4ZTM3YWUwZDVlNzkwNzEyMg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzE0ODc1MGM5Y2Y3ZGE1YjBlZjBjMWQ0Yzk5MmVmYTZhODJmNzRiMGEzYzZi
10
+ OTYwZjJjNTMzZjU0NTA0OGZkZGE3MDA5M2NhNjk4ZWE1YzEwNDlhMTM2MzU1
11
+ ZmFhMTU3YzMzZjliMWI4NDQ5YzA1ZGQzZTg5OGYwNzFjY2U3ZTE=
12
+ data.tar.gz: !binary |-
13
+ YzRhNjNjMmM5MzkyOGQ3NTMyZjlmMTMwNGRjMDNiM2JmZGYwMWZiYzM4YjM4
14
+ MzhkMjY5NmZlZDgzZTNhN2VhNWI4NjBmNDhmYmFjMDU0ZWZiMTdmOTVlM2U1
15
+ ODg1MzdmM2U1MTkzZmYwNTI4N2IyMzlmZjk4ZmVmNDA1YzFjMDE=
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_notifier.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 German Antsiferov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # PgNotifier
2
+
3
+ Process notifies about postgresql notifications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pg_notifier'
11
+ ```
12
+
13
+ ## Quickstart
14
+
15
+ Create notifiers.rb:
16
+
17
+ ``` ruby
18
+ require 'pg_notifier'
19
+
20
+ module PgNotifier
21
+ configure do |notifier|
22
+ notifier.logger = Logger.new('/var/log/pg_notifier.log')
23
+ notifier.timeout = 1
24
+ notifier.db_config = {
25
+ host: 'localhost',
26
+ port: 5432,
27
+ dbname: database_production,
28
+ user: 'postgres',
29
+ password: 'postgres'
30
+ }
31
+ end
32
+
33
+ notify 'created_user' do |channel, pid, payload|
34
+ puts "#{channel} #{pid} #{payload}"
35
+ end
36
+ end
37
+ ```
38
+ Run it with the pg_notifier executable:
39
+
40
+ ``` bash
41
+ $ pg_notifier notifiers.rb
42
+ ```
43
+
44
+ If you need to load your entire environment for your jobs, simply add:
45
+
46
+ ``` bash
47
+ require 'pg_notifier'
48
+
49
+ require './config/boot'
50
+ require './config/environment'
51
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pg_notifier"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/pg_notifier ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ STDERR.sync = STDOUT.sync = true
4
+
5
+ require 'pathname'
6
+ require 'pg_notifier'
7
+
8
+ usage = 'pg_notifier <file.rb>'
9
+ file = ARGV.shift or abort usage
10
+
11
+ file = "./#{file}" unless Pathname.new(file).absolute?
12
+
13
+ require file
14
+
15
+ PgNotifier::run
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,105 @@
1
+ require 'pg'
2
+ require 'thread'
3
+ require 'logger'
4
+
5
+ module PgNotifier
6
+ class Manager
7
+ attr_accessor :logger, :db_config, :timeout
8
+
9
+ def initialize(attrs = {})
10
+ @logger = attrs.fetch :logger, Logger.new(STDOUT)
11
+ @db_config = attrs.fetch :db_config , {}
12
+ @timeout = attrs.fetch :timeout, 1
13
+
14
+ @finish = false
15
+ @mutex = Mutex.new
16
+ @resource = ConditionVariable.new
17
+ end
18
+
19
+ def notify(channel, options = {}, &block)
20
+ subscriptions_by_channels[channel] ||= []
21
+ subscriptions_by_channels[channel] << Subscription.new(channel, options, &block)
22
+ end
23
+
24
+ def subscriptions_by_channels
25
+ @subscriptions_by_channels ||= {}
26
+ end
27
+
28
+ def channels
29
+ subscriptions_by_channels.keys
30
+ end
31
+
32
+ def connection
33
+ @connection ||= PG::Connection.open db_config
34
+ end
35
+
36
+ def run
37
+ logger.info "Starting pg_notifier for #{channels.count} channels: [ #{channels.join(' ')} ]"
38
+
39
+ sig_read, sig_write = IO.pipe
40
+
41
+ (%w[INT TERM HUP] & Signal.list.keys).each do |sig|
42
+ trap sig do
43
+ sig_write.puts(sig)
44
+ end
45
+ end
46
+
47
+ Thread.new do
48
+ channels.each do |channel|
49
+ pg_result = connection.exec "LISTEN #{channel};"
50
+ end
51
+
52
+ @mutex.synchronize do
53
+ until @finish do
54
+ connection.wait_for_notify do |channel, pid, payload|
55
+ subscriptions = subscriptions_by_channels.fetch channel, []
56
+ subscriptions.each { |subscription| subscription.notify(channel, pid, payload) }
57
+ end
58
+
59
+ @resource.wait @mutex, timeout
60
+ end
61
+ end
62
+ 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
+ case sig
74
+ when 'INT'
75
+ shutdown
76
+ when 'TERM'
77
+ graceful_shutdown
78
+ when 'HUP'
79
+ graceful_shutdown
80
+ end
81
+ end
82
+
83
+ def shutdown
84
+ logger.info 'Shutting down'
85
+
86
+ @finish = true
87
+ connection.finish unless connection.finished?
88
+
89
+ exit(0)
90
+ end
91
+
92
+ def graceful_shutdown
93
+ logger.info 'Gracefully shutting down'
94
+
95
+ @finish = true
96
+
97
+ @mutex.synchronize do
98
+ connection.finish unless connection.finished?
99
+ @resource.signal
100
+ end
101
+
102
+ exit(0)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,15 @@
1
+ module PgNotifier
2
+ class Subscription
3
+ attr_reader :channel, :options, :block
4
+
5
+ def initialize(channel, options, &block)
6
+ @channel = channel
7
+ @options = options
8
+ @block = block
9
+ end
10
+
11
+ def notify(channel, pid, payload)
12
+ block.call [channel, pid, payload]
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module PgNotifier
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ require "pg_notifier/version"
2
+ require "pg_notifier/subscription"
3
+ require "pg_notifier/manager"
4
+
5
+ module PgNotifier
6
+ class << self
7
+ def manager
8
+ @manager ||= Manager.new
9
+ end
10
+
11
+ def run
12
+ manager.run
13
+ end
14
+
15
+ def notify(channel, options = {}, &block)
16
+ manager.notify(channel, options, &block)
17
+ end
18
+
19
+ def configure(&block)
20
+ manager.tap(&block)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg_notifier/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pg_notifier"
8
+ spec.version = PgNotifier::VERSION
9
+ spec.authors = ["German Antsiferov"]
10
+ spec.email = ["dxdy@bk.ru"]
11
+
12
+ spec.summary = %q{Process notifies about postgresql notifications.}
13
+ spec.description = %q{Process notifies about postgresql notifications.}
14
+ spec.homepage = "https://github.com/mr-dxdy/pg_notifier.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "pg"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - German Antsiferov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Process notifies about postgresql notifications.
70
+ email:
71
+ - dxdy@bk.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/pg_notifier
84
+ - bin/setup
85
+ - lib/pg_notifier.rb
86
+ - lib/pg_notifier/manager.rb
87
+ - lib/pg_notifier/subscription.rb
88
+ - lib/pg_notifier/version.rb
89
+ - pg_notifier.gemspec
90
+ homepage: https://github.com/mr-dxdy/pg_notifier.git
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.6.10
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Process notifies about postgresql notifications.
114
+ test_files: []