Delete event file only after replication completed for all destinations

The event file is created only once even if the ref is replicated to
more than one destination, but it was deleting after replication is
completed to each destination.

This was wrong, as only after the first destination completes the file
was successfully deleted. And upon subsequent destination completion,
the deletion was failing with NoSuchFileException. On top of the error
getting logged, this is also wrong as the plugin could be reloaded
before replication is completed on all destinations. And if one of them
completed, then replication for that ref would not have been
rescheduled.

Change-Id: I07b3b88e42382330da409dd9e156498c0bf434a9
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java
index 6f0803a..ec878db 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java
@@ -86,7 +86,6 @@
       URIish uri,
       RefPushResult status,
       RemoteRefUpdate.Status refUpdateStatus) {
-    deleteEvent();
     pushResultProcessing.onRefReplicatedToOneNode(project, ref, uri, status, refUpdateStatus);
 
     RefReplicationStatus completedRefStatus = null;
@@ -116,12 +115,6 @@
     }
   }
 
-  private void deleteEvent() {
-    if (eventKey != null) {
-      eventsStorage.delete(eventKey);
-    }
-  }
-
   public void markAllPushTasksScheduled() {
     countingLock.lock();
     try {
@@ -152,10 +145,17 @@
   }
 
   private void doRefPushTasksCompleted(RefReplicationStatus refStatus) {
+    deleteEvent();
     pushResultProcessing.onRefReplicatedToAllNodes(
         refStatus.project, refStatus.ref, refStatus.nodesToReplicateCount);
   }
 
+  private void deleteEvent() {
+    if (eventKey != null) {
+      eventsStorage.delete(eventKey);
+    }
+  }
+
   private RefReplicationStatus getRefStatus(String project, String ref) {
     if (!statusByProjectRef.contains(project, ref)) {
       RefReplicationStatus refStatus = new RefReplicationStatus(project, ref);