caddy 0.0.1 → 0.0.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 +4 -4
  2. data/README.md +14 -0
  3. data/lib/caddy.rb +36 -21
  4. data/lib/caddy/version.rb +2 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 557e6259f9f172bdaf516e829bb169d8a89663f9
4
- data.tar.gz: 0f36ca38a97ff49e466d97206823e1ad78c1e34b
3
+ metadata.gz: 37c9cbaa3ab3273b12d27046c3723417437e1e6d
4
+ data.tar.gz: 4111cd80d00e965f691e0eb312d397c7c80bb28a
5
5
  SHA512:
6
- metadata.gz: b54cbcab570b82c83f97d7e78823ee6affa185cfb8ec1f6edf9d81c6fd8ec01454266cdc496cbea6c656f8acf545c5b2e05ac1122eff5fcedd0805c5ef1468e6
7
- data.tar.gz: 0a6b074bb61328edbd8ee7bfc05e94ab2fcca3d75f64b2e1a1cb5f44a981780e16f7b77fee5df06a707d3b38f92e7151c2c150f0de8d5ec5aaeb43c04dc00e94
6
+ metadata.gz: bbbcaed1cee1a6d39f4eec77632679360386de76933e3dabf4160e04948891cc2ef71afa13d8f0afda68509d038034df8d3537b75453c3b66c6f5d057f3c19f7
7
+ data.tar.gz: 8227f5b289ccb13b667ca407a05ad71aa30d36a195677de1063dc0f3bad7b7144dc95795fcfc35d057ead60b08906580b0a3bc0b2d6a76fb8fcb09c58ebbdeea
data/README.md CHANGED
@@ -22,6 +22,20 @@ def index
22
22
  end
23
23
  ```
24
24
 
25
+ ## Using Caddy with Spring (in development)
26
+
27
+ For testing Caddy in development with Spring, you need to have Caddy start after fork:
28
+
29
+ ```ruby
30
+ # in your caddy.rb initializer, perhaps
31
+
32
+ if Rails.env.development?
33
+ Spring.after_fork do
34
+ Caddy.start
35
+ end
36
+ end
37
+ ```
38
+
25
39
  ## Give it to me!
26
40
 
27
41
  Add this line to your application's Gemfile:
@@ -1,19 +1,19 @@
1
+ # frozen_string_literal: true
1
2
  require "caddy/version"
2
3
  require "concurrent/timer_task"
3
4
 
4
5
  module Caddy
5
6
  class << self
6
- attr_accessor :refresher
7
+ attr_accessor :refresher, :refresher_error_handler
7
8
  attr_writer :refresh_interval
8
9
  end
9
10
 
10
11
  DEFAULT_REFRESH_INTERVAL = 60
11
- REFRESH_INTERVAL_JITTER_PCT = 0.1
12
- DEFAULT_INITIAL_VALUE_WAIT = 0.1
12
+ REFRESH_INTERVAL_JITTER_PCT = 0.15
13
13
 
14
14
  def self.[](k)
15
15
  raise "Please run `Caddy.start` before attempting to access values" unless @task
16
- raise "Caddy variable access before initial load; allow some more time for your system to warm up" unless @task.value
16
+ raise "Caddy variable access before initial load; allow some more time for your app to start up" unless @task.value
17
17
  @task.value[k]
18
18
  end
19
19
 
@@ -22,44 +22,59 @@ module Caddy
22
22
  raise "Please set your cache refresher via `Caddy.refresher = -> { <code that returns a value> }`"
23
23
  end
24
24
 
25
- if @task
26
- puts "Caddy already running; shutting it down and starting over. Please ensure you run `Caddy.start`"\
27
- " only after fork/worker start in your web processes."
28
- @task.shutdown
29
- end
30
-
31
- jitter_amount = refresh_interval * REFRESH_INTERVAL_JITTER_PCT
25
+ jitter_amount = [1, refresh_interval * REFRESH_INTERVAL_JITTER_PCT].max
32
26
  interval = refresh_interval + rand(-jitter_amount...jitter_amount)
33
27
 
34
- @task = Concurrent::TimerTask.new(
28
+ task = Concurrent::TimerTask.new(
35
29
  run_now: true,
36
- freeze_on_deref: true,
37
30
  execution_interval: interval,
38
31
  timeout_interval: interval - 1) { refresher.call }
39
32
 
40
- @task.add_observer(CaddyTaskObserver.new)
41
- @task.execute
33
+ task.add_observer(InternalCaddyTaskObserver.new)
34
+ task.execute
35
+
36
+ _stop_internal # stop any existing task from running
37
+
38
+ @task = task # and transfer over the new task
42
39
  end
43
40
 
44
41
  def self.stop
45
42
  raise "Please run `Caddy.start` before running `Caddy.stop`" unless @task
46
43
 
47
- @task.shutdown
48
- @task = nil
44
+ _stop_internal
49
45
  end
50
46
 
51
47
  def self.refresh_interval
52
48
  @refresh_interval || DEFAULT_REFRESH_INTERVAL
53
49
  end
54
50
 
55
- class CaddyTaskObserver
51
+ private_class_method def self._stop_internal
52
+ @task.shutdown if @task && @task.running?
53
+ end
54
+
55
+ class InternalCaddyTaskObserver
56
56
  def update(time, _, boom)
57
57
  return unless boom
58
58
 
59
- if boom.is_a?(Concurrent::TimeoutError)
60
- puts "(#{time}) Caddy refresher timed out"
59
+ if Caddy.refresher_error_handler
60
+ if Caddy.refresher_error_handler.respond_to?(:call)
61
+ begin
62
+ Caddy.refresher_error_handler.call(boom)
63
+ rescue => incepted_boom
64
+ STDERR.puts "(#{time}) Caddy error handler itself errored: #{incepted_boom}"
65
+ end
66
+ else
67
+ # rubocop:disable Style/StringLiterals
68
+ STDERR.puts 'Caddy error handler not callable. Please set the error handler like:'\
69
+ ' `Caddy.refresher_error_handler = -> (e) { puts "#{e}" }`'
70
+ # rubocop:enable Style/StringLiterals
71
+
72
+ STDERR.puts "(#{time}) Caddy refresher failed with error #{boom}"
73
+ end
74
+ elsif boom.is_a?(Concurrent::TimeoutError)
75
+ STDERR.puts "(#{time}) Caddy refresher timed out"
61
76
  else
62
- puts "(#{time}) Caddy refresher failed with error #{boom}"
77
+ STDERR.puts "(#{time}) Caddy refresher failed with error #{boom}"
63
78
  end
64
79
  end
65
80
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Caddy
2
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2".freeze
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caddy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Elser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-01 00:00:00.000000000 Z
11
+ date: 2016-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby