couchrest_changes 0.0.1 → 0.0.2
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.
- data/Readme.md +9 -1
- data/lib/couchrest/changes/config.rb +1 -0
- data/lib/couchrest/changes/version.rb +1 -1
- data/lib/couchrest/changes.rb +28 -13
- metadata +1 -1
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.
|
data/lib/couchrest/changes.rb
CHANGED
@@ -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
|
-
|
17
|
-
|
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
|
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(
|
85
|
-
logger.debug "Looking up sequence here: #{
|
86
|
-
FileUtils.touch(
|
87
|
-
unless File.writable?(
|
88
|
-
raise StandardError.new("Can't
|
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(
|
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
|
-
|
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
|
117
|
+
File.write Config.seq_file, MultiJson.dump(seq)
|
103
118
|
end
|
104
119
|
|
105
120
|
#
|