midget_jobs 0.1.6 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +67 -0
- data/Gemfile +1 -0
- data/README.md +27 -0
- data/db/migrate/20191022104513_create_midget_jobs.rb +2 -0
- data/lib/generators/midget_jobs/templates/create_midget_jobs.rb +2 -0
- data/lib/generators/midget_jobs/templates/midget_job_initializer.rb +1 -0
- data/lib/midget_jobs/version.rb +1 -1
- data/lib/models/concerns/postgres_notifications_listener.rb +22 -13
- data/lib/models/midget_job.rb +3 -1
- data/midget_job.gemspec +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3e3afb7822a9f252d8442f20d7bf89f80e23cb0d782cff7b53427803c67ff5c
|
4
|
+
data.tar.gz: bf1d546693adaa2c2349b220d4677bad7c7def45ad2b2fda835e0f83247dd036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdadf27c542f6fbef5d84067b54acbb871d95bfc4f319e480705ef73687553918cc2c008c7b5feef565521fdb91b6dce7f442081d076382f19ceb167fae61dd2
|
7
|
+
data.tar.gz: dd99f425b0939821dc2cd00d9cce0fd8fce80b0c064cb007732b8cb4e2e583e1bab3d8372a48e0b55e7118ccb2c8a381eb00e2afc13c675e6e2bf5716ad9bf57
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# all the cops that are enabled in the default
|
2
|
+
# https://github.com/bbatsov/rubocop/blob/master/config/enabled.yml
|
3
|
+
# https://github.com/bbatsov/rubocop/blob/master/config/default.yml
|
4
|
+
|
5
|
+
require: rubocop-performance
|
6
|
+
require: rubocop-rails
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
TargetRubyVersion: 2.6
|
10
|
+
Include:
|
11
|
+
- '**/Rakefile'
|
12
|
+
- '**/config.ru'
|
13
|
+
Exclude:
|
14
|
+
- 'db/**/*'
|
15
|
+
- 'config/**/*'
|
16
|
+
- 'script/**/*'
|
17
|
+
- !ruby/regexp /old_and_unused\.rb$/
|
18
|
+
|
19
|
+
Layout/LineLength:
|
20
|
+
Max: 160
|
21
|
+
|
22
|
+
Metrics/MethodLength:
|
23
|
+
CountComments: false # count full line comments?
|
24
|
+
Max: 12
|
25
|
+
|
26
|
+
Metrics/AbcSize:
|
27
|
+
Max: 20
|
28
|
+
|
29
|
+
Style/AndOr:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Documentation:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/ClassAndModuleChildren:
|
36
|
+
# Checks the style of children definitions at classes and modules.
|
37
|
+
#
|
38
|
+
# Basically there are two different styles:
|
39
|
+
#
|
40
|
+
# `nested` - have each child on a separate line
|
41
|
+
# class Foo
|
42
|
+
# class Bar
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# `compact` - combine definitions as much as possible
|
47
|
+
# class Foo::Bar
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# The compact style is only forced, for classes / modules with one child.
|
51
|
+
EnforcedStyle: nested
|
52
|
+
SupportedStyles:
|
53
|
+
- nested
|
54
|
+
- compact
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
CaseIndentation:
|
58
|
+
# Valid values are: case, end
|
59
|
+
EnforcedStyle: case
|
60
|
+
IndentOneStep: true
|
61
|
+
|
62
|
+
# Prefer `has_?` style for Hash methods
|
63
|
+
Style/PreferredHashMethods:
|
64
|
+
EnforcedStyle: verbose
|
65
|
+
|
66
|
+
Rails:
|
67
|
+
Enabled: true
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,15 @@
|
|
3
3
|
MidgetJobs is simple implementation of background jobs for [Ruby on Rails](https://rubyonrails.org). I does not meant to be used without it.
|
4
4
|
It is connected to [ActiveJob](https://github.com/rails/rails/tree/master/activejob) and [PostgreSQL](https://www.postgresql.org) using its triggers, notifications and listening.
|
5
5
|
|
6
|
+
## Update from 0.1.7 to 0.2.0
|
7
|
+
There is new column **runner** in **midget_jobs** table!
|
8
|
+
It is used to recognise who will run particular job (if there are multiple application over one database)
|
9
|
+
|
10
|
+
I added new configuration to Initializer. Add this to your config/initializers/midget_jobs.rb
|
11
|
+
```Rails.application.config.x.midget_jobs.runner = Rails.application.class.parent_name```
|
12
|
+
|
13
|
+
Please create migration with generator
|
14
|
+
```rails generate migration AddRunnerToMidgetJob runner:string:index```
|
6
15
|
|
7
16
|
## Installation
|
8
17
|
|
@@ -21,6 +30,24 @@ Then run
|
|
21
30
|
$ rails generate midget_jobs:install
|
22
31
|
|
23
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
|
24
51
|
|
25
52
|
## Usage
|
26
53
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
Rails.application.config.active_job.queue_adapter = :midget_job
|
2
2
|
Rails.application.config.x.midget_jobs.at_once = 4
|
3
|
+
Rails.application.config.x.midget_jobs.runner = Rails.application.class.module_parent_name
|
3
4
|
|
4
5
|
if Rails.const_defined?('Server') || $0.include?('puma')
|
5
6
|
Rails.application.config.after_initialize do
|
data/lib/midget_jobs/version.rb
CHANGED
@@ -2,22 +2,31 @@
|
|
2
2
|
|
3
3
|
module PostgresNotificationsListener
|
4
4
|
def listen_notifications
|
5
|
-
Rails.logger.info "#{
|
5
|
+
Rails.logger.info "#{name} listening_job started"
|
6
6
|
Thread.new do
|
7
|
-
Rails.logger.info "#{
|
8
|
-
|
7
|
+
Rails.logger.info "#{name} listening_job running on #{name.pluralize.underscore}_notices"
|
8
|
+
connection.execute "LISTEN #{name.pluralize.underscore}_notices"
|
9
|
+
exception_counter ||= 0
|
9
10
|
loop do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
process_notification(data)
|
16
|
-
end
|
17
|
-
rescue StandardError => error
|
18
|
-
Rails.logger.error "-------------Ex #{self.name} listening_job wait_for_notify #{error}----------------"
|
19
|
-
retry
|
11
|
+
Rails.logger.info "#{name} listening_job wait_for_notify"
|
12
|
+
connection.raw_connection.wait_for_notify do |event, pid, payload|
|
13
|
+
data = JSON.parse payload, symbolize_names: true
|
14
|
+
Rails.logger.info "postgres #{event.inspect}, pid: #{pid.inspect}, data: #{data.inspect}"
|
15
|
+
process_notification(data)
|
20
16
|
end
|
17
|
+
exception_counter = 0
|
18
|
+
rescue PG::ConnectionBad => e
|
19
|
+
exception_counter += 1
|
20
|
+
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
|
+
ActiveRecord::Base.connection.verify!
|
23
|
+
sleep 0.1
|
24
|
+
(exception_counter < 3) ? retry : raise(e)
|
25
|
+
rescue StandardError => e
|
26
|
+
exception_counter += 1
|
27
|
+
Rails.logger.error "#{name} listening_job wait_for_notify exception #{e.inspect} retry #{exception_counter}th time!"
|
28
|
+
sleep 0.1
|
29
|
+
(exception_counter < 3) ? retry : raise(e)
|
21
30
|
end
|
22
31
|
end.abort_on_exception = true
|
23
32
|
end
|
data/lib/models/midget_job.rb
CHANGED
@@ -3,6 +3,8 @@ require 'models/concerns/postgres_notifications_listener'
|
|
3
3
|
class MidgetJob < ActiveRecord::Base
|
4
4
|
extend PostgresNotificationsListener
|
5
5
|
|
6
|
+
default_scope { where(runner: Rails.configuration.x.midget_jobs.runner) }
|
7
|
+
|
6
8
|
scope :for_processing, -> { where('run_at < ?', Time.current) }
|
7
9
|
|
8
10
|
validates :job_id, :queue, :serialized, :run_at, presence: true
|
@@ -16,7 +18,7 @@ class MidgetJob < ActiveRecord::Base
|
|
16
18
|
rescue => detail
|
17
19
|
Rails.logger.error detail.cause
|
18
20
|
end
|
19
|
-
end
|
21
|
+
end.abort_on_exception = true
|
20
22
|
end
|
21
23
|
|
22
24
|
def self.process_notification(hash_data)
|
data/midget_job.gemspec
CHANGED
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.require_paths = ["lib"]
|
30
30
|
|
31
31
|
spec.add_development_dependency "bundler", "~> 2.0"
|
32
|
-
spec.add_development_dependency "rake", "
|
32
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
33
33
|
spec.add_development_dependency "minitest", "~> 5.0"
|
34
34
|
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.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- josefchmel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 12.3.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,6 +60,7 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
|
+
- ".rubocop.yml"
|
63
64
|
- ".travis.yml"
|
64
65
|
- CODE_OF_CONDUCT.md
|
65
66
|
- Gemfile
|