Log steps for updates

This commit is contained in:
René Pfeuffer
2019-05-15 13:29:50 +02:00
parent 93dbaeae7c
commit ed7c2a01e7
4 changed files with 52 additions and 8 deletions

View File

@@ -0,0 +1,11 @@
package sonia.scm.migration;
public class UpdateException extends RuntimeException {
public UpdateException(String message) {
super(message);
}
public UpdateException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -5,9 +5,9 @@ import sonia.scm.version.Version;
@ExtensionPoint @ExtensionPoint
public interface UpdateStep { public interface UpdateStep {
void doUpdate(); void doUpdate() throws Exception;
Version getTargetVersion(); Version getTargetVersion();
String affectedDataType(); String getAffectedDataType();
} }

View File

@@ -1,5 +1,8 @@
package sonia.scm.update; package sonia.scm.update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.migration.UpdateException;
import sonia.scm.migration.UpdateStep; import sonia.scm.migration.UpdateStep;
import sonia.scm.store.ConfigurationEntryStore; import sonia.scm.store.ConfigurationEntryStore;
import sonia.scm.store.ConfigurationEntryStoreFactory; import sonia.scm.store.ConfigurationEntryStoreFactory;
@@ -13,6 +16,8 @@ import static java.util.stream.Collectors.toList;
public class UpdateEngine { public class UpdateEngine {
public static final Logger LOG = LoggerFactory.getLogger(UpdateEngine.class);
private static final String STORE_NAME = "executedUpdates"; private static final String STORE_NAME = "executedUpdates";
private final List<UpdateStep> steps; private final List<UpdateStep> steps;
@@ -25,9 +30,12 @@ public class UpdateEngine {
} }
private List<UpdateStep> sortSteps(Set<UpdateStep> steps) { private List<UpdateStep> sortSteps(Set<UpdateStep> steps) {
return steps.stream() LOG.trace("sorting available update steps:");
List<UpdateStep> sortedSteps = steps.stream()
.sorted(Comparator.comparing(UpdateStep::getTargetVersion).reversed()) .sorted(Comparator.comparing(UpdateStep::getTargetVersion).reversed())
.collect(toList()); .collect(toList());
sortedSteps.forEach(step -> LOG.trace("{} for version {}", step.getAffectedDataType(), step.getTargetVersion()));
return sortedSteps;
} }
public void update() { public void update() {
@@ -38,16 +46,41 @@ public class UpdateEngine {
} }
private void execute(UpdateStep updateStep) { private void execute(UpdateStep updateStep) {
updateStep.doUpdate(); try {
LOG.info("running update step for type {} and version {}",
updateStep.getAffectedDataType(),
updateStep.getTargetVersion()
);
updateStep.doUpdate();
} catch (Exception e) {
throw new UpdateException(
String.format(
"could not execute update for type %s to version %s in class %s",
updateStep.getAffectedDataType(),
updateStep.getTargetVersion(),
updateStep.getClass()),
e);
}
UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion()); UpdateVersionInfo newVersionInfo = new UpdateVersionInfo(updateStep.getTargetVersion().getParsedVersion());
store.put(updateStep.affectedDataType(), newVersionInfo); store.put(updateStep.getAffectedDataType(), newVersionInfo);
} }
private boolean notRunYet(UpdateStep updateStep) { private boolean notRunYet(UpdateStep updateStep) {
UpdateVersionInfo updateVersionInfo = store.get(updateStep.affectedDataType()); LOG.trace("checking whether to run update step for type {} and version {}",
updateStep.getAffectedDataType(),
updateStep.getTargetVersion()
);
UpdateVersionInfo updateVersionInfo = store.get(updateStep.getAffectedDataType());
if (updateVersionInfo == null) { if (updateVersionInfo == null) {
LOG.trace("no updates for type {} run yet; step will be executed", updateStep.getAffectedDataType());
return true; return true;
} }
return updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion()); boolean result = updateStep.getTargetVersion().isNewer(updateVersionInfo.getLatestVersion());
LOG.trace("latest version for type {}: {}; step will be executed: {}",
updateStep.getAffectedDataType(),
updateVersionInfo.getLatestVersion(),
result
);
return result;
} }
} }

View File

@@ -85,7 +85,7 @@ class UpdateEngineTest {
} }
@Override @Override
public String affectedDataType() { public String getAffectedDataType() {
return type; return type;
} }