couchrest_changes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -11,7 +11,7 @@ users = CouchRest::Changes.new('users')
11
11
  Callbacks can be defined in blocks:
12
12
  ```ruby
13
13
  users.created do |hash|
14
- puts "A new user was created with the id: #{hash[:id]}
14
+ puts "A new user was created with the id: #{hash[:id]}"
15
15
  end
16
16
  ```
17
17
 
@@ -57,4 +57,12 @@ seq_file: "/var/log/couch_changes_users.seq"
57
57
  # Configure log_file like this if you want to log to a file instead of syslog:
58
58
  # log_file: "/var/log/couch_changes.log"
59
59
  log_level: debug
60
+
61
+ options:
62
+ your_own_options: "go here"
60
63
  ```
64
+
65
+ Examples
66
+ ------------------------
67
+
68
+ See [tapicero](https://github.com/leapcode/tapicero) for a daemon that uses CouchRest::Changes. Historically CouchRest::Changes was extracted from tapicero.
@@ -11,6 +11,7 @@ module CouchRest
11
11
  attr_writer :log_level
12
12
  attr_accessor :logger
13
13
  attr_accessor :options
14
+ attr_accessor :flags
14
15
 
15
16
  def load(base_dir, *configs)
16
17
  @base_dir = Pathname.new(base_dir)
@@ -1,5 +1,5 @@
1
1
  module CouchRest
2
2
  class Changes
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -13,8 +13,8 @@ module CouchRest
13
13
  db_name = Config.complete_db_name(db_name)
14
14
  logger.info "Tracking #{db_name}"
15
15
  @db = CouchRest.new(Config.couch_host).database(db_name)
16
- @seq_filename = Config.seq_file
17
- read_seq(@seq_filename)
16
+ read_seq(Config.seq_file) unless Config.flags.include?('--rerun')
17
+ check_seq
18
18
  end
19
19
 
20
20
  # triggered when a document was newly created
@@ -40,12 +40,16 @@ module CouchRest
40
40
  def listen
41
41
  logger.info "listening..."
42
42
  logger.debug "Starting at sequence #{since}"
43
- result = db.changes :feed => :continuous, :since => since, :heartbeat => 1000 do |hash|
43
+ result = db.changes feed_options do |hash|
44
44
  callbacks(hash)
45
45
  store_seq(hash["seq"])
46
46
  end
47
47
  logger.info "couch stream ended unexpectedly."
48
48
  logger.debug result.inspect
49
+ rescue MultiJson::LoadError
50
+ # appearently MultiJson has issues with the end of the
51
+ # couch stream if we do not use the continuous feed.
52
+ # For now we just catch the exception and proceed.
49
53
  end
50
54
 
51
55
  protected
@@ -58,6 +62,14 @@ module CouchRest
58
62
  @db
59
63
  end
60
64
 
65
+ def feed_options
66
+ if Config.flags.include?('--run-once')
67
+ { :since => since }
68
+ else
69
+ { :feed => :continuous, :since => since, :heartbeat => 1000 }
70
+ end
71
+ end
72
+
61
73
  def since
62
74
  @since ||= 0 # fetch_last_seq
63
75
  end
@@ -81,25 +93,28 @@ module CouchRest
81
93
  end
82
94
  end
83
95
 
84
- def read_seq(seq_filename)
85
- logger.debug "Looking up sequence here: #{seq_filename}"
86
- FileUtils.touch(seq_filename)
87
- unless File.writable?(seq_filename)
88
- raise StandardError.new("Can't access sequence file")
96
+ def read_seq(filename)
97
+ logger.debug "Looking up sequence here: #{filename}"
98
+ FileUtils.touch(filename)
99
+ unless File.writable?(filename)
100
+ raise StandardError.new("Can't write to sequence file #{filename}")
89
101
  end
90
- @since = File.read(seq_filename)
102
+ @since = File.read(filename)
103
+ rescue Errno::ENOENT => e
104
+ logger.warn "No sequence file found. Starting from scratch"
105
+ end
106
+
107
+ def check_seq
91
108
  if @since == ''
92
109
  @since = nil
93
110
  logger.debug "Found no sequence in the file."
94
- else
111
+ elsif @since
95
112
  logger.debug "Found sequence: #{@since}"
96
113
  end
97
- rescue Errno::ENOENT => e
98
- logger.warn "No sequence file found. Starting from scratch"
99
114
  end
100
115
 
101
116
  def store_seq(seq)
102
- File.write @seq_filename, MultiJson.dump(seq)
117
+ File.write Config.seq_file, MultiJson.dump(seq)
103
118
  end
104
119
 
105
120
  #
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: