round_robin 1.0.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.
- data/LICENSE +21 -0
- data/README.md +40 -0
- data/lib/round_robin.rb +13 -0
- data/lib/round_robin/job.rb +26 -0
- data/lib/round_robin/tasks.rb +22 -0
- data/lib/round_robin/worker.rb +18 -0
- data/lib/tasks/round_robin.rake +2 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) Jan Andersson
|
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.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
Round Robin
|
2
|
+
===========
|
3
|
+
|
4
|
+
A library for running background jobs over and over again in a round robin fashion.
|
5
|
+
|
6
|
+
Consists of two parts:
|
7
|
+
|
8
|
+
* A rake task to start workers
|
9
|
+
* A ruby library for adding, removing, querying and processing jobs
|
10
|
+
|
11
|
+
Overview
|
12
|
+
--------
|
13
|
+
|
14
|
+
A Round Robin Job may be any Ruby class which responds to the method `perform`.
|
15
|
+
|
16
|
+
``` ruby
|
17
|
+
class SearchJob
|
18
|
+
def self.perform(search_id)
|
19
|
+
search = Search.find(search_id)
|
20
|
+
# perform the search
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
RoundRobin.add(SearchJob, 1)
|
25
|
+
RoundRobin.add(SearchJob, 2)
|
26
|
+
RoundRobin.add(SearchJob, 3)
|
27
|
+
```
|
28
|
+
|
29
|
+
Now fire up two workers with:
|
30
|
+
|
31
|
+
COUNT=2 rake round_robin:workers
|
32
|
+
|
33
|
+
The two workers workers will take one job at a time each and process them in order.
|
34
|
+
|
35
|
+
The workers will be persisted and will be kept even after a restart of the
|
36
|
+
server. Typically in a Rails project a `RoundRobin.add` can be called in an
|
37
|
+
`after_create` callback and similarly a `RoundRobin.remove` may be called
|
38
|
+
`after_destroy`.
|
39
|
+
|
40
|
+
Round Robin is inspired by Resque and DelayedJob.
|
data/lib/round_robin.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'round_robin/worker'
|
2
|
+
require 'round_robin/job'
|
3
|
+
|
4
|
+
module RoundRobin
|
5
|
+
def self.add(klass, *args)
|
6
|
+
json = {:class => klass.to_s, :args => args}.to_json
|
7
|
+
RoundRobin::Job.create(:handler => json)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.clear
|
11
|
+
RoundRobin::Job.destroy_all
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module RoundRobin
|
4
|
+
class Job < ActiveRecord::Base
|
5
|
+
self.table_name = 'round_robin_jobs'
|
6
|
+
|
7
|
+
def invoke_job
|
8
|
+
begin
|
9
|
+
update_attributes(:started_at => Time.now, :finished_at => nil)
|
10
|
+
klass = Module.const_get(parsed_handler["class"])
|
11
|
+
args = parsed_handler["args"]
|
12
|
+
klass.perform(*args)
|
13
|
+
rescue Exception => e
|
14
|
+
|
15
|
+
ensure
|
16
|
+
update_attribute(:finished_at, Time.now)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
private
|
22
|
+
def parsed_handler
|
23
|
+
@parsed_handler ||= JSON.parse(handler)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'round_robin'
|
2
|
+
|
3
|
+
namespace :round_robin do
|
4
|
+
|
5
|
+
desc "Start a worker"
|
6
|
+
task :work => :environment do
|
7
|
+
worker = RoundRobin::Worker.new
|
8
|
+
Process.daemon(true)
|
9
|
+
worker.work
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Start multiple workers"
|
13
|
+
task :workers => :environment do
|
14
|
+
threads = []
|
15
|
+
ENV['COUNT'].to_i.times do
|
16
|
+
threads << Thread.new do
|
17
|
+
system "rake round_robin:work"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
threads.each { |thread| thread.join }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RoundRobin
|
2
|
+
class Worker
|
3
|
+
attr_accessor :shutdown
|
4
|
+
|
5
|
+
def work
|
6
|
+
begin
|
7
|
+
RoundRobin::Job.order("started_at").limit(1).first.try(:invoke_job)
|
8
|
+
sleep sleep_time
|
9
|
+
end while not shutdown
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def sleep_time
|
15
|
+
1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: round_robin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jan Andersson
|
9
|
+
- Kristian Hellquist
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-03-01 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &70225900394860 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70225900394860
|
26
|
+
description: ''
|
27
|
+
email:
|
28
|
+
- jan.andersson@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/round_robin/job.rb
|
34
|
+
- lib/round_robin/tasks.rb
|
35
|
+
- lib/round_robin/worker.rb
|
36
|
+
- lib/round_robin.rb
|
37
|
+
- lib/tasks/round_robin.rake
|
38
|
+
- LICENSE
|
39
|
+
- README.md
|
40
|
+
homepage: http://github.com/janne/round_robin
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Library and tasks to handle round robin style workers
|
64
|
+
test_files: []
|