self_destruct 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/self_destruct.rb +78 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6bd1552cc0fcc21a8cbfe8fe19d8f77014e38bc
4
+ data.tar.gz: 5e6099e05b64617f92f84388bf8d857fb7a4224d
5
+ SHA512:
6
+ metadata.gz: da78b7b41d3631b403ac8450df78fa2b7d256c476899ad9ef4269372cf9443c0c4c9b41ac7e3552ca9e9ce4c054f8c4855b5aadff1fc00f8d44f8c64b08a8301
7
+ data.tar.gz: 33159134211f93c34e553eef8c8c36ee90a4a5951b2aeaa06b1649a46fdf0bc2e41a6f7cea281eca38ccbb9014304d2c105c9683933f7687eac7a262595a9911
@@ -0,0 +1,78 @@
1
+ module SelfDestruct
2
+
3
+ class Agent
4
+
5
+ DEFAULT_RATIO_THRESHOLD = 5
6
+ DEFAULT_GRACE_PERIOD = 60 # in seconds
7
+ DEFAULT_MIN_ATTEMPTS = 20
8
+ DEFAULT_FAILURE_LAMBDA = lambda { abort "Too many failures" }
9
+ DEFAULT_MAX_COUNT = 1000000
10
+
11
+ def initialize(opts = {})
12
+ @success_count = 0
13
+ @failure_count = 0
14
+ @start_time = nil
15
+ @ratio_threshold = opts[:ratio_threshold] || DEFAULT_RATIO_THRESHOLD
16
+ @grace_period = opts[:grace_period] || DEFAULT_GRACE_PERIOD
17
+ @min_attempts = opts[:min_attempts] || DEFAULT_MIN_ATTEMPTS
18
+ @failure_func = opts[:failure_lambda] || DEFAULT_FAILURE_LAMBDA
19
+ @max_count = opts[:max_count] || DEFAULT_MAX_COUNT
20
+ end
21
+
22
+ def inc_successes
23
+ @success_count += 1
24
+ check_ratio!
25
+ end
26
+
27
+ def inc_failures
28
+ @failure_count += 1
29
+ check_ratio!
30
+ end
31
+
32
+ def reset!
33
+ @success_count = 0
34
+ @failure_count = 0
35
+ @start_time = nil
36
+ end
37
+
38
+ private
39
+
40
+ def set_start_time
41
+ @start_time ||= Time.now
42
+ end
43
+
44
+ def check_ratio!
45
+ ratio = get_ratio
46
+ if (ratio < @ratio_threshold) and grace_period_has_passed? and minimum_attempts_reached?
47
+ do_failure
48
+ end
49
+ check_total_num
50
+ end
51
+
52
+ def check_total_num
53
+ reset! if get_total > @max_count
54
+ end
55
+
56
+ def minimum_attempts_reached?
57
+ get_total >= @min_attempts
58
+ end
59
+
60
+ def grace_period_has_passed?
61
+ set_start_time
62
+ (Time.now - @start_time) > @grace_period
63
+ end
64
+
65
+ def get_total
66
+ @success_count + @failure_count
67
+ end
68
+
69
+ def get_ratio
70
+ @success_count.to_f / @failure_count.to_f
71
+ end
72
+
73
+ def do_failure
74
+ @failure_func.call
75
+ end
76
+
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: self_destruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Huynh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2010-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Adds the ability for ruby processes to die after some conditions are
28
+ met.
29
+ email: me@khuynh.info
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/self_destruct.rb
35
+ homepage: https://github.com/heepster/self_destruct
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.4.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Adds the ability for ruby processes to die after some conditions are met.
59
+ test_files: []