success_repeater 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/lib/success_repeater/version.rb +3 -0
- data/lib/success_repeater.rb +56 -0
- data/success_repeater.gemspec +23 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 luigi.sk
|
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,36 @@
|
|
1
|
+
|
2
|
+
# SuccessRepeater
|
3
|
+
|
4
|
+
Rerun yield code if exception is raised.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'success_repeater'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install success_repeater
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
require 'success_repeater'
|
23
|
+
transaction = SuccessRepeater::Base.new(:max_seconds_run=>10.minutes.to_i,
|
24
|
+
:sleep_time=>30.seconds.to_i)
|
25
|
+
transaction.run do
|
26
|
+
# your code
|
27
|
+
puts "run your code. If error was raised then process sleeps to 30seconds a run your code again."
|
28
|
+
end
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "success_repeater/version"
|
2
|
+
|
3
|
+
module SuccessRepeater
|
4
|
+
class Base
|
5
|
+
attr_accessor :max_seconds_run, :sleep_time
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
options = options.reverse_merge(
|
9
|
+
:max_seconds_run => 20.hours.to_i,
|
10
|
+
:sleep_time => 10.minutes.to_i
|
11
|
+
)
|
12
|
+
@max_hours_run = options[:max_seconds_run]
|
13
|
+
@sleep_time = options[:sleep_time]
|
14
|
+
end
|
15
|
+
|
16
|
+
def run(&block)
|
17
|
+
done = false
|
18
|
+
start_at = DateTime.now
|
19
|
+
error = nil
|
20
|
+
while !done
|
21
|
+
begin
|
22
|
+
if defined?(ActiveRecord::Base.transaction)
|
23
|
+
# do it in transaction
|
24
|
+
ActiveRecord::Base.transaction do
|
25
|
+
block.call
|
26
|
+
end
|
27
|
+
else
|
28
|
+
block.call
|
29
|
+
end
|
30
|
+
done = true
|
31
|
+
rescue => e
|
32
|
+
on_failure(e)
|
33
|
+
# prevent blocking cron next day job
|
34
|
+
seconds_run = ((DateTime.now - start_at) * 24 * 60 *60).to_i
|
35
|
+
if seconds_run > @max_seconds_run
|
36
|
+
done = true
|
37
|
+
error = e
|
38
|
+
else
|
39
|
+
done = false
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
unless error.nil?
|
46
|
+
on_failure(error)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def on_failure(e)
|
51
|
+
ExceptionNotifier::Notifier.background_exception_notification(e) if defined?(ExceptionNotifier::Notifier.background_exception_notification)
|
52
|
+
Rails.logger.error("Succ repeater error #{e.backtrace.join("\n")}")
|
53
|
+
sleep(sleep_time)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
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 'success_repeater/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "success_repeater"
|
8
|
+
spec.version = SuccessRepeater::VERSION
|
9
|
+
spec.authors = ["luigi.sk"]
|
10
|
+
spec.email = ["luigi.sk@gmail.com"]
|
11
|
+
spec.description = %q{repeat yield command in transaction until is executed successfully}
|
12
|
+
spec.summary = %q{devel}
|
13
|
+
spec.homepage = "https://github.com/luigi-sk/success-repeater"
|
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,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: success_repeater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- luigi.sk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-06-04 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "1.3"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: repeat yield command in transaction until is executed successfully
|
38
|
+
email:
|
39
|
+
- luigi.sk@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/success_repeater.rb
|
53
|
+
- lib/success_repeater/version.rb
|
54
|
+
- success_repeater.gemspec
|
55
|
+
homepage: https://github.com/luigi-sk/success-repeater
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
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
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.24
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: devel
|
82
|
+
test_files: []
|
83
|
+
|
84
|
+
has_rdoc:
|