back_run 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +131 -0
- data/LICENSE.txt +21 -0
- data/README.md +109 -0
- data/Rakefile +6 -0
- data/back_run.gemspec +34 -0
- data/bin/back_run +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/back_run.rb +18 -0
- data/lib/back_run/adapter.rb +21 -0
- data/lib/back_run/job.rb +74 -0
- data/lib/back_run/metrics_collector.rb +13 -0
- data/lib/back_run/pubsub.rb +66 -0
- data/lib/back_run/version.rb +3 -0
- data/lib/back_run/worker.rb +52 -0
- metadata +206 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 49e48588544fa2abddde853d63d946fa8b28c8f3
|
4
|
+
data.tar.gz: 221d6bf2fb86257ea4fb937354758fe9a81c1094
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a462d8f7d70b9fd2dfa9f02913aca9ac4265a575fb225f1ba749366401d6faec2b45d0f85b9548e9a9fafd270e96c79db5869fa4aa9068d9ee7f100d124b18e2
|
7
|
+
data.tar.gz: b4a7f28f1aa464b3ae6068498a9b6f368dada8b8607650fa5f56fc56c4d50b354f1c9546cf5680422767df8691207726c2fc9e9a6d6627250dca9fe1771a8c8b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at alanhalatian@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
back_run (0.1.0)
|
5
|
+
activesupport (~> 5.2)
|
6
|
+
concurrent-ruby (~> 1.1)
|
7
|
+
google-cloud-pubsub (~> 0.37)
|
8
|
+
thor (~> 0.20)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
activesupport (5.2.3)
|
14
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
|
+
i18n (>= 0.7, < 2)
|
16
|
+
minitest (~> 5.1)
|
17
|
+
tzinfo (~> 1.1)
|
18
|
+
addressable (2.6.0)
|
19
|
+
public_suffix (>= 2.0.2, < 4.0)
|
20
|
+
ast (2.4.0)
|
21
|
+
byebug (11.0.1)
|
22
|
+
coderay (1.1.2)
|
23
|
+
concurrent-ruby (1.1.5)
|
24
|
+
diff-lcs (1.3)
|
25
|
+
faraday (0.15.4)
|
26
|
+
multipart-post (>= 1.2, < 3)
|
27
|
+
google-cloud-core (1.3.0)
|
28
|
+
google-cloud-env (~> 1.0)
|
29
|
+
google-cloud-env (1.2.0)
|
30
|
+
faraday (~> 0.11)
|
31
|
+
google-cloud-pubsub (0.37.1)
|
32
|
+
concurrent-ruby (~> 1.1)
|
33
|
+
google-cloud-core (~> 1.2)
|
34
|
+
google-gax (~> 1.7)
|
35
|
+
googleapis-common-protos (>= 1.3.9, < 2.0)
|
36
|
+
grpc-google-iam-v1 (~> 0.6.9)
|
37
|
+
google-gax (1.7.0)
|
38
|
+
google-protobuf (~> 3.2)
|
39
|
+
googleapis-common-protos (>= 1.3.5, < 2.0)
|
40
|
+
googleauth (>= 0.6.2, < 0.10.0)
|
41
|
+
grpc (>= 1.7.2, < 2.0)
|
42
|
+
rly (~> 0.2.3)
|
43
|
+
google-protobuf (3.9.0-universal-darwin)
|
44
|
+
googleapis-common-protos (1.3.9)
|
45
|
+
google-protobuf (~> 3.0)
|
46
|
+
googleapis-common-protos-types (~> 1.0)
|
47
|
+
grpc (~> 1.0)
|
48
|
+
googleapis-common-protos-types (1.0.4)
|
49
|
+
google-protobuf (~> 3.0)
|
50
|
+
googleauth (0.8.1)
|
51
|
+
faraday (~> 0.12)
|
52
|
+
jwt (>= 1.4, < 3.0)
|
53
|
+
memoist (~> 0.16)
|
54
|
+
multi_json (~> 1.11)
|
55
|
+
os (>= 0.9, < 2.0)
|
56
|
+
signet (~> 0.7)
|
57
|
+
grpc (1.22.0-universal-darwin)
|
58
|
+
google-protobuf (~> 3.8)
|
59
|
+
googleapis-common-protos-types (~> 1.0)
|
60
|
+
grpc-google-iam-v1 (0.6.9)
|
61
|
+
googleapis-common-protos (>= 1.3.1, < 2.0)
|
62
|
+
grpc (~> 1.0)
|
63
|
+
i18n (1.6.0)
|
64
|
+
concurrent-ruby (~> 1.0)
|
65
|
+
jaro_winkler (1.5.3)
|
66
|
+
jwt (2.2.1)
|
67
|
+
memoist (0.16.0)
|
68
|
+
method_source (0.9.2)
|
69
|
+
minitest (5.11.3)
|
70
|
+
multi_json (1.13.1)
|
71
|
+
multipart-post (2.1.1)
|
72
|
+
os (1.0.1)
|
73
|
+
parallel (1.17.0)
|
74
|
+
parser (2.6.3.0)
|
75
|
+
ast (~> 2.4.0)
|
76
|
+
pry (0.12.2)
|
77
|
+
coderay (~> 1.1.0)
|
78
|
+
method_source (~> 0.9.0)
|
79
|
+
pry-byebug (3.7.0)
|
80
|
+
byebug (~> 11.0)
|
81
|
+
pry (~> 0.10)
|
82
|
+
public_suffix (3.1.1)
|
83
|
+
rainbow (3.0.0)
|
84
|
+
rake (10.5.0)
|
85
|
+
rly (0.2.3)
|
86
|
+
rspec (3.8.0)
|
87
|
+
rspec-core (~> 3.8.0)
|
88
|
+
rspec-expectations (~> 3.8.0)
|
89
|
+
rspec-mocks (~> 3.8.0)
|
90
|
+
rspec-core (3.8.2)
|
91
|
+
rspec-support (~> 3.8.0)
|
92
|
+
rspec-expectations (3.8.4)
|
93
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
94
|
+
rspec-support (~> 3.8.0)
|
95
|
+
rspec-mocks (3.8.1)
|
96
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
97
|
+
rspec-support (~> 3.8.0)
|
98
|
+
rspec-support (3.8.2)
|
99
|
+
rubocop (0.73.0)
|
100
|
+
jaro_winkler (~> 1.5.1)
|
101
|
+
parallel (~> 1.10)
|
102
|
+
parser (>= 2.6)
|
103
|
+
rainbow (>= 2.2.2, < 4.0)
|
104
|
+
ruby-progressbar (~> 1.7)
|
105
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
106
|
+
ruby-progressbar (1.10.1)
|
107
|
+
signet (0.11.0)
|
108
|
+
addressable (~> 2.3)
|
109
|
+
faraday (~> 0.9)
|
110
|
+
jwt (>= 1.5, < 3.0)
|
111
|
+
multi_json (~> 1.10)
|
112
|
+
thor (0.20.3)
|
113
|
+
thread_safe (0.3.6)
|
114
|
+
tzinfo (1.2.5)
|
115
|
+
thread_safe (~> 0.1)
|
116
|
+
unicode-display_width (1.6.0)
|
117
|
+
|
118
|
+
PLATFORMS
|
119
|
+
ruby
|
120
|
+
|
121
|
+
DEPENDENCIES
|
122
|
+
back_run!
|
123
|
+
bundler (~> 1.17)
|
124
|
+
pry-byebug (~> 3.7)
|
125
|
+
rake (~> 10.0)
|
126
|
+
rspec (~> 3.0)
|
127
|
+
rspec-mocks (~> 3.0)
|
128
|
+
rubocop (~> 0.73.0)
|
129
|
+
|
130
|
+
BUNDLED WITH
|
131
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Alan Halatian
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# BackRun
|
2
|
+
|
3
|
+
BackRun is a backround job system that is compatible with ActiveJob interface of Rails and allows developers to easily enqueue jobs to a Google Pub Sub Backend.
|
4
|
+
BackRun uses threads to handle many jobs at the same time in the same process.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
|
8
|
+
- Transparent enqueuing with ActiveJob to Google Pub Sub
|
9
|
+
- Enqueue jobs with delay
|
10
|
+
- Each worker can be configured with an amount of threads that will be the responsibles for running the enqueued jobs. If there are no available threads the job is enqueued again.
|
11
|
+
- If a job fails, it is retried at most two times at least five minutes apart.
|
12
|
+
- Collect metrics using ActiveSupport::Notifications
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'back_run'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install back_run
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Configuration
|
33
|
+
|
34
|
+
The ActiveJob adapter must be set to `:back_run`. This can be done in `config/application.rb`.
|
35
|
+
Add the Google Pub Sub project id and the path to the credentials file.
|
36
|
+
```ruby
|
37
|
+
class Application < Rails::Application
|
38
|
+
# ...
|
39
|
+
config.active_job.queue_adapter = :back_run
|
40
|
+
config.back_run = {
|
41
|
+
project_id: 'project_id',
|
42
|
+
credentials_path: 'path/to/credentials.json'
|
43
|
+
}
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### Job Creation
|
48
|
+
Create a job to be processed asynchronously:
|
49
|
+
```ruby
|
50
|
+
rails g job Example
|
51
|
+
```
|
52
|
+
This will generate automatically the following file in `app/jobs/example_job.rb`
|
53
|
+
```ruby
|
54
|
+
class ExampleJob < ApplicationJob
|
55
|
+
queue_as :default
|
56
|
+
|
57
|
+
def perform(*args)
|
58
|
+
# Do something later
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Jobs can be added to the job queue from anywhere. We can add a job to the queue by:
|
64
|
+
```ruby
|
65
|
+
ExampleJob.perform_later(args)
|
66
|
+
```
|
67
|
+
This arguments are serialized using `to_json` method and deserialized with `JSON.parse`. So you should use primitive objects as arguments.
|
68
|
+
|
69
|
+
### Worker configuration
|
70
|
+
In order to start consuming the Google Pub Sub topics, you should start a worker by running the following command:
|
71
|
+
```
|
72
|
+
bundle exec back_run -q queue-1 queue-2 -c 2
|
73
|
+
```
|
74
|
+
|
75
|
+
You can specify the queues that the worker should listen to (in this case queue-1 and queue-2) and the number of threads that the worker will use to execute the jobs.
|
76
|
+
|
77
|
+
### Metrics
|
78
|
+
The system provides some information about the jobs performed (the success ones). You should subscribe to the `back_run.event_executed' event using `ActiveSupport::Notification`
|
79
|
+
```ruby
|
80
|
+
ActiveSupport::Notifications.subscribe 'back_run.event_executed' do |*args|
|
81
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
82
|
+
# Do something with the event
|
83
|
+
end
|
84
|
+
```
|
85
|
+
This is an example of the event payload:
|
86
|
+
```
|
87
|
+
{
|
88
|
+
duration_seconds: 10,
|
89
|
+
job: 'ExampleJob'
|
90
|
+
}
|
91
|
+
```
|
92
|
+
|
93
|
+
## Development
|
94
|
+
|
95
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
96
|
+
|
97
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
98
|
+
|
99
|
+
## Contributing
|
100
|
+
|
101
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alanhala/back_run. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
102
|
+
|
103
|
+
## License
|
104
|
+
|
105
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
106
|
+
|
107
|
+
## Code of Conduct
|
108
|
+
|
109
|
+
Everyone interacting in the BackRun project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alanhala/back_run/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/back_run.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'back_run/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'back_run'
|
7
|
+
spec.version = BackRun::VERSION
|
8
|
+
spec.authors = ['Alan Halatian']
|
9
|
+
spec.email = ['alanhalatian@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Background job system to use with Rails based on Google Pub Sub'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.executables << 'back_run'
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
24
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.7'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
spec.add_development_dependency 'rspec-mocks', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.73.0'
|
29
|
+
|
30
|
+
spec.add_dependency 'activesupport', '~> 5.2'
|
31
|
+
spec.add_dependency 'concurrent-ruby', '~> 1.1'
|
32
|
+
spec.add_dependency 'google-cloud-pubsub', '~> 0.37'
|
33
|
+
spec.add_dependency 'thor', '~> 0.20'
|
34
|
+
end
|
data/bin/back_run
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'thor'
|
3
|
+
require 'rails'
|
4
|
+
require File.expand_path('./config/environment.rb')
|
5
|
+
require_relative '../lib/back_run/worker.rb'
|
6
|
+
|
7
|
+
module BackRun
|
8
|
+
class Cli < Thor
|
9
|
+
default_task :start
|
10
|
+
desc 'start', 'Starts BackRun'
|
11
|
+
method_option(
|
12
|
+
:concurrency, aliases: '-c', type: :numeric, desc: 'Threads per worker', default: 2
|
13
|
+
)
|
14
|
+
method_option :queues, aliases: '-q', type: :array, desc: 'Queues to process ', required: true
|
15
|
+
|
16
|
+
def start
|
17
|
+
BackRun.logger.info(
|
18
|
+
"Starting worker with #{options[:concurrency]} threads, listening to #{options[:queues]}"
|
19
|
+
)
|
20
|
+
BackRun::Worker.new(BackRun.pubsub, options[:queues], options[:concurrency]).start_listening!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
::Rails.application.eager_load!
|
26
|
+
BackRun::Cli.start
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'back_run'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/back_run.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'back_run/version'
|
3
|
+
require_relative './back_run/adapter'
|
4
|
+
require_relative './back_run/pubsub'
|
5
|
+
|
6
|
+
module BackRun
|
7
|
+
def self.logger
|
8
|
+
@logger ||= Logger.new(STDOUT)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.logger=(logger)
|
12
|
+
@logger = logger
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.pubsub
|
16
|
+
@pubsub ||= Pubsub.new
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative 'pubsub'
|
2
|
+
require_relative 'job'
|
3
|
+
|
4
|
+
module BackRun
|
5
|
+
class Adapter
|
6
|
+
def enqueue(job)
|
7
|
+
BackRun.pubsub.publish(Job.new_from_active_job(job))
|
8
|
+
end
|
9
|
+
|
10
|
+
def enqueue_at(job, _timestamp)
|
11
|
+
enqueue(job)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ActiveJob
|
17
|
+
module QueueAdapters
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ActiveJob::QueueAdapters::BackRunAdapter = BackRun::Adapter
|
data/lib/back_run/job.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
require 'active_support/core_ext/numeric/time'
|
3
|
+
|
4
|
+
module BackRun
|
5
|
+
class Job
|
6
|
+
# This is a Google Pubsub restriction. The ack deadline of a message can not be modified more
|
7
|
+
# than 600 seconds
|
8
|
+
MAX_REMAINING_SECONDS = 600
|
9
|
+
MINUTES_PER_RETRY = 5.minutes
|
10
|
+
attr_reader :queue_name, :klass, :duration_seconds
|
11
|
+
|
12
|
+
def initialize(klass, json_args, queue_name, scheduled_at, retries)
|
13
|
+
@klass = klass
|
14
|
+
@args = json_args
|
15
|
+
@queue_name = queue_name
|
16
|
+
@scheduled_at = scheduled_at
|
17
|
+
@retries = retries
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.new_from_active_job(job)
|
21
|
+
new(job.class.to_s, job.arguments.to_json, job.queue_name, job.scheduled_at, 0)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.from_json(json)
|
25
|
+
attributes = JSON.parse(json)
|
26
|
+
new(
|
27
|
+
attributes['class'], attributes['args'], attributes['queue_name'],
|
28
|
+
attributes['scheduled_at'], attributes['retries']
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_json(*_args)
|
33
|
+
{
|
34
|
+
class: @klass, args: @args, queue_name: @queue_name, scheduled_at: @scheduled_at.to_f,
|
35
|
+
retries: @retries
|
36
|
+
}.to_json
|
37
|
+
end
|
38
|
+
|
39
|
+
def perform(worker)
|
40
|
+
register_duration { @klass.constantize.new.perform(*JSON.parse(@args)) }
|
41
|
+
BackRun.logger.info("Finished processing the job: #{@klass}")
|
42
|
+
rescue StandardError => e
|
43
|
+
BackRun.logger.error("Failed processing the job: #{e.message}")
|
44
|
+
handle_job_failure(worker)
|
45
|
+
end
|
46
|
+
|
47
|
+
def should_run_now?
|
48
|
+
@scheduled_at.nil? || @scheduled_at <= Time.now.to_f
|
49
|
+
end
|
50
|
+
|
51
|
+
def remaining_seconds_to_run
|
52
|
+
[((Time.at(@scheduled_at) - Time.now) / 1.second).round, MAX_REMAINING_SECONDS].min
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def handle_job_failure(worker)
|
58
|
+
@retries += 1
|
59
|
+
if @retries <= 2
|
60
|
+
@scheduled_at = (Time.now + MINUTES_PER_RETRY).to_f
|
61
|
+
worker.retry_job(self)
|
62
|
+
else
|
63
|
+
worker.kill_job(self)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def register_duration
|
68
|
+
start_time = Time.now
|
69
|
+
yield
|
70
|
+
end_time = Time.now
|
71
|
+
@duration_seconds = (end_time - start_time) / 1.second
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module BackRun
|
2
|
+
class MetricsCollector
|
3
|
+
EVENT_NAME = 'back_run.event_executed'.freeze
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def job_executed(job)
|
7
|
+
ActiveSupport::Notifications.instrument(
|
8
|
+
EVENT_NAME, duration_seconds: job.duration_seconds, job: job.klass
|
9
|
+
)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'google/cloud/pubsub'
|
2
|
+
require_relative 'job'
|
3
|
+
|
4
|
+
module BackRun
|
5
|
+
class Pubsub
|
6
|
+
def initialize
|
7
|
+
@pubsub = Google::Cloud::Pubsub.new(
|
8
|
+
project_id: Rails.application.config.back_run[:project_id],
|
9
|
+
credentials: Rails.application.config.back_run[:credentials_path]
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def publish(job, topic = nil)
|
14
|
+
topic_name = topic || job.queue_name
|
15
|
+
topic = fetch_topic(topic_name)
|
16
|
+
topic.publish(job.to_json)
|
17
|
+
end
|
18
|
+
|
19
|
+
# The gem is using Concurrent::CachedThreadPool which overrides the max_threads, so it's
|
20
|
+
# ignoring the stream configuration used in the listen method. It may cause that lot's of
|
21
|
+
# threads are created when the traffic increases.
|
22
|
+
# This PR https://github.com/googleapis/google-cloud-ruby/pull/3682 fixes that but it's not
|
23
|
+
# released yet.
|
24
|
+
def subscribe(worker)
|
25
|
+
worker.queues.each do |queue|
|
26
|
+
subscription = subscription_for(queue)
|
27
|
+
subscriber = subscription.listen do |message|
|
28
|
+
job = Job.from_json(message.data)
|
29
|
+
worker.message_received(job, ack_callback(job, message), modify_ack_callback(message))
|
30
|
+
end
|
31
|
+
subscriber.on_error { |error| BackRun.logger.error(error) }
|
32
|
+
subscriber.start
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def kill_job(job)
|
37
|
+
BackRun.logger.info("Moving to morgue queue #{job.klass}")
|
38
|
+
publish(job, 'morgue')
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def subscription_for(queue)
|
44
|
+
topic = fetch_topic(queue)
|
45
|
+
topic.subscription("worker_#{queue}") || topic.subscribe("worker_#{queue}")
|
46
|
+
end
|
47
|
+
|
48
|
+
def ack_callback(job, message)
|
49
|
+
proc do
|
50
|
+
BackRun.logger.info("Message received from queue: #{job.queue_name}")
|
51
|
+
message.ack!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def modify_ack_callback(message)
|
56
|
+
proc do |seconds|
|
57
|
+
BackRun.logger.info("Running job in #{seconds} seconds")
|
58
|
+
message.modify_ack_deadline!(seconds)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def fetch_topic(topic_name)
|
63
|
+
@pubsub.topic(topic_name) || @pubsub.create_topic(topic_name)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'concurrent-ruby'
|
2
|
+
require_relative 'pubsub'
|
3
|
+
require_relative 'metrics_collector'
|
4
|
+
|
5
|
+
module BackRun
|
6
|
+
class Worker
|
7
|
+
attr_reader :queues
|
8
|
+
|
9
|
+
def initialize(pubsub, queues, threads, pool = nil)
|
10
|
+
@queues = queues
|
11
|
+
@pubsub = pubsub
|
12
|
+
@pool = pool || Concurrent::ThreadPoolExecutor.new(
|
13
|
+
min_threads: 1, max_threads: threads, max_queue: 1
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
def start_listening!
|
18
|
+
@pubsub.subscribe(self)
|
19
|
+
sleep
|
20
|
+
end
|
21
|
+
|
22
|
+
def message_received(job, ack, modify_ack_deadline)
|
23
|
+
if job.should_run_now?
|
24
|
+
ack.call
|
25
|
+
run_job(job)
|
26
|
+
else
|
27
|
+
remaining_seconds = job.remaining_seconds_to_run
|
28
|
+
modify_ack_deadline.call(remaining_seconds)
|
29
|
+
end
|
30
|
+
rescue Concurrent::RejectedExecutionError
|
31
|
+
BackRun.logger.info('Thread pool busy. Republishing the job')
|
32
|
+
retry_job(job)
|
33
|
+
end
|
34
|
+
|
35
|
+
def retry_job(job)
|
36
|
+
@pubsub.publish(job)
|
37
|
+
end
|
38
|
+
|
39
|
+
def kill_job(job)
|
40
|
+
@pubsub.kill_job(job)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def run_job(job)
|
46
|
+
@pool.post do
|
47
|
+
job.perform(self)
|
48
|
+
MetricsCollector.job_executed(job)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: back_run
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alan Halatian
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry-byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-mocks
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.73.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.73.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '5.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '5.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: concurrent-ruby
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.1'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.1'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: google-cloud-pubsub
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0.37'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0.37'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: thor
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.20'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.20'
|
153
|
+
description:
|
154
|
+
email:
|
155
|
+
- alanhalatian@gmail.com
|
156
|
+
executables:
|
157
|
+
- back_run
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- ".travis.yml"
|
165
|
+
- CODE_OF_CONDUCT.md
|
166
|
+
- Gemfile
|
167
|
+
- Gemfile.lock
|
168
|
+
- LICENSE.txt
|
169
|
+
- README.md
|
170
|
+
- Rakefile
|
171
|
+
- back_run.gemspec
|
172
|
+
- bin/back_run
|
173
|
+
- bin/console
|
174
|
+
- bin/setup
|
175
|
+
- lib/back_run.rb
|
176
|
+
- lib/back_run/adapter.rb
|
177
|
+
- lib/back_run/job.rb
|
178
|
+
- lib/back_run/metrics_collector.rb
|
179
|
+
- lib/back_run/pubsub.rb
|
180
|
+
- lib/back_run/version.rb
|
181
|
+
- lib/back_run/worker.rb
|
182
|
+
homepage:
|
183
|
+
licenses:
|
184
|
+
- MIT
|
185
|
+
metadata: {}
|
186
|
+
post_install_message:
|
187
|
+
rdoc_options: []
|
188
|
+
require_paths:
|
189
|
+
- lib
|
190
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
|
+
requirements:
|
197
|
+
- - ">="
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
version: '0'
|
200
|
+
requirements: []
|
201
|
+
rubyforge_project:
|
202
|
+
rubygems_version: 2.6.14.3
|
203
|
+
signing_key:
|
204
|
+
specification_version: 4
|
205
|
+
summary: Background job system to use with Rails based on Google Pub Sub
|
206
|
+
test_files: []
|