Ver Fonte

Improve logging for catching #1174, and fix tests.

Winston Li há 10 anos atrás
pai
commit
ab09d044a8

+ 72 - 12
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/BridgeAPI.java

@@ -17,6 +17,7 @@ import uk.ac.ic.wlgitbridge.snapshot.push.PostbackManager;
 import uk.ac.ic.wlgitbridge.snapshot.push.PushRequest;
 import uk.ac.ic.wlgitbridge.snapshot.push.PushResult;
 import uk.ac.ic.wlgitbridge.snapshot.push.exception.*;
+import uk.ac.ic.wlgitbridge.util.Log;
 import uk.ac.ic.wlgitbridge.util.Util;
 
 import java.io.IOException;
@@ -45,7 +46,8 @@ public class BridgeAPI {
         mainProjectLock.unlockForProject(projectName);
     }
 
-    public boolean repositoryExists(Credential oauth2, String projectName) throws ServiceMayNotContinueException, ForbiddenException {
+    public boolean repositoryExists(Credential oauth2, String projectName)
+            throws ServiceMayNotContinueException, ForbiddenException {
         lockForProject(projectName);
         GetDocRequest getDocRequest = new GetDocRequest(oauth2, projectName);
         getDocRequest.request();
@@ -63,28 +65,71 @@ public class BridgeAPI {
         return true;
     }
 
-    public void getWritableRepositories(Credential oauth2, String projectName, Repository repository) throws IOException, SnapshotPostException, GitAPIException, ForbiddenException {
+    public void getWritableRepositories(Credential oauth2,
+                                        String projectName,
+                                        Repository repository)
+            throws IOException,
+                   SnapshotPostException,
+                   GitAPIException,
+                   ForbiddenException {
         Util.sout("Fetching project: " + projectName);
         dataStore.updateProjectWithName(oauth2, projectName, repository);
     }
 
-    public void putDirectoryContentsToProjectWithName(Credential oauth2, String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents, String hostname) throws SnapshotPostException, IOException, ForbiddenException {
+    public void
+    putDirectoryContentsToProjectWithName(Credential oauth2,
+                                          String projectName,
+                                          RawDirectory directoryContents,
+                                          RawDirectory oldDirectoryContents,
+                                          String hostname)
+            throws SnapshotPostException, IOException, ForbiddenException {
         mainProjectLock.lockForProject(projectName);
         CandidateSnapshot candidate = null;
         try {
-            Util.sout("Pushing project: " + projectName);
+            Log.info("[Project {}] Pushing", projectName);
             String postbackKey = postbackManager.makeKeyForProject(projectName);
-            candidate = dataStore.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, oldDirectoryContents);
-            PushRequest pushRequest = new PushRequest(oauth2, candidate, postbackKey);
+            Log.info(
+                    "[Project {}] Created postback key: {}",
+                    projectName,
+                    postbackKey
+            );
+            candidate =
+                    dataStore.createCandidateSnapshotFromProjectWithContents(
+                            projectName,
+                            directoryContents,
+                            oldDirectoryContents
+                    );
+            PushRequest pushRequest = new PushRequest(
+                    oauth2,
+                    candidate,
+                    postbackKey
+            );
             pushRequest.request();
             PushResult result = pushRequest.getResult();
             if (result.wasSuccessful()) {
-                dataStore.approveSnapshot(postbackManager.getVersionID(projectName), candidate);
+                Log.info(
+                        "[Project {}] Push to Overleaf successful",
+                        projectName
+                );
+                Log.info("[Project {}] Waiting for postback...", projectName);
+                int versionID =
+                        postbackManager.waitForVersionIdOrThrow(projectName);
+                Log.info(
+                        "[Project {}] Got version ID for push: {}",
+                        projectName,
+                        versionID
+                );
+                dataStore.approveSnapshot(versionID, candidate);
+                Log.info(
+                        "[Project {}] Approved version ID: {}",
+                        projectName,
+                        versionID
+                );
             } else {
                 throw new OutOfDateException();
             }
         } catch (SevereSnapshotPostException e) {
-            e.printStackTrace();
+            Log.warn("Failed to put to Overleaf", e);
             throw e;
         } catch (SnapshotPostException e) {
             throw e;
@@ -93,22 +138,37 @@ public class BridgeAPI {
         } finally {
             if (candidate != null) {
                 candidate.deleteServletFiles();
+            } else {
+                Log.error(
+                        "Candidate snapshot was null: this should never happen."
+                );
             }
             mainProjectLock.unlockForProject(projectName);
         }
     }
 
-    public void checkPostbackKey(String projectName, String postbackKey) throws InvalidPostbackKeyException {
+    public void checkPostbackKey(String projectName, String postbackKey)
+            throws InvalidPostbackKeyException {
         postbackManager.checkPostbackKey(projectName, postbackKey);
     }
 
     /* Called by postback thread. */
-    public void postbackReceivedSuccessfully(String projectName, String postbackKey, int versionID) throws UnexpectedPostbackException {
+    public void postbackReceivedSuccessfully(String projectName,
+                                             String postbackKey,
+                                             int versionID)
+            throws UnexpectedPostbackException {
         postbackManager.postVersionIDForProject(projectName, versionID, postbackKey);
     }
 
-    public void postbackReceivedWithException(String projectName, String postbackKey, SnapshotPostException exception) throws UnexpectedPostbackException {
-        postbackManager.postExceptionForProject(projectName, exception, postbackKey);
+    public void postbackReceivedWithException(String projectName,
+                                              String postbackKey,
+                                              SnapshotPostException exception)
+            throws UnexpectedPostbackException {
+        postbackManager.postExceptionForProject(
+                projectName,
+                exception,
+                postbackKey
+        );
     }
 
 }

+ 1 - 1
services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/push/PostbackManager.java

@@ -22,7 +22,7 @@ public class PostbackManager {
         postbackContentsTable = new HashMap<String, PostbackPromise>();
     }
 
-    public int getVersionID(String projectName) throws SnapshotPostException {
+    public int waitForVersionIdOrThrow(String projectName) throws SnapshotPostException {
         try {
             return postbackContentsTable.get(projectName).waitForPostback();
         } catch (SnapshotPostException e) {

+ 6 - 1
services/git-bridge/src/test/java/uk/ac/ic/wlgitbridge/WLGitBridgeIntegrationTest.java

@@ -612,7 +612,12 @@ public class WLGitBridgeIntegrationTest {
                 "\t\"username\": \"\",\n" +
                 "\t\"password\": \"\",\n" +
                 "\t\"postbackBaseUrl\": \"http://127.0.0.1:" + port + "\",\n" +
-                "\t\"serviceName\": \"Overleaf\"\n" +
+                "\t\"serviceName\": \"Overleaf\"\n," +
+                "    \"oauth2\": {\n" +
+                "        \"oauth2ClientID\": \"clientID\",\n" +
+                "        \"oauth2ClientSecret\": \"oauth2 client secret\",\n" +
+                "        \"oauth2Server\": \"https://www.overleaf.com\"\n" +
+                "    }\n" +
                 "}\n");
         writer.close();
         return config.getAbsolutePath();