retryit 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +75 -0
  3. data/bin/retry +7 -0
  4. data/lib/retryit.rb +89 -0
  5. metadata +49 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be1aa72d40e7c8badd21382d2c3fd41ee6579b26
4
+ data.tar.gz: 2e9d14f94fec8bbf040b08c2fa462ed105ec6794
5
+ SHA512:
6
+ metadata.gz: 23ca9102779a73de3f60b3221021de7614d186305b0ec217f3ce85fde07b8774bc971f785d4b0d72d93075351f22539a19aa13d993cd94cea07b2094e5ed8124
7
+ data.tar.gz: dd75f23f180fe7aae37dbac236e7dbc05a01dcd62876af85e74e41344b7bfef7da2f53b105f9cd512a0a63cc5505f29482e0225ca80691a45671489fa842df6a
@@ -0,0 +1,75 @@
1
+ retryit - The command line retry tool
2
+ ------------------------------------------
3
+
4
+ Retry any shell command with exponential backoff or constant delay.
5
+
6
+ ### Instructions
7
+
8
+ Install:
9
+
10
+ `gem install retryit`
11
+
12
+ ### Usage
13
+
14
+ Help:
15
+
16
+ `retry -?`
17
+
18
+ Usage: retry [options] -e execute command
19
+ -h, -?, --help
20
+ -t, --tries=# Set max retries: Default 10
21
+ -s, --sleep=secs Constant sleep amount (seconds)
22
+ -m, --min=secs Exponenetial Backoff: minimum sleep amount (seconds): Default 0.3
23
+ -x, --max=secs Exponenetial Backoff: maximum sleep amount (seconds): Default 60
24
+
25
+ ### Examples
26
+
27
+ Test functionality:
28
+
29
+ `retry 'echo "y u no work"; false'`
30
+
31
+ y u no work
32
+ Before retry #1: sleeping 0.3 seconds
33
+ y u no work
34
+ Before retry #2: sleeping 0.6 seconds
35
+ y u no work
36
+ Before retry #3: sleeping 1.2 seconds
37
+ y u no work
38
+ Before retry #4: sleeping 2.4 seconds
39
+ y u no work
40
+ Before retry #5: sleeping 4.8 seconds
41
+ y u no work
42
+ Before retry #6: sleeping 9.6 seconds
43
+ y u no work
44
+ Before retry #7: sleeping 19.2 seconds
45
+ y u no work
46
+ Before retry #8: sleeping 38.4 seconds
47
+ y u no work
48
+ Before retry #9: sleeping 60.0 seconds
49
+ y u no work
50
+ Before retry #10: sleeping 60.0 seconds
51
+ y u no work
52
+
53
+ Limit retries:
54
+
55
+ `retry -t 4 -e 'echo "y u no work"; false'`
56
+
57
+ y u no work
58
+ Before retry #1: sleeping 0.3 seconds
59
+ y u no work
60
+ Before retry #2: sleeping 0.6 seconds
61
+ y u no work
62
+ Before retry #3: sleeping 1.2 seconds
63
+ y u no work
64
+ Before retry #4: sleeping 2.4 seconds
65
+ y u no work
66
+
67
+ Bad command:
68
+
69
+ `retry poop`
70
+
71
+ Command Failed: poop
72
+
73
+ ### License
74
+
75
+ Apache 2.0 - go nuts
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+
5
+ require File.expand_path("../../lib/retryit.rb", __FILE__)
6
+
7
+ RetryIt.new.run(ARGV)
@@ -0,0 +1,89 @@
1
+ require 'optparse'
2
+
3
+
4
+ class RetryIt
5
+
6
+ attr_accessor :max_retries, :min_sleep, :max_sleep, :constant_sleep
7
+
8
+ def initialize()
9
+ @max_tries = 10
10
+ @min_sleep = 0.3
11
+ @max_sleep = 60.0
12
+ @constant_sleep = nil
13
+ end
14
+
15
+ def load_options(args)
16
+ return if args.size < 1
17
+
18
+ optparser = OptionParser.new do |opts|
19
+ opts.banner = "Usage: retry [options] -e execute command"
20
+
21
+ opts.on("-h", "-?", "--help") do |v|
22
+ puts opts
23
+ exit
24
+ end
25
+
26
+ opts.on("-t#", "--tries=#", Integer, "Set max retries: Default 10") do |v|
27
+ @max_tries = v
28
+ end
29
+
30
+ opts.on("-s#", "--sleep=secs", Float, "Constant sleep amount (seconds)") do |v|
31
+ @constant_sleep = v
32
+ end
33
+
34
+ opts.on("-m#", "--min=secs", Float, "Exponenetial Backoff: minimum sleep amount (seconds): Default 0.3") do |v|
35
+ @min_sleep = v
36
+ end
37
+
38
+ opts.on("-x#", "--max=secs", Float, "Exponenetial Backoff: maximum sleep amount (seconds): Default 60") do |v|
39
+ @max_sleep = v
40
+ end
41
+
42
+ end
43
+
44
+ optparser.parse(*args)
45
+ end
46
+
47
+ def sleep_amount(attempts)
48
+ @constant_sleep || [@min_sleep * (2 ** (attempts - 1)), @max_sleep].min
49
+ end
50
+
51
+ def log_out(message)
52
+ STDERR.puts(message)
53
+ end
54
+
55
+ def run(args)
56
+
57
+ if (args.size < 1 || ["-h", "-?", "--help"].include?(args[0]))
58
+ load_options(["-?"])
59
+ end
60
+
61
+ idx = args.find_index("-e")
62
+ if !idx.nil?
63
+ load_options(args[0...idx])
64
+ args = args[(idx+1)..-1]
65
+ end
66
+
67
+ raise "max_tries must be greater than 0" unless @max_tries > 0
68
+ raise "minimum sleep cannot be greater than maximum sleep" unless @max_sleep >= @min_sleep
69
+ raise "unknown execute command" unless args.size > 0
70
+
71
+ process = nil
72
+ attempts = 0
73
+ success = false
74
+ while (success == false && attempts <= @max_tries)
75
+ if (attempts > 0)
76
+ sleep_time = sleep_amount(attempts)
77
+ log_out("Before retry ##{attempts}: sleeping #{sleep_time} seconds")
78
+ sleep sleep_time
79
+ end
80
+ success = system(args[0], *args[1..-1])
81
+ process = $?
82
+ attempts += 1
83
+ end
84
+
85
+ log_out("Command Failed: #{args[0]}") if success.nil?
86
+ exit process.exitstatus
87
+ end
88
+
89
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retryit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Neville Kadwa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: General purpose retry cli program for anything
14
+ email:
15
+ - neville@kadwa.com
16
+ executables:
17
+ - retry
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - bin/retry
23
+ - lib/retryit.rb
24
+ homepage: http://github.com/kadwanev/retry
25
+ licenses:
26
+ - Apache-2.0
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: retry any command line
48
+ test_files: []
49
+ has_rdoc: