resque_optimized_retry 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 +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/lib/resque_optimized_retry/version.rb +3 -0
- data/lib/resque_optimized_retry.rb +55 -0
- data/resque_optimized_retry.gemspec +23 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Rodrigo Fontoura
|
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,48 @@
|
|
1
|
+
# ResqueOptimizedRetry
|
2
|
+
|
3
|
+
Um problema que sempre tive ao trabalhar com sistema grande onde tem muita conexão com o Redis é na hora de dar retry nos jobs que falharam. Normalmente perde-se jobs e/ou gera muita slowquery. Com slowquery fica tudo que utiliza o Redis muito lento, inclusive o retry e a execução dos outros jobs. Com essa gem você irá cosneguir dar retry e clear nos seus jobs que falharam sem perder nenhum job e gerando o mínimo de slowquery possível (É sempre bom lembrar que as slowquery dependem muito de como seu sistema utiliza o Redis, por isso não tem como afirmar com 100% de certeza que não vai haver nenhuma slowquery ao executar retry e/ou clear).
|
4
|
+
|
5
|
+
Em um teste realizado com 345k de jobs falhados obteve 2 slowquery, demorou cerca de 8 minutos e não perdeu nenhum job.
|
6
|
+
Em um novo teste realizado com 56k de jobs utilizando a gem resque-cleaner obteve 20k+ slowquery, demorou mais de 1 hora e não perdeu nenhum job.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'resque_optimized_retry'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install resque_optimized_retry
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Retry and Clear
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
ResqueOptimizedRetry::Jobs.new.retry_and_clear
|
28
|
+
```
|
29
|
+
|
30
|
+
### Retry
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
ResqueOptimizedRetry::Jobs.new.retry
|
34
|
+
```
|
35
|
+
|
36
|
+
### Clear
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
ResqueOptimizedRetry::Jobs.new.clear
|
40
|
+
```
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "resque_optimized_retry/version"
|
2
|
+
|
3
|
+
module ResqueOptimizedRetry
|
4
|
+
class Jobs
|
5
|
+
def initialize options = {}
|
6
|
+
@redis = Resque.redis
|
7
|
+
@retried_count = 0
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def retry_and_clear
|
12
|
+
while can_retry?
|
13
|
+
failed_job = redis.lpop("failed")
|
14
|
+
break if failed_job.blank?
|
15
|
+
job = JSON.parse failed_job
|
16
|
+
Resque::Job.create job['queue'], job['payload']['class'], *job['payload']['args']
|
17
|
+
@retried_count += 1
|
18
|
+
end
|
19
|
+
@retried_count
|
20
|
+
end
|
21
|
+
|
22
|
+
def retry
|
23
|
+
while can_retry?
|
24
|
+
failed_job = redis.lrange("failed", @retried_count, @retried_count + 1)
|
25
|
+
break if failed_job.blank?
|
26
|
+
job = JSON.parse failed_job[0]
|
27
|
+
Resque::Job.create job['queue'], job['payload']['class'], *job['payload']['args']
|
28
|
+
@retried_count += 1
|
29
|
+
end
|
30
|
+
@retried_count
|
31
|
+
end
|
32
|
+
|
33
|
+
def clear
|
34
|
+
Resque::Failure.clear
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def redis
|
40
|
+
@redis
|
41
|
+
end
|
42
|
+
|
43
|
+
def can_retry?
|
44
|
+
not_crossed_the_limit? || has_failed_jobs?
|
45
|
+
end
|
46
|
+
|
47
|
+
def not_crossed_the_limit?
|
48
|
+
!(@options[:limit].present? && @options[:limit].to_i >= @retried_count && has_failed_jobs?)
|
49
|
+
end
|
50
|
+
|
51
|
+
def has_failed_jobs?
|
52
|
+
redis.llen("failed") > 0
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'resque_optimized_retry/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "resque_optimized_retry"
|
8
|
+
spec.version = ResqueOptimizedRetry::VERSION
|
9
|
+
spec.authors = ["Rodrigo Fontoura"]
|
10
|
+
spec.email = ["rodrigo.fontoura@dito.com.br"]
|
11
|
+
spec.description = %q{Gem para dar retry de forma otimizada nos jobs que falharam do Resque}
|
12
|
+
spec.summary = %q{Gem para dar retry de forma otimizada nos jobs que falharam do Resque}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque_optimized_retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rodrigo Fontoura
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Gem para dar retry de forma otimizada nos jobs que falharam do Resque
|
47
|
+
email:
|
48
|
+
- rodrigo.fontoura@dito.com.br
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/resque_optimized_retry.rb
|
59
|
+
- lib/resque_optimized_retry/version.rb
|
60
|
+
- resque_optimized_retry.gemspec
|
61
|
+
homepage: ''
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.21
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Gem para dar retry de forma otimizada nos jobs que falharam do Resque
|
86
|
+
test_files: []
|