nectarine 0.1.0
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +83 -0
- data/Rakefile +27 -0
- data/lib/nectarine.rb +32 -0
- data/lib/nectarine/good_job.rb +9 -0
- data/lib/nectarine/railtie.rb +4 -0
- data/lib/nectarine/version.rb +3 -0
- data/lib/tasks/nectarine_tasks.rake +4 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 10f4ed6346611714e8ceb3f08bb5cb4b4c5f34a94be6dc7eac181e6eab21bbcc
|
4
|
+
data.tar.gz: '0799a978010ccdbe25aa9b1ce0c1c09a6e2855a516dd81a212836199b194f0c3'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 541cb106a4f26e3f5033ef6e918fcc23a0c9acfcea833fe8d0e8c2ace759ac8b389298c32cc6fa29255ab079f155de7ee03fa8f72066e8839741c41e9d0520ce
|
7
|
+
data.tar.gz: 1e64d7e9916044490a17f4db046f7d7807267cea27597bbd0c830d4ea49aafc3207f83eb5d585f1ce55cb46bf1ec856db28f7b307fa9d160e692b8168a8a0b6a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 n8
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Nectarine
|
2
|
+
|
3
|
+
*Parallel processing*
|
4
|
+
|
5
|
+
> a mode of computer operation in which a process is split into parts that execute simultaneously on different processors attached to the same computer.
|
6
|
+
|
7
|
+
I find myself constantly working on apps that need a mechanism to split an array of objects into multiple threads or CPUs to perform things on those objects, then return processing back to the main thread.
|
8
|
+
|
9
|
+
But it's still something I find surprisingly difficult to do in Ruby/Rails. There's definitely libraries that successfully help, but they all suffer from problems around versions of Ruby being used (green threads, native threads, interpreter locks) or your OS/environment (Windows/Mac/Linux).
|
10
|
+
|
11
|
+
It dawned on me though that we already have a really interesting place to do parallel processing and communication amongst multiple threads/CPUs that's fairly agnostic of environment AND has built in fault tolerance: our current Job queue.
|
12
|
+
|
13
|
+
Whether we're using Resque/Sidekiq/Delayed Job/etc. Why not send things there for parallel processing our main thread waits for execution?
|
14
|
+
|
15
|
+
So here's a proof of concept to do just that. It's called Nectarine.
|
16
|
+
|
17
|
+
Nectarine works over a collection of Active Record objects, sending each object to your Job system using Active Job, and executing a single method on that object. The main thread blocks until processing has completed on all the objects.
|
18
|
+
|
19
|
+
That's it. I'm using it now in an app ([FilmHope.com](https://filmhope.com/)) that takes a user's video, converts it into hundreds of static thumbnails, and then for each thumbnail, Nectarine jobs out processing on each image, calling a bunch of other APIs as needed. It's a super slow task, which is why having Nectarine handle those hundreds of thumbs in parallel is a nice workflow to have.
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
Once nectarine is installed, you can call it on your collections of Active Record objects.
|
24
|
+
|
25
|
+
Let's say a Snail model in our system has a super slow method like "start_racing". It's slow because start_racing calls external APIs, does a bunch of other magic, whatever. It's slow.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Snail.all.each(&:start_racing)
|
29
|
+
Rails.logger.debug("That took forever")
|
30
|
+
```
|
31
|
+
|
32
|
+
Would take forever.
|
33
|
+
|
34
|
+
Instead:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Snail.all.nectarine(:start_racing)
|
38
|
+
Rails.logger.debug("That took a lot less than forever")
|
39
|
+
```
|
40
|
+
|
41
|
+
That's it. In this example, the Rails.logger.debug line isn't executed until all the snails in Snail.all have executed their "start_racing" method.
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
Add this line to your application's Gemfile:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem 'nectarine'
|
48
|
+
```
|
49
|
+
|
50
|
+
And then execute:
|
51
|
+
```bash
|
52
|
+
$ bundle
|
53
|
+
```
|
54
|
+
|
55
|
+
Or install it yourself as:
|
56
|
+
```bash
|
57
|
+
$ gem install nectarine
|
58
|
+
```
|
59
|
+
|
60
|
+
## What's next.
|
61
|
+
|
62
|
+
There's a lot of gaps in this library right now.
|
63
|
+
|
64
|
+
A total Timeout period is currently hard coded at 15 minutes. That should at a very minium be configurable. There's other things like status checks of the jobs that just happen every 2 seconds.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
Timeout::timeout(15 * 60) do
|
68
|
+
while(!all_job_statuses_complete?(all_jobs)) do
|
69
|
+
sleep 2
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
It might be smarter to do some exponential backoff checking for their completion. There's also not very good error control or handling.
|
75
|
+
|
76
|
+
This also just uses whatever your default queue is, which is something that should probably be configurable.
|
77
|
+
|
78
|
+
So all feedback and pull requests are more than welcome!
|
79
|
+
|
80
|
+
|
81
|
+
## P.S.
|
82
|
+
|
83
|
+
If you need a some help building a software project, [**whether it's advice or just extra hands, please shout**](https://www.rockstarcoders.com/contact-us/). We've got a great crew at Rockstar Coders and we'd love to help.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Nectarine'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
data/lib/nectarine.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "nectarine/railtie"
|
2
|
+
require "nectarine/good_job"
|
3
|
+
|
4
|
+
module Nectarine
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
class ActiveRecord::Relation
|
9
|
+
def nectarine(method)
|
10
|
+
|
11
|
+
method_to_run = method.to_s
|
12
|
+
|
13
|
+
all_jobs = []
|
14
|
+
self.each do |item|
|
15
|
+
all_jobs << Nectarine::GoodJob.perform_later(item, method_to_run)
|
16
|
+
end
|
17
|
+
|
18
|
+
Timeout::timeout(15 * 60) do
|
19
|
+
while(!all_job_statuses_complete?(all_jobs)) do
|
20
|
+
sleep 2
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
return
|
25
|
+
end
|
26
|
+
|
27
|
+
def all_job_statuses_complete?(all_jobs)
|
28
|
+
done = all_jobs.select{|job| ActiveJob::Status.get(job).completed?}
|
29
|
+
done.size == all_jobs.size
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nectarine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- n8
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activejob-status
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.5
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.1.5
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Runs a method over a collection of Rails models. But does it in parallel
|
56
|
+
using Active Job.
|
57
|
+
email:
|
58
|
+
- nate.kontny@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- MIT-LICENSE
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- lib/nectarine.rb
|
67
|
+
- lib/nectarine/good_job.rb
|
68
|
+
- lib/nectarine/railtie.rb
|
69
|
+
- lib/nectarine/version.rb
|
70
|
+
- lib/tasks/nectarine_tasks.rake
|
71
|
+
homepage: https://github.com/n8/nectarine
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.7.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Runs a method over a collection of Rails models. But does it in parallel
|
95
|
+
using Active Job.
|
96
|
+
test_files: []
|