Skip to content

Instantly share code, notes, and snippets.

@bdionne
Created February 22, 2013 13:00
Show Gist options
  • Save bdionne/5013239 to your computer and use it in GitHub Desktop.
Save bdionne/5013239 to your computer and use it in GitHub Desktop.
read_changes(StartSeq, Db, ChangesQueue, Options) ->
try
couch_replicator_api_wrap:changes_since(Db, all_docs, StartSeq,
fun(#doc_info{high_seq = Seq, id = Id} = DocInfo) ->
case Id of
<<>> ->
% Previous CouchDB releases had a bug which allowed a doc
% with an empty ID to be inserted into databases. Such doc
% is impossible to GET.
twig:log(error,"Replicator: ignoring document with empty ID in "
"source database `~s` (_changes sequence ~p)",
[couch_replicator_api_wrap:db_uri(Db), Seq]);
_ ->
ok = couch_work_queue:queue(ChangesQueue, DocInfo)
end,
put(last_seq, Seq)
end, Options),
% check for disconnect of continuous
case couch_util:get_value(continuous, Options, false) of
true ->
read_changes(get(last_seq), Db, ChangesQueue, Options);
false ->
ok
end,
couch_work_queue:close(ChangesQueue)
catch exit:{http_request_failed, _, _, _} = Error ->
case get(retries_left) of
N when N > 0 ->
put(retries_left, N - 1),
LastSeq = get(last_seq),
Db2 = case LastSeq of
StartSeq ->
twig:log(notice,"Retrying _changes request to source database ~s"
" with since=~p in ~p seconds",
[couch_replicator_api_wrap:db_uri(Db), LastSeq, Db#httpdb.wait / 1000]),
ok = timer:sleep(Db#httpdb.wait),
Db#httpdb{wait = 2 * Db#httpdb.wait};
_ ->
twig:log(notice,"Retrying _changes request to source database ~s"
" with since=~p", [couch_replicator_api_wrap:db_uri(Db), LastSeq]),
Db
end,
read_changes(LastSeq, Db2, ChangesQueue, Options);
_ ->
exit(Error)
end
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment