Skip to content

Commit

Permalink
fix(ut): new list for UT that needs to be ran without a transaction. …
Browse files Browse the repository at this point in the history
…Ref: #29501 (#30354)

This pull request includes changes to the `StartupTasksExecutor` and
`TaskLocatorUtil` classes to ensure that certain startup tasks are
executed without a transaction.
  • Loading branch information
erickgonzalez authored Oct 15, 2024
1 parent 57ad2c0 commit 38935f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,18 @@ public void executeStartUpTasks() throws DotDataException {
if (StartupTask.class.isAssignableFrom(c)) {
StartupTask task = (StartupTask) c.getDeclaredConstructor().newInstance();
if (task.forceRun()) {
HibernateUtil.startTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.startTransaction();
}
Logger.info(this, "Running Startup Tasks : " + name);
task.executeUpgrade();
} else {
Logger.info(this, "Not Running Startup Tasks: " + name);
}
}
HibernateUtil.closeAndCommitTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.closeAndCommitTransaction();
}
}
Logger.info(this, "Finishing startup tasks.");
} catch (Throwable e) {
Expand Down Expand Up @@ -250,7 +254,9 @@ public void executeSchemaUpgrades() throws DotDataException {
}

if (!firstTimeStart && task.forceRun()) {
HibernateUtil.startTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.startTransaction();
}
Logger.info(this, "Running Upgrade Tasks: " + name);
task.executeUpgrade();

Expand All @@ -262,7 +268,9 @@ public void executeSchemaUpgrades() throws DotDataException {
.addParam(new Date())
.loadResult();
Logger.info(this, "Database upgraded to version: " + taskId);
HibernateUtil.closeAndCommitTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.closeAndCommitTransaction();
}
Config.DB_VERSION = taskId;
}
} catch (Exception e) {
Expand Down Expand Up @@ -315,7 +323,9 @@ public void executeDataUpgrades() throws DotDataException {
}

if (!firstTimeStart && task.forceRun()) {
HibernateUtil.startTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.startTransaction();
}
Logger.info(this, "Running Data Upgrade Tasks: " + name);
task.executeUpgrade();
}
Expand All @@ -326,7 +336,9 @@ public void executeDataUpgrades() throws DotDataException {
.addParam(new Date())
.loadResult();
Logger.info(this, "Data upgraded to version: " + taskId);
HibernateUtil.closeAndCommitTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.closeAndCommitTransaction();
}
Config.DATA_VERSION = taskId;
}
} catch (Exception e) {
Expand Down Expand Up @@ -363,14 +375,18 @@ public void executeBackportedTasks() throws DotDataException {
if (StartupTask.class.isAssignableFrom(c) && taskId > Config.DB_VERSION) {
StartupTask task = (StartupTask) c.getDeclaredConstructor().newInstance();
if (task.forceRun()) {
HibernateUtil.startTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.startTransaction();
}
Logger.info(this, "Running Backported Tasks : " + name);
task.executeUpgrade();
} else {
Logger.info(this, "Not Running Backported Tasks: " + name);
}
}
HibernateUtil.closeAndCommitTransaction();
if(!TaskLocatorUtil.getTaskClassesNoTransaction().contains(c)){
HibernateUtil.closeAndCommitTransaction();
}
}
Logger.info(this, "Finishing Backported tasks.");
} catch (Throwable e) {
Expand Down
10 changes: 10 additions & 0 deletions dotCMS/src/main/java/com/dotmarketing/util/TaskLocatorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,14 @@ public static List<Class<?>> getStartupRunOnceDataTaskClasses() {
return ret.stream().sorted(classNameComparator).collect(Collectors.toList());
}

/**
* List of tasks that are need to run without transaction. It can be all kind: Run-Once, Run-Always, Data, Backported, etc
* @return The list of tasks
*/
public static List<Class<?>> getTaskClassesNoTransaction() {
final List<Class<?>> ret = new ArrayList<>();
ret.add(Task241014AddTemplateValueOnContentletIndex.class);
return ret.stream().sorted(classNameComparator).collect(Collectors.toList());
}

}

0 comments on commit 38935f7

Please sign in to comment.