stormtroopers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/stormtroopers.rb +0 -0
- data/config/stormtroopers.yml +11 -0
- data/lib/stormtroopers/army.rb +30 -0
- data/lib/stormtroopers/factory/delayed_job.rb +10 -0
- data/lib/stormtroopers/factory/dummy.rb +8 -0
- data/lib/stormtroopers/factory.rb +13 -0
- data/lib/stormtroopers/manager.rb +56 -0
- data/lib/stormtroopers/trooper/delayed_job.rb +32 -0
- data/lib/stormtroopers/trooper/dummy.rb +20 -0
- data/lib/stormtroopers/trooper.rb +37 -0
- data/lib/stormtroopers/version.rb +3 -0
- data/lib/stormtroopers.rb +12 -0
- data/stormtroopers.gemspec +21 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andre Meij
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Stormtroopers
|
2
|
+
|
3
|
+
Stormtroopers is a jruby execution environment for delayed jobs
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'stormtroopers'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install stormtroopers
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
configure using a config file 'stormtroopers.yml' and run with `bundle exec stormtroopers`
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
File without changes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Stormtroopers
|
2
|
+
class Army
|
3
|
+
attr_reader :factory, :threads, :max_threads
|
4
|
+
|
5
|
+
def initialize(config)
|
6
|
+
@factory = "stormtroopers/#{config[:factory].delete(:type)}_factory".camelize.constantize.new(config[:factory])
|
7
|
+
@max_threads = config[:max_threads] || 1
|
8
|
+
@threads = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def manage
|
12
|
+
cleanup
|
13
|
+
if threads.count < max_threads
|
14
|
+
if trooper = factory.produce
|
15
|
+
threads << Thread.new { trooper.run }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def finish
|
21
|
+
threads.each(&:join)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def cleanup
|
27
|
+
threads.reject!{ |thread| !thread.alive? }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'active_support/core_ext/hash'
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
4
|
+
|
5
|
+
module Stormtroopers
|
6
|
+
class Manager
|
7
|
+
include Singleton
|
8
|
+
# This class is dependant on rails and active support
|
9
|
+
|
10
|
+
def manage
|
11
|
+
logger.info "Starting"
|
12
|
+
loop do
|
13
|
+
armies.each(&:manage)
|
14
|
+
sleep 0.1
|
15
|
+
end
|
16
|
+
rescue Interrupt => e
|
17
|
+
logger.info "Stopping, waiting for running jobs to complete"
|
18
|
+
armies.each(&:finish)
|
19
|
+
logger.info "Stopped, all running jobs completed"
|
20
|
+
end
|
21
|
+
|
22
|
+
def armies
|
23
|
+
@armies ||= config[:armies].map do |army_config|
|
24
|
+
Army.new(army_config)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def config
|
29
|
+
@config ||= HashWithIndifferentAccess.new(YAML.load_file(config_file))
|
30
|
+
end
|
31
|
+
|
32
|
+
def config_file
|
33
|
+
if defined?(Rails)
|
34
|
+
"#{Rails.root}/config/stormtroopers.yml"
|
35
|
+
else
|
36
|
+
"#{File.expand_path('../../../config', __FILE__)}/stormtroopers.yml"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def logger
|
41
|
+
if defined?(Rails)
|
42
|
+
Rails.logger
|
43
|
+
else
|
44
|
+
@logger ||= Logger.new(STDOUT)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
class << self
|
51
|
+
def logger(*args)
|
52
|
+
instance.logger(*args)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Stormtroopers
|
2
|
+
class DelayedJobTrooper < Trooper
|
3
|
+
attr_reader :job
|
4
|
+
|
5
|
+
def initialize(job)
|
6
|
+
@job = job
|
7
|
+
end
|
8
|
+
|
9
|
+
def run
|
10
|
+
job.invoke_job
|
11
|
+
job.destroy
|
12
|
+
rescue => error
|
13
|
+
job.last_error = "#{error.message}\n#{error.backtrace.join("\n")}"
|
14
|
+
logger.info "#{job.name} failed with #{error.class.name}: #{error.message} - #{job.attempts} failed attempts"
|
15
|
+
reschedule
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def reschedule
|
21
|
+
if (job.attempts += 1) < max_attempts(job)
|
22
|
+
time ||= job.reschedule_at
|
23
|
+
job.run_at = time
|
24
|
+
job.unlock
|
25
|
+
job.save!
|
26
|
+
else
|
27
|
+
logger.error "PERMANENTLY removing #{job.name} because of #{job.attempts} consecutive failures.", Logger::INFO
|
28
|
+
job.hook(:failure)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Stormtroopers
|
2
|
+
class DummyTrooper < Trooper
|
3
|
+
|
4
|
+
def run
|
5
|
+
logger.debug "#{name}: Dummy job started, sleeping for #{sleep_duration}"
|
6
|
+
sleep(sleep_duration)
|
7
|
+
logger.debug "#{name}: Dummy job completed, w00t"
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def name
|
13
|
+
parameters[:name] || 'noname'
|
14
|
+
end
|
15
|
+
|
16
|
+
def sleep_duration
|
17
|
+
parameters[:sleep_duration] || 1
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Stormtroopers
|
2
|
+
class Trooper
|
3
|
+
attr_reader :task, :parameters
|
4
|
+
|
5
|
+
def initialize(parameters = {}, &block)
|
6
|
+
@parameters = parameters
|
7
|
+
@task = block
|
8
|
+
end
|
9
|
+
|
10
|
+
def before_run
|
11
|
+
# Empty hook for overriding
|
12
|
+
end
|
13
|
+
|
14
|
+
def after_run
|
15
|
+
# Empty hook for overriding
|
16
|
+
end
|
17
|
+
|
18
|
+
def exception(exception)
|
19
|
+
raise
|
20
|
+
# Empty hook for overriding
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
before_execution
|
25
|
+
task.call
|
26
|
+
after_execution
|
27
|
+
rescue => e
|
28
|
+
exception(e)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def logger
|
34
|
+
Manager.logger
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "stormtroopers/version"
|
2
|
+
require "stormtroopers/trooper"
|
3
|
+
require "stormtroopers/manager"
|
4
|
+
require "stormtroopers/factory"
|
5
|
+
require "stormtroopers/army"
|
6
|
+
|
7
|
+
Dir['./lib/stormtroopers/factory/*.rb'].each{ |f| require f }
|
8
|
+
Dir['./lib/stormtroopers/trooper/*.rb'].each{ |f| require f }
|
9
|
+
|
10
|
+
module Stormtroopers
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stormtroopers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "stormtroopers"
|
8
|
+
gem.version = Stormtroopers::VERSION
|
9
|
+
gem.authors = ["Andre Meij", "Mark Kremer"]
|
10
|
+
gem.email = ["andre@socialreferral.com"]
|
11
|
+
gem.description = %q{Stormtroopers is a jruby execution environment for delayed jobs }
|
12
|
+
gem.summary = %q{Execute delayed jobs in a threaded jruby environment}
|
13
|
+
gem.homepage = "http://github.com/socialreferral/stormtroopers"
|
14
|
+
|
15
|
+
gem.add_dependency('activesupport', '>= 3.2.0')
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stormtroopers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andre Meij
|
9
|
+
- Mark Kremer
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 3.2.0
|
31
|
+
description: ! 'Stormtroopers is a jruby execution environment for delayed jobs '
|
32
|
+
email:
|
33
|
+
- andre@socialreferral.com
|
34
|
+
executables:
|
35
|
+
- stormtroopers.rb
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- bin/stormtroopers.rb
|
45
|
+
- config/stormtroopers.yml
|
46
|
+
- lib/stormtroopers.rb
|
47
|
+
- lib/stormtroopers/army.rb
|
48
|
+
- lib/stormtroopers/factory.rb
|
49
|
+
- lib/stormtroopers/factory/delayed_job.rb
|
50
|
+
- lib/stormtroopers/factory/dummy.rb
|
51
|
+
- lib/stormtroopers/manager.rb
|
52
|
+
- lib/stormtroopers/trooper.rb
|
53
|
+
- lib/stormtroopers/trooper/delayed_job.rb
|
54
|
+
- lib/stormtroopers/trooper/dummy.rb
|
55
|
+
- lib/stormtroopers/version.rb
|
56
|
+
- stormtroopers.gemspec
|
57
|
+
homepage: http://github.com/socialreferral/stormtroopers
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.23
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Execute delayed jobs in a threaded jruby environment
|
81
|
+
test_files: []
|
82
|
+
has_rdoc:
|