Skip to content

Instantly share code, notes, and snippets.

@bdionne
Created February 12, 2013 13:38
Show Gist options
  • Save bdionne/4769933 to your computer and use it in GitHub Desktop.
Save bdionne/4769933 to your computer and use it in GitHub Desktop.
update_rep_doc(RepDbName, RepDocId, KVs) when is_binary(RepDocId) ->
{Pid, Ref} =
spawn_monitor(fun() ->
try
case fabric:open_doc(mem3:dbname(RepDbName), RepDocId, []) of
{ok, LatestRepDoc} ->
update_rep_doc(RepDbName, LatestRepDoc, KVs);
_ ->
ok
end
catch
throw:conflict ->
% a race condition may cause an update conflict,
% in which cae update_rep_doc is called again to refetch
twig:log(error, "Conflict error when updating replication document `~s`."
" Retrying.", [RepDocId]),
ok = timer:sleep(5),
update_rep_doc(RepDbName, RepDocId, KVs);
Type:Error ->
exit({Type, Error})
end
end),
receive
{'DOWN', Ref, process, Pid, normal} ->
ok;
{'DOWN', Ref, process, Pid, {throw, Error}} ->
throw(Error);
{'DOWN', Ref, process, Pid, {error, Error}} ->
erlang:error(Error)
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment