Skip to content

Commit

Permalink
[SYNCOPE-1850] Checking concurrent task execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Jan 3, 2025
1 parent 02f9e32 commit db0703a
Show file tree
Hide file tree
Showing 26 changed files with 171 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/crosschecks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dockerhub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Elasticsearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_OpenSearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Payara.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Tomcat_PostgreSQL_JSON.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Tomcat_PostgreSQL_XML.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Tomcat_PostgreSQL_YAML.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_WA_OIDCC4UI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_WA_SAML2SP4UI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_WA_SRA_CASClient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_WA_SRA_OAuth2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_WA_SRA_OIDC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_WA_SRA_SAML2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Wildfly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fit_Zookeeper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/neo4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/oracle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup Maven
uses: stCarolas/setup-maven@v5
with:
maven-version: 3.9.9
maven-version: 3.9.6
- uses: actions/cache@v4
with:
path: ~/.m2/repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ public DefaultJobManager(

@Override
public boolean isRunning(final String jobName) {
boolean locked = jobStatusDAO.lock(jobName);
if (locked) {
jobStatusDAO.unlock(jobName);
synchronized (jobName) {
boolean locked = jobStatusDAO.lock(jobName);
if (locked) {
jobStatusDAO.unlock(jobName);
}
return !locked;
}
return !locked;
}

protected void registerJob(
Expand Down Expand Up @@ -247,28 +249,21 @@ public int getOrder() {
@Transactional
@Override
public void load(final String domain) {
String notificationJobCronExp = AuthContextUtils.callAsAdmin(SyncopeConstants.MASTER_DOMAIN, () -> {
String result = StringUtils.EMPTY;

String conf = confParamOps.get(
SyncopeConstants.MASTER_DOMAIN, "notificationjob.cronExpression", null, String.class);
if (conf == null) {
result = NotificationJob.DEFAULT_CRON_EXP;
} else if (!StringUtils.EMPTY.equals(conf)) {
result = conf;
}
return result;
});

AuthContextUtils.runAsAdmin(domain, () -> {
// 1. jobs for SchedTasks
Set<SchedTask> tasks = new HashSet<>(taskDAO.<SchedTask>findAll(TaskType.SCHEDULED));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.PULL));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.PUSH));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.MACRO));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.LIVE_SYNC));

boolean loadException = false;
for (Iterator<SchedTask> it = tasks.iterator(); it.hasNext() && !loadException;) {
SchedTask task = it.next();

LOG.debug("Loading job for {} Task {} {}",
taskUtilsFactory.getInstance(task).getType(), task.getKey(), task.getName());

try {
register(domain, task, task.getStartAt(), securityProperties.getAdminUser(), false, Map.of());
} catch (Exception e) {
Expand All @@ -278,11 +273,14 @@ public void load(final String domain) {
}

if (loadException) {
LOG.debug("Errors while loading job instances for tasks, aborting");
LOG.error("Errors while loading job for tasks, aborting");
} else {
// 2. jobs for Reports
for (Iterator<? extends Report> it = reportDAO.findAll().iterator(); it.hasNext() && !loadException;) {
Report report = it.next();

LOG.debug("Loading job for Report {} {}", report.getKey(), report.getName());

try {
register(domain, report, null, securityProperties.getAdminUser(), false);
} catch (Exception e) {
Expand All @@ -292,12 +290,25 @@ public void load(final String domain) {
}

if (loadException) {
LOG.debug("Errors while loading job instances for reports, aborting");
LOG.error("Errors while loading job for reports, aborting");
}
}
});

if (SyncopeConstants.MASTER_DOMAIN.equals(domain)) {
String notificationJobCronExp = AuthContextUtils.callAsAdmin(SyncopeConstants.MASTER_DOMAIN, () -> {
String result = StringUtils.EMPTY;

String conf = confParamOps.get(
SyncopeConstants.MASTER_DOMAIN, "notificationjob.cronExpression", null, String.class);
if (conf == null) {
result = NotificationJob.DEFAULT_CRON_EXP;
} else if (!StringUtils.EMPTY.equals(conf)) {
result = conf;
}
return result;
});

// 3. NotificationJob
if (StringUtils.isBlank(notificationJobCronExp)) {
LOG.debug("Empty value provided for {}'s cron, not scheduling", NotificationJob.class.getSimpleName());
Expand Down Expand Up @@ -348,21 +359,28 @@ public void unload(final String domain) {
Set<SchedTask> tasks = new HashSet<>(taskDAO.<SchedTask>findAll(TaskType.SCHEDULED));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.PULL));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.PUSH));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.MACRO));
tasks.addAll(taskDAO.<SchedTask>findAll(TaskType.LIVE_SYNC));

tasks.forEach(task -> {
LOG.debug("Unloading job for {} Task {} {}",
taskUtilsFactory.getInstance(task).getType(), task.getKey(), task.getName());

try {
unregister(task);
} catch (Exception e) {
LOG.error("While unloading job instance for task {}", task.getKey(), e);
LOG.error("While unloading job for task {}", task.getKey(), e);
}
});

// 2. jobs for Reports
reportDAO.findAll().forEach(report -> {
LOG.debug("Unloading job for Report {} {}", report.getKey(), report.getName());

try {
unregister(report);
} catch (Exception e) {
LOG.error("While unloading job instance for report {}", report.getName(), e);
LOG.error("While unloading job for report {}", report.getName(), e);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,27 @@ public void run() {
AuthContextUtils.getWho(),
false)));

if (AuthContextUtils.callAsAdmin(context.getDomain(), () -> jobStatusDAO.lock(context.getJobName()))) {
LOG.debug("Job {} locked, starting execution", context.getJobName());
boolean locked = false;
try {
locked = AuthContextUtils.callAsAdmin(context.getDomain(), () -> jobStatusDAO.lock(context.getJobName()));
} catch (Exception e) {
LOG.debug("While attempting to lock job {}", context.getJobName(), e);
}
if (!locked) {
LOG.debug("Could not lock job {}, skipping execution", context.getJobName());
return;
}

LOG.debug("Job {} locked, starting execution", context.getJobName());

try {
execute(context);
} catch (JobExecutionException e) {
LOG.error("While executing job {}", context.getJobName(), e);
} finally {
LOG.debug("Job {} execution completed", context.getJobName());

try {
execute(context);
} catch (JobExecutionException e) {
LOG.error("While executing job {}", context.getJobName(), e);
} finally {
AuthContextUtils.runAsAdmin(context.getDomain(), () -> jobStatusDAO.unlock(context.getJobName()));
}
} else {
LOG.info("Could not lock job {}, skipping execution", context.getJobName());
AuthContextUtils.runAsAdmin(context.getDomain(), () -> jobStatusDAO.unlock(context.getJobName()));
}
}
}
Loading

0 comments on commit db0703a

Please sign in to comment.