midget_jobs 0.2.4 → 0.2.5

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: b3e3afb7822a9f252d8442f20d7bf89f80e23cb0d782cff7b53427803c67ff5c
4
- data.tar.gz: bf1d546693adaa2c2349b220d4677bad7c7def45ad2b2fda835e0f83247dd036
3
+ metadata.gz: 48a48a20bf9bb5ba396ec2f468c15fbcb6ac6d007c52ae8980fede79d7b91ad2
4
+ data.tar.gz: d2e1a91c7327de1fe53e60143867fe215e913c83d0763f113a3b66c13b6acbfa
5
5
  SHA512:
6
- metadata.gz: cdadf27c542f6fbef5d84067b54acbb871d95bfc4f319e480705ef73687553918cc2c008c7b5feef565521fdb91b6dce7f442081d076382f19ceb167fae61dd2
7
- data.tar.gz: dd99f425b0939821dc2cd00d9cce0fd8fce80b0c064cb007732b8cb4e2e583e1bab3d8372a48e0b55e7118ccb2c8a381eb00e2afc13c675e6e2bf5716ad9bf57
6
+ metadata.gz: '05855d4770375c491f6c887a5a8ed2e37295fb68ef760dff0bbc841547db5cc42fa9c3a33985cddd738c6dc8e505c846efbd8173253555567b91780fb99f8f44'
7
+ data.tar.gz: 9097c8a42b63a8085858dea385b9a58017451aa019fa6562635cd960729e653dda42b33915ced46e0a5195bd653a243d37df26a6e02b47bb101cf015bb99c148
@@ -1,3 +1,3 @@
1
1
  module MidgetJobs
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PostgresNotificationsListener
4
+ TIME_TO_DIE = 3
5
+ EXCEPTION_TIME = 0.333
6
+
4
7
  def listen_notifications
5
8
  Rails.logger.info "#{name} listening_job started"
6
9
  Thread.new do
7
10
  Rails.logger.info "#{name} listening_job running on #{name.pluralize.underscore}_notices"
8
11
  connection.execute "LISTEN #{name.pluralize.underscore}_notices"
9
- exception_counter ||= 0
10
12
  loop do
11
13
  Rails.logger.info "#{name} listening_job wait_for_notify"
12
14
  connection.raw_connection.wait_for_notify do |event, pid, payload|
@@ -14,20 +16,29 @@ module PostgresNotificationsListener
14
16
  Rails.logger.info "postgres #{event.inspect}, pid: #{pid.inspect}, data: #{data.inspect}"
15
17
  process_notification(data)
16
18
  end
17
- exception_counter = 0
18
19
  rescue PG::ConnectionBad => e
19
- exception_counter += 1
20
+ measure_exception_severity
20
21
  Rails.logger.error "#{name} listening_job wait_for_notify lost connection #{e.inspect} retry #{exception_counter}th time!"
21
- Rails.logger.error "Reestablish AR connection..."
22
+ Rails.logger.error 'Reestablish AR connection...'
22
23
  ActiveRecord::Base.connection.verify!
23
24
  sleep 0.1
24
- (exception_counter < 3) ? retry : raise(e)
25
+ @exception_counter.to_i < EXCEPTION_TIME ? retry : raise(e)
25
26
  rescue StandardError => e
26
- exception_counter += 1
27
+ measure_exception_severity
27
28
  Rails.logger.error "#{name} listening_job wait_for_notify exception #{e.inspect} retry #{exception_counter}th time!"
28
29
  sleep 0.1
29
- (exception_counter < 3) ? retry : raise(e)
30
+ @exception_counter.to_i < EXCEPTION_TIME ? retry : raise(e)
30
31
  end
31
32
  end.abort_on_exception = true
32
33
  end
34
+
35
+ private
36
+
37
+ def measure_exception_severity
38
+ @exception_counter, @last_exception_time = too_soon? ? [exception_counter.to_i + 1, @last_exception_time] : [0, Time.zone.now]
39
+ end
40
+
41
+ def too_soon?
42
+ (Time.zone.now - @last_exception_time) < TOO_SOON
43
+ end
33
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midget_jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - josefchmel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-02 00:00:00.000000000 Z
11
+ date: 2020-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler