pg-locks-monitor 0.3.0 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3bd2f4561c83a6d10a0120670bb89f8140f01f3afad7434fbea969e113cc542
4
- data.tar.gz: 2fdb6e38890f1b085a32e72fee4d32eeb4e77e6a1f865737b2f3541d76d333ad
3
+ metadata.gz: 3c123754d01da5acc5a517511066987515ec3167c0063a59c67b272ba0fd39ac
4
+ data.tar.gz: c85d88f96b9593ebb04a849e6cc79958a3040eb119980d17585cb69fdea1733b
5
5
  SHA512:
6
- metadata.gz: 1396e94b66ae62db3ee552855cfe8b2e7d10c5efdf87f323bc889c8fb17530cf9076e6239f2dc9713907f28ddbb5de602f4baa507b38621419534be545be83c6
7
- data.tar.gz: a9bafef4918812b931737a0096cd2096181512e6eb5b35585b4e5caf50d7230f1dca4b8b2a37eeac7b521968577cf915cd97bff1f79b757dc909b5d1a4eb4f18
6
+ metadata.gz: b1f98b1c258f99a3943e178d81f370b664be38b1841edb302364f0796c06302b2487819bb09d66d3c6dd5161999af21ecb1e4e769f7ff0a3be287620774811f3
7
+ data.tar.gz: 340ae2e8bf451994e1f960a5c8196bd1739790b2e7634ded97c1990fb12d0c121078a301e8fcb9408a7bf63ce8f096730d8f038817fd18eb29a2114c7f6eeca0
data/README.md CHANGED
@@ -232,9 +232,4 @@ PgLocksMonitor.configure do |config|
232
232
 
233
233
  config.notifier_class = PgLocksEmailNotifier
234
234
  end
235
-
236
235
  ```
237
-
238
- ## Contributions
239
-
240
- This gem is in a very early stage of development so feedback and PRs are welcome.
@@ -2,25 +2,32 @@
2
2
 
3
3
  require "uri"
4
4
  require "pg"
5
- require "ruby-pg-extras"
5
+ require "rails-pg-extras"
6
+ require "active_record"
6
7
 
7
8
  module PgLocksMonitor
8
9
  def self.snapshot!
9
- locks = RubyPgExtras.locks(
10
+ locks = RailsPgExtras.locks(
10
11
  in_format: :hash,
11
12
  ).select do |lock|
12
13
  if (age = lock.fetch("age"))
13
- DurationHelper.parse_to_ms(age) > configuration.locks_min_duration_ms
14
+ (ActiveSupport::Duration.parse(age).to_f * 1000) > configuration.locks_min_duration_ms
14
15
  end
15
- end.select(&configuration.locks_filter_proc)
16
+ end.select do |lock|
17
+ # meta locks with duplicate data
18
+ lock.fetch("locktype") != "virtualxid"
19
+ end
20
+ .select(&configuration.locks_filter_proc)
16
21
  .first(configuration.locks_limit)
17
22
 
18
23
  if locks.count > 0 && configuration.monitor_locks
19
24
  configuration.notifier_class.call(locks)
20
25
  end
21
26
 
22
- blocking = RubyPgExtras.blocking(in_format: :hash).select do |block|
23
- DurationHelper.parse_to_ms(block.fetch("blocking_duration")) > configuration.blocking_min_duration_ms
27
+ blocking = RailsPgExtras.blocking(in_format: :hash).select do |block|
28
+ if (age = block.fetch("blocking_duration"))
29
+ (ActiveSupport::Duration.parse(age).to_f * 1000) > configuration.blocking_min_duration_ms
30
+ end
24
31
  end.select(&configuration.blocking_filter_proc)
25
32
  .first(configuration.locks_limit)
26
33
 
@@ -41,22 +48,6 @@ module PgLocksMonitor
41
48
  def self.configure
42
49
  yield(configuration)
43
50
  end
44
-
45
- class DurationHelper
46
- require "date"
47
-
48
- def self.parse_to_ms(duration_str)
49
- time = DateTime.strptime(duration_str, "%H:%M:%S.%N")
50
- hours = time.hour
51
- minutes = time.minute
52
- seconds = time.second
53
- nanoseconds = time.second_fraction * (10 ** 9)
54
-
55
- total_ms = (hours * 3600 * 1000) + (minutes * 60 * 1000) + (seconds * 1000) + (nanoseconds / 1_000_000).to_i
56
-
57
- total_ms
58
- end
59
- end
60
51
  end
61
52
 
62
53
  require "pg_locks_monitor/default_notifier"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgLocksMonitor
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.2"
5
5
  end
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.test_files = s.files.grep(%r{^(spec)/})
16
16
  s.require_paths = ["lib"]
17
17
  s.license = "MIT"
18
- s.add_dependency "ruby-pg-extras"
18
+ s.add_dependency "rails-pg-extras"
19
19
  s.add_dependency "slack-notifier"
20
20
  s.add_development_dependency "rake"
21
21
  s.add_development_dependency "rspec"
data/spec/smoke_spec.rb CHANGED
@@ -5,8 +5,8 @@ require "spec_helper"
5
5
  describe PgLocksMonitor do
6
6
  def spawn_update
7
7
  Thread.new do
8
- conn = PG.connect(ENV["DATABASE_URL"])
9
- conn.exec("
8
+ conn = RailsPgExtras.connection
9
+ conn.execute("
10
10
  BEGIN;
11
11
  UPDATE pg_locks_monitor_users SET name = 'Updated';
12
12
  select pg_sleep(2);
@@ -25,6 +25,10 @@ describe PgLocksMonitor do
25
25
  it "returns correct locks data" do
26
26
  spawn_update
27
27
  spawn_update
28
+ result = PgLocksMonitor.snapshot!
29
+ expect(result.fetch(:locks).count).to eq(0)
30
+ expect(result.fetch(:blocking).count).to eq(0)
31
+
28
32
  sleep 1
29
33
 
30
34
  result = PgLocksMonitor.snapshot!
data/spec/spec_helper.rb CHANGED
@@ -35,14 +35,15 @@ RSpec.configure do |config|
35
35
  end
36
36
 
37
37
  config.before(:suite) do
38
- conn = RubyPgExtras.connection
39
- conn.exec("CREATE TABLE IF NOT EXISTS pg_locks_monitor_users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);")
40
- conn.exec("INSERT INTO pg_locks_monitor_users (name) VALUES ('Alice');")
41
- conn.exec("INSERT INTO pg_locks_monitor_users (name) VALUES ('Bob');")
38
+ ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"])
39
+ conn = RailsPgExtras.connection
40
+ conn.execute("CREATE TABLE IF NOT EXISTS pg_locks_monitor_users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);")
41
+ conn.execute("INSERT INTO pg_locks_monitor_users (name) VALUES ('Alice');")
42
+ conn.execute("INSERT INTO pg_locks_monitor_users (name) VALUES ('Bob');")
42
43
  end
43
44
 
44
45
  config.after(:suite) do
45
- conn = RubyPgExtras.connection
46
- conn.exec("DROP TABLE IF EXISTS pg_locks_monitor_users;")
46
+ conn = RailsPgExtras.connection
47
+ conn.execute("DROP TABLE IF EXISTS pg_locks_monitor_users;")
47
48
  end
48
49
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-locks-monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-02 00:00:00.000000000 Z
11
+ date: 2024-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ruby-pg-extras
14
+ name: rails-pg-extras
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="