midget_jobs 0.2.1 → 0.2.7

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: '0381bb924f700b2ab31f3bcee872560499d312666ddac89221f592b9fde9af19'
4
- data.tar.gz: f3c51ee3b9950148ec5b4c0803dea0380945c5e34a5672cceb37e52c8ff881b5
3
+ metadata.gz: 65c455e1ddfd935045beb212467ca4e1896310ac5e2a973871d5750936b1f7be
4
+ data.tar.gz: 89af9ee69b5201f0ca0866a7c335416d43824a04f3e8cdc4c21e2a7c1ceca7ff
5
5
  SHA512:
6
- metadata.gz: c37ac71de44ca971da13aae9b8a2a17e2b7777904ca0a8fa777c495a2c5d3a1d7276b3b447937e01b734bb45b9caaf34fbd12961731cfa6f36f9e01849768781
7
- data.tar.gz: 5f3f5b3e68a5b3876cf8df4f3b652c8cf64d60361eaa55ec57fd6a9d2e3b755051b0810019e83cdb7fe31bc76d90937c7662fe17fed2d04c69d5c77a32276031
6
+ metadata.gz: f00c9d9a709239e7910f7728433f00309cb1e1c39be1773dc7edd5c7d5911f337f262b76663c77dc2a276ed33a3c7020214395d72be7370680b933183f8dd4aa
7
+ data.tar.gz: d292108100513964de006d19128cbed3bff3b0766bd4e7ec723fd77d57d96a3ed1b7d9aa2b64f9783f46acdebc1088f0f9c221942e7c10a8b7b0a17fc00492ef
data/README.md CHANGED
@@ -30,6 +30,24 @@ Then run
30
30
  $ rails generate midget_jobs:install
31
31
 
32
32
  This generator creates an initializer file at config/initializers and migrations to db/migrate
33
+
34
+ #### With Puma's multiple workers on production
35
+
36
+ In file `config/initializers/midget_jobs.rb` disable start with puma:
37
+
38
+ ...
39
+ if Rails.const_defined?('Server')# || $0.include?('puma')
40
+
41
+ In file `config/puma/production.rb` update `on_worker_boot do` block as:
42
+
43
+ ...
44
+ on_worker_boot do
45
+ ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
46
+ Rails.logger.debug 'on_worker_boot starting jobs'
47
+
48
+ MidgetJobs.initialize_workers
49
+ ...
50
+ end
33
51
 
34
52
  ## Usage
35
53
 
@@ -1,3 +1,3 @@
1
1
  module MidgetJobs
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -1,6 +1,9 @@
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
@@ -13,10 +16,29 @@ module PostgresNotificationsListener
13
16
  Rails.logger.info "postgres #{event.inspect}, pid: #{pid.inspect}, data: #{data.inspect}"
14
17
  process_notification(data)
15
18
  end
19
+ rescue PG::ConnectionBad => e
20
+ measure_exception_severity
21
+ Rails.logger.error "#{name} listening_job wait_for_notify lost connection #{e.inspect} retry #{@exception_counter}th time!"
22
+ Rails.logger.error 'Reestablish AR connection...'
23
+ ActiveRecord::Base.connection.verify!
24
+ sleep 0.1
25
+ @exception_counter.to_i < EXCEPTION_TIME ? retry : raise(e)
16
26
  rescue StandardError => e
17
- Rails.logger.error "-----------Ex #{name} listening_job wait_for_notify #{e}-retrying!------------"
18
- retry
27
+ measure_exception_severity
28
+ Rails.logger.error "#{name} listening_job wait_for_notify exception #{e.inspect} retry #{@exception_counter}th time!"
29
+ sleep 0.1
30
+ @exception_counter.to_i < EXCEPTION_TIME ? retry : raise(e)
19
31
  end
20
32
  end.abort_on_exception = true
21
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 if @last_exception_time.present?
43
+ end
22
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.1
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - josefchmel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-25 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