Skip to content

Commit

Permalink
[#6] date parsing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Oct 4, 2024
1 parent 1e032f8 commit 3689a4a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
13 changes: 8 additions & 5 deletions lib/pg-locks-monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

require "uri"
require "pg"
require "ruby-pg-extras"
require "rails-pg-extras"
require "active_record"

module PgLocksMonitor
def self.snapshot!
locks = RubyPgExtras.locks(
locks = RailsPgExtras.locks(
in_format: :hash,
).select do |lock|
if (age = lock.fetch("age"))
DurationHelper.parse_to_ms(age) > configuration.locks_min_duration_ms
(ActiveSupport::Duration.parse(age).to_f * 1000) > configuration.locks_min_duration_ms
end
end.select(&configuration.locks_filter_proc)
.first(configuration.locks_limit)
Expand All @@ -19,8 +20,10 @@ def self.snapshot!
configuration.notifier_class.call(locks)
end

blocking = RubyPgExtras.blocking(in_format: :hash).select do |block|
DurationHelper.parse_to_ms(block.fetch("blocking_duration")) > configuration.blocking_min_duration_ms
blocking = RailsPgExtras.blocking(in_format: :hash).select do |block|
if (age = block.fetch("blocking_duration"))
(ActiveSupport::Duration.parse(age).to_f * 1000) > configuration.locks_min_duration_ms
end
end.select(&configuration.blocking_filter_proc)
.first(configuration.locks_limit)

Expand Down
2 changes: 1 addition & 1 deletion lib/pg_locks_monitor/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module PgLocksMonitor
VERSION = "0.3.0"
VERSION = "0.3.1"
end
2 changes: 1 addition & 1 deletion pg-locks-monitor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.test_files = s.files.grep(%r{^(spec)/})
s.require_paths = ["lib"]
s.license = "MIT"
s.add_dependency "ruby-pg-extras"
s.add_dependency "rails-pg-extras"
s.add_dependency "slack-notifier"
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
Expand Down
8 changes: 6 additions & 2 deletions spec/smoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
describe PgLocksMonitor do
def spawn_update
Thread.new do
conn = PG.connect(ENV["DATABASE_URL"])
conn.exec("
conn = RailsPgExtras.connection
conn.execute("
BEGIN;
UPDATE pg_locks_monitor_users SET name = 'Updated';
select pg_sleep(2);
Expand All @@ -25,6 +25,10 @@ def spawn_update
it "returns correct locks data" do
spawn_update
spawn_update
result = PgLocksMonitor.snapshot!
expect(result.fetch(:locks).count).to eq(0)
expect(result.fetch(:blocking).count).to eq(0)

sleep 1

result = PgLocksMonitor.snapshot!
Expand Down
13 changes: 7 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
end

config.before(:suite) do
conn = RubyPgExtras.connection
conn.exec("CREATE TABLE IF NOT EXISTS pg_locks_monitor_users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);")
conn.exec("INSERT INTO pg_locks_monitor_users (name) VALUES ('Alice');")
conn.exec("INSERT INTO pg_locks_monitor_users (name) VALUES ('Bob');")
ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"])
conn = RailsPgExtras.connection
conn.execute("CREATE TABLE IF NOT EXISTS pg_locks_monitor_users (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);")
conn.execute("INSERT INTO pg_locks_monitor_users (name) VALUES ('Alice');")
conn.execute("INSERT INTO pg_locks_monitor_users (name) VALUES ('Bob');")
end

config.after(:suite) do
conn = RubyPgExtras.connection
conn.exec("DROP TABLE IF EXISTS pg_locks_monitor_users;")
conn = RailsPgExtras.connection
conn.execute("DROP TABLE IF EXISTS pg_locks_monitor_users;")
end
end

0 comments on commit 3689a4a

Please sign in to comment.