anchor.diff:      Server = self(),
       ok = couch_config:register(
           fun("replicator", "db") ->
  -            ok = gen_server:call(Server, rep_db_changed, infinity)
  +            ok = gen_server:cast(Server, rep_db_changed)
           end
       ),
       {ok, #state{
           changes_feed_loop = Loop,
           changes_queue = Queue,
  -        changes_processor = Processor}
  +        changes_processor = Processor,
  +        db_notifier = db_update_notifier()}
       }.
   
  -handle_call(rep_db_changed, _From, State) ->
  +
  +handle_call(Msg, From, State) ->
  +    ?LOG_ERROR("Replicator DB listener receive unexpected call ~p from ~p",
  +        [Msg, From]),
  +    {stop, {error, {unexpected_call, Msg}}, State}.
  +
  +
  +handle_cast(rep_db_changed, State) ->
       #state{
           changes_feed_loop = Loop,
           changes_queue = Queue
       } = State,
       exit(Loop, rep_db_changed),
  +    couch_work_queue:queue(Queue, stop_all_replications),
       {ok, NewLoop} = changes_feed_loop(Queue),
  -    {reply, ok, State#state{changes_feed_loop = NewLoop}}.
  -
  -
  -handle_cast(_Msg, State) ->
  -    {noreply, State}.
  -
  +    {noreply, State#state{changes_feed_loop = NewLoop}};
  +
  +handle_cast(rep_db_created, #state{changes_feed_loop = nil} = State) ->
  +    {ok, NewLoop} = changes_feed_loop(State#state.changes_queue),
  +    {noreply, State#state{changes_feed_loop = NewLoop}};
  +
  +handle_cast(Msg, State) ->
  +    ?LOG_ERROR("Replicator DB listener receive unexpected cast ~p", [Msg]),
  +    {stop, {error, {unexpected_cast, Msg}}, State}.
  +
  +
  +handle_info({'EXIT', From, normal}, #state{changes_feed_loop = From} = State) ->
  +    % replicator DB deleted
  +    couch_work_queue:queue(State#state.changes_queue, stop_all_replications),
  +    {noreply, State#state{changes_feed_loop = nil}};
  +
  +handle_info({'EXIT', From, Reason}, #state{db_notifier = From} = State) ->
  +    ?LOG_ERROR("Database update notifier died. Reason: ~p", [Reason]),
  +    {stop, {db_update_notifier_died, Reason}, State};
   
   handle_info({'EXIT', _OldChangesLoop, rep_db_changed}, State) ->
       {noreply, State};
   
   handle_info({'EXIT', From, Reason}, #state{changes_processor = From} = State) ->
       ?LOG_ERROR("Replicator DB changes processor died. Reason: ~p", [Reason]),
  -    {stop, rep_db_changes_processor_error, State}.
  +    {stop, {rep_db_changes_processor_died, Reason}, State}.
   
   
   terminate(_Reason, State) ->
  
positive.code:     {ok, Pid}.
  
  
  changes_processor(ChangesQueue) ->
      Pid = spawn_link(
          fun() ->
  
negative.code:     [] ->
          none
      end.
  
  stop_all_replications() ->
      ets:foldl(
          fun({_, RepId}, _) -> couch_rep:end_replication(RepId) end,
          ok,
          ?DOC_TO_REP_ID_MAP
      ),
      true = ets:delete_all_objects(?REP_ID_TO_DOC_ID_MAP),
      true = ets:delete_all_objects(?DOC_TO_REP_ID_MAP).
  