couchrest_changes 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ module CouchRest
5
5
  module Config
6
6
  extend self
7
7
 
8
+ attr_writer :app_name
8
9
  attr_accessor :connection
9
10
  attr_accessor :seq_file
10
11
  attr_accessor :log_file
@@ -43,6 +44,10 @@ module CouchRest
43
44
  join('_')
44
45
  end
45
46
 
47
+ def app_name
48
+ @app_name ||= Pathname.new($0).basename
49
+ end
50
+
46
51
  private
47
52
 
48
53
  def init_logger
@@ -51,7 +56,7 @@ module CouchRest
51
56
  @logger = Logger.new(log_file)
52
57
  else
53
58
  require 'syslog/logger'
54
- @logger = Syslog::Logger.new('leap_key_daemon')
59
+ @logger = Syslog::Logger.new(app_name)
55
60
  end
56
61
  @logger.level = Logger.const_get(log_level.upcase)
57
62
  end
@@ -1,5 +1,5 @@
1
1
  module CouchRest
2
2
  class Changes
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -11,8 +11,8 @@ module CouchRest
11
11
 
12
12
  def initialize(db_name, options = {})
13
13
  db_name = Config.complete_db_name(db_name)
14
- logger.info "Tracking #{db_name}"
15
- logger.debug "Options: #{options.inspect}" if options.keys.any?
14
+ info "Tracking #{db_name}"
15
+ debug "Options: #{options.inspect}" if options.keys.any?
16
16
  @options = options
17
17
  @db = CouchRest.new(Config.couch_host).database(db_name)
18
18
  read_seq(Config.seq_file) unless rerun?
@@ -40,30 +40,24 @@ module CouchRest
40
40
  end
41
41
 
42
42
  def listen
43
- logger.info "listening..."
44
- logger.debug "Starting at sequence #{since}"
43
+ info "listening..."
44
+ debug "Starting at sequence #{since}"
45
45
  result = db.changes feed_options do |hash|
46
+ @retry_count = 0
46
47
  callbacks(hash)
47
48
  store_seq(hash["seq"])
48
49
  end
49
- logger.info "couch stream ended unexpectedly." unless run_once?
50
- logger.debug result.inspect
51
- rescue MultiJson::LoadError
52
- # appearently MultiJson has issues with the end of the
53
- # couch stream if we do not use the continuous feed.
54
- # For now we just catch the exception and proceed.
50
+ raise EOFError
51
+ # appearently MultiJson has issues with the end of the couch stream.
52
+ # So sometimes we get a MultiJson::LoadError instead...
53
+ rescue MultiJson::LoadError, EOFError, RestClient::ServerBrokeConnection
54
+ return if run_once?
55
+ log_and_recover(result)
56
+ retry
55
57
  end
56
58
 
57
59
  protected
58
60
 
59
- def logger
60
- logger ||= Config.logger
61
- end
62
-
63
- def db
64
- @db
65
- end
66
-
67
61
  def feed_options
68
62
  if run_once?
69
63
  { :since => since }
@@ -96,22 +90,22 @@ module CouchRest
96
90
  end
97
91
 
98
92
  def read_seq(filename)
99
- logger.debug "Looking up sequence here: #{filename}"
93
+ debug "Looking up sequence here: #{filename}"
100
94
  FileUtils.touch(filename)
101
95
  unless File.writable?(filename)
102
96
  raise StandardError.new("Can't write to sequence file #{filename}")
103
97
  end
104
98
  @since = File.read(filename)
105
99
  rescue Errno::ENOENT => e
106
- logger.warn "No sequence file found. Starting from scratch"
100
+ warn "No sequence file found. Starting from scratch"
107
101
  end
108
102
 
109
103
  def check_seq
110
104
  if @since == ''
111
105
  @since = nil
112
- logger.debug "Found no sequence in the file."
106
+ debug "Found no sequence in the file."
113
107
  elsif @since
114
- logger.debug "Found sequence: #{@since}"
108
+ debug "Found sequence: #{@since}"
115
109
  end
116
110
  end
117
111
 
@@ -119,13 +113,21 @@ module CouchRest
119
113
  File.write Config.seq_file, MultiJson.dump(seq)
120
114
  end
121
115
 
116
+ def log_and_recover(result)
117
+ debug result.inspect if result
118
+ info "Couch stream ended unexpectedly."
119
+ info "Will retry in 15 seconds."
120
+ info "Retried #{retry_count} times so far."
121
+ sleep 15
122
+ @retry_count += 1
123
+ end
124
+
122
125
  #
123
126
  # UNUSED: this is useful for only following new sequences.
124
127
  # might also require .to_json to work on bigcouch.
125
128
  #
126
129
  def fetch_last_seq
127
130
  hash = db.changes :limit => 1, :descending => true
128
- logger.info "starting at seq: " + hash["last_seq"]
129
131
  return hash["last_seq"]
130
132
  end
131
133
 
@@ -136,5 +138,39 @@ module CouchRest
136
138
  def run_once?
137
139
  Config.flags.include?('--run-once')
138
140
  end
141
+
142
+ def info(message)
143
+ return unless log_attempt?
144
+ logger.info message
145
+ end
146
+
147
+ def debug(message)
148
+ return unless log_attempt?
149
+ logger.debug message
150
+ end
151
+
152
+ def warn(message)
153
+ return unless log_attempt?
154
+ logger.warn message
155
+ end
156
+
157
+ # let's not clutter the logs if couch is down for a longer time.
158
+ def log_attempt?
159
+ [0, 1, 2, 4, 8, 20, 40, 120].include?(retry_count) ||
160
+ retry_count % 240 == 0
161
+ end
162
+
163
+ def retry_count
164
+ @retry_count ||= 0
165
+ end
166
+
167
+ def logger
168
+ logger ||= Config.logger
169
+ end
170
+
171
+ def db
172
+ @db
173
+ end
174
+
139
175
  end
140
176
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest_changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-22 00:00:00.000000000 Z
12
+ date: 2014-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: couchrest