Skip to content

Commit

Permalink
Migrations restart when an exception is thrown in the Confluence Migr…
Browse files Browse the repository at this point in the history
…ator Pro code #101

Green "Migration complete" message shown when clicking on cancel #45
  • Loading branch information
raphj committed Mar 29, 2024
1 parent 2d4fb48 commit f789abb
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 77 deletions.
Expand Up @@ -117,11 +117,11 @@ public void onEvent(Event event, Object source, Object data)
boolean anySelected = !isPropertyEnabled(jobStatusToAsk, "onlyLinkMapping")
&& handleSpaceSelection((ConfluenceFilteringEvent) event, (ConfluenceXMLPackage) data, jobStatusToAsk);

if (isPropertyEnabled(jobStatusToAsk, "saveLinkMapping")) {
if (!jobStatusToAsk.isCanceled() && isPropertyEnabled(jobStatusToAsk, "saveLinkMapping")) {
updateLinkMapping(jobStatusToAsk);
}

if (!anySelected) {
if (!anySelected || jobStatusToAsk.isCanceled()) {
((CancelableEvent) event).cancel();
}
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
package com.xwiki.confluencepro.internal;

import java.lang.reflect.Type;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -38,11 +39,14 @@
import javax.inject.Provider;
import javax.inject.Singleton;

import com.xpn.xwiki.XWiki;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.xwiki.component.annotation.Component;
import org.xwiki.contrib.confluence.filter.PageIdentifier;
import org.xwiki.contrib.confluence.filter.internal.ConfluenceFilter;
import org.xwiki.logging.LogLevel;
import org.xwiki.logging.event.LogEvent;
import org.xwiki.logging.tail.LogTail;
import org.xwiki.model.reference.DocumentReference;
Expand Down Expand Up @@ -82,6 +86,12 @@ public class DefaultConfluenceMigrationManager implements ConfluenceMigrationMan

private static final String LINKS_BROKEN = "Links to this page may be broken";

private static final String LOGS = "logs";

private static final String EXECUTED = "executed";

private static final String AN_EXCEPTION_OCCURRED = "An exception occurred";

@Inject
private Provider<XWikiContext> contextProvider;

Expand All @@ -91,17 +101,23 @@ public class DefaultConfluenceMigrationManager implements ConfluenceMigrationMan
@Inject
private ConfluenceMigrationPrerequisitesManager prerequisitesManager;

@Inject
private Logger logger;

@Override
public void updateAndSaveMigration(ConfluenceMigrationJobStatus jobStatus)
{
XWikiContext context = contextProvider.get();
XWikiDocument document = null;
BaseObject object = null;
XWiki wiki = context.getWiki();
DocumentReference statusDocumentReference = jobStatus.getRequest().getStatusDocumentReference();
try {

XWikiDocument document =
context.getWiki().getDocument(jobStatus.getRequest().getStatusDocumentReference(), context).clone();
document = wiki.getDocument(statusDocumentReference, context).clone();
// Set executed to true.
BaseObject object = document.getXObject(MIGRATION_OBJECT);
object.set("executed", 1, context);
object = document.getXObject(MIGRATION_OBJECT);
object.set(EXECUTED, jobStatus.isCanceled() ? 3 : 1, context);
SpaceQuestion spaceQuestion = (SpaceQuestion) jobStatus.getQuestion();
// Set imported spaces.
Set<String> spaces = new HashSet<>();
Expand All @@ -117,8 +133,21 @@ public void updateAndSaveMigration(ConfluenceMigrationJobStatus jobStatus)

setLogRelatedFields(jobStatus, object, context);

context.getWiki().saveDocument(document, "Migration executed!", context);
} catch (XWikiException e) {
wiki.saveDocument(document, "Migration executed!", context);
} catch (Exception e) {
if (object != null) {
List<Map<String, Object>> logList = new ArrayList<>(1);
addToJsonList(LogLevel.ERROR, Instant.now().toEpochMilli(), AN_EXCEPTION_OCCURRED, e, logList);
object.set(LOGS, new Gson().toJson(logList), context);
object.set(EXECUTED, 4, context);
logger.error(AN_EXCEPTION_OCCURRED, e);
try {
wiki.saveDocument(document, "Migration failed", context);
} catch (XWikiException err) {
logger.error("Could not update the migration document [{}] with an error status",
statusDocumentReference, err);
}
}
}
}

Expand Down Expand Up @@ -232,7 +261,7 @@ private void setLogRelatedFields(ConfluenceMigrationJobStatus jobStatus, BaseObj
object.setLargeStringValue("otherIssues", gson.toJson(otherIssues));
object.setLargeStringValue("brokenLinksPages", gson.toJson(brokenLinksPages));
object.setLargeStringValue("brokenLinks", gson.toJson(brokenLinks));
object.set("logs", gson.toJson(logList), context);
object.set(LOGS, gson.toJson(logList), context);
object.setLongValue("imported", docCount);
}

Expand Down Expand Up @@ -371,14 +400,19 @@ public void enablePrerequisites()
prerequisitesManager.enablePrerequisites();
}

private void addToJsonList(LogEvent logEvent, List<Map<String, Object>> logList)
private void addToJsonList(LogEvent e, List<Map<String, Object>> logList)
{
addToJsonList(e.getLevel(), e.getTimeStamp(), e.getFormattedMessage(), e.getThrowable(), logList);
}

private void addToJsonList(LogLevel l, long timeStamp, String msg, Throwable t, List<Map<String, Object>> logList)
{
Map<String, Object> logMap = new HashMap<>();
logMap.put("level", logEvent.getLevel().toString());
logMap.put("timeStamp", logEvent.getTimeStamp());
logMap.put("message", logEvent.getFormattedMessage());
if (logEvent.getThrowable() != null) {
logMap.put("throwable", ExceptionUtils.getStackFrames(logEvent.getThrowable()));
logMap.put("level", l.toString());
logMap.put("timeStamp", timeStamp);
logMap.put("message", msg);
if (t != null) {
logMap.put("throwable", ExceptionUtils.getStackFrames(t));
}
logList.add(logMap);
}
Expand Down
Expand Up @@ -50,16 +50,16 @@
<customDisplay/>
<defaultValue/>
<disabled>0</disabled>
<displayFormType/>
<displayType/>
<hint/>
<name>executed</name>
<number>6</number>
<numberType>integer</numberType>
<prettyName>executed</prettyName>
<size>30</size>
<unmodifiable>0</unmodifiable>
<validationMessage/>
<validationRegExp/>
<classType>com.xpn.xwiki.objects.classes.BooleanClass</classType>
<classType>com.xpn.xwiki.objects.classes.NumberClass</classType>
</executed>
<imported>
<customDisplay/>
Expand Down

0 comments on commit f789abb

Please sign in to comment.