service_jynx 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c009a39d4d2e7c6cc7def699c51e36dbf5c3afd
4
- data.tar.gz: 17f338eda74d45838774899c5091e96dfb07e579
3
+ metadata.gz: 31a8eeaba3f5ddc9eec324006473b0809e5b917e
4
+ data.tar.gz: aa1348f58ab0d36f3402e4f2e679657abacfa6ba
5
5
  SHA512:
6
- metadata.gz: 847b210f597317a88396ce42468b4c313c6b9207dedfe1f94888fa05e31aa2431f7496b528d2f52e319d14c06ddf482f09f4bd1749bd9f7c20943ca262e1201d
7
- data.tar.gz: 0f98d7d2ed5d9a1068c66856d51779b8f8b4119753a8bd30418b56c0ab6d035bb84ec294a2fbfe26d458f24fb365032007087b725140705c4c338087f5d56703
6
+ metadata.gz: 25b220479d24fe6adfc5b60c6bb784fe912d0cc06942bf494c612f901f760f5db1ed538eb4482347eb2fb0e42722ed5b038229fcc03d2136508192475321a7f4
7
+ data.tar.gz: b1cf5af0c289a4e1008bf384e65f92c9273a0605e5bda4a74c325d02902254da2ec776560669d4ace659432dba536e4ccda67cd5a1aba67a9af451a1f73a1ac0
data/README.md CHANGED
@@ -16,9 +16,9 @@ The code is MRI depended and is not thread safe(!), is is also designed specific
16
16
 
17
17
  def index
18
18
  begin
19
- response = {countery: "Israel"}
19
+ response = {country: "Israel"}
20
20
  if ServiceJynx.alive?("inbox")
21
- response = {countery: HTTParty.get("https://api.github.com/users/AvnerCohen", :timeout => 20)["location"]}
21
+ response = {country: HTTParty.get("https://api.github.com/users/AvnerCohen", :timeout => 20)["location"]}
22
22
  else
23
23
  response.merge!({error: "service down ! #{__method__}"})
24
24
  end
@@ -26,12 +26,54 @@ The code is MRI depended and is not thread safe(!), is is also designed specific
26
26
  ServiceJynx.failure!("inbox")
27
27
  response.merge!({error: "exception occured, #{__method__}"})
28
28
  ensure
29
- render json: response.to_json and return
29
+ render json: response and return
30
30
  end
31
31
  end
32
32
 
33
33
  ````
34
34
 
35
+
36
+ ## Complete Use case extracted
37
+
38
+ [1] Register the service for Jynx monitoring at application start time:
39
+
40
+ ````
41
+ opts = {
42
+ time_window_in_seconds: 20,
43
+ max_errors: 10,
44
+ grace_period: 60
45
+ }
46
+ ServiceJynx.register!("github_api", opts)
47
+ ````
48
+
49
+ [2] Define a module that wraps your HTTP calls to have a generic safe api
50
+
51
+ ````
52
+ module HttpInternalWrapper
53
+ extend self
54
+ def get_api(service_name, url, &on_error_block)
55
+ if ServiceJynx.alive?(service_name)
56
+ HTTParty.get(url, :timeout => 20)
57
+ else
58
+ on_error_block.call("#{service_name}_service set as down.")
59
+ end
60
+ rescue Exception => e
61
+ ServiceJynx.failure!(service_name)
62
+ on_error_block.call("Exception in #{service_name}_service exceution - #{e.message}")
63
+ end
64
+ end
65
+ ````
66
+
67
+ [3] Execute with a stubbed on_error_block that gets executed on failure or service down
68
+
69
+ ````
70
+ HttpInternalWrapper.get_api("github_api", "https://api.github.com/users/AvnerCohen") do |msg|
71
+ @logger.error "#{msg} -- #{path} failed at #{__method__} !!"
72
+ {error: "Github api is currently unavailable"} # stub an empty hash with error messages
73
+ end
74
+ ````
75
+
76
+
35
77
  ## Defaults
36
78
 
37
79
  Defined when registering a service:
data/lib/service_jynx.rb CHANGED
@@ -33,7 +33,12 @@ module ServiceJynx
33
33
  now = Time.now.to_i
34
34
  jynx.errors << now
35
35
  jynx.clean_aged(now)
36
- down!(name, "Max error count (#{jynx.max_errors}) reached at #{Time.now}.") if jynx.errors.count > jynx.max_errors
36
+ if jynx.errors.count > jynx.max_errors
37
+ down!(name, "Max error count (#{jynx.max_errors}) reached at #{Time.now}.")
38
+ :WENT_DOWN
39
+ else
40
+ :FAIL_MARKED
41
+ end
37
42
  end
38
43
 
39
44
 
@@ -1,3 +1,3 @@
1
1
  module ServiceJynx
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -88,6 +88,20 @@ describe ServiceJynx do
88
88
  ServiceJynx.alive?(:dummy_service).should eq(false)
89
89
  end
90
90
 
91
+ it "should report result for failure" do
92
+ ServiceJynx.register!(:dummy_service, {time_window_in_seconds: 2, max_errors: 3})
93
+ jynx = ServiceJynx.counters.fetch(:dummy_service)
94
+ ServiceJynx.alive?(:dummy_service).should eq(true)
95
+ ServiceJynx.failure!(:dummy_service).should eq(:FAIL_MARKED)
96
+ ServiceJynx.failure!(:dummy_service).should eq(:FAIL_MARKED)
97
+ ServiceJynx.failure!(:dummy_service).should eq(:FAIL_MARKED)
98
+
99
+ ## After 3 errors, report as down
100
+ ServiceJynx.failure!(:dummy_service).should eq(:WENT_DOWN)
101
+ ServiceJynx.alive?(:dummy_service).should eq(false)
102
+ end
103
+
104
+
91
105
  it "should auto disable when errors limit reached old errors and restart again when grace period passes" do
92
106
  ServiceJynx.register!(:dummy_service, {time_window_in_seconds: 2, max_errors: 20, grace_period: 5})
93
107
  jynx = ServiceJynx.counters.fetch(:dummy_service)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_jynx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avner Cohen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-02 00:00:00.000000000 Z
11
+ date: 2013-12-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use errors count over sliding windows to block calls to an external service
14
14
  or method, or whatever.