Skip to content

Commit

Permalink
[PEOV-75] Log error on cron task register, if tw-task is registered a…
Browse files Browse the repository at this point in the history
…nd in error state (#210)

* [PEOV-75] When registering cron tasks, log error if job already exists but task is in error state.

* [PEOV-75] Checkstyle

* [PEOV-75] Version bump + Changelog + Modify error log
  • Loading branch information
KaurKadakWise authored Oct 16, 2024
1 parent ef80f48 commit b576433
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

#### 1.44.0 - 2024/10/16

- When registering cron tasks, log error if job already exists, but task is in error state.
- If silent mode is turned on, then this log will not appear.

#### 1.43.0 - 2024/08/09

- Added support for task context
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=1.43.0
version=1.44.0
org.gradle.internal.http.socketTimeout=120000
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.google.common.base.Joiner;
import com.transferwise.common.gracefulshutdown.GracefulShutdownStrategy;
import com.transferwise.tasks.ITasksService;
import com.transferwise.tasks.dao.ITaskDao;
import com.transferwise.tasks.domain.FullTaskRecord;
import com.transferwise.tasks.domain.IBaseTask;
import com.transferwise.tasks.domain.TaskStatus;
import com.transferwise.tasks.impl.jobs.interfaces.IJob;
import com.transferwise.tasks.impl.jobs.interfaces.IJobsService;
import io.micrometer.core.instrument.Gauge;
Expand All @@ -28,6 +31,8 @@
@Slf4j
public class JobsService implements IJobsService, GracefulShutdownStrategy, InitializingBean {

@Autowired
private ITaskDao taskDao;
@Autowired
private ITasksService tasksService;
@Autowired
Expand Down Expand Up @@ -58,6 +63,22 @@ public void applicationStarted() {
initJobs(false);
}

@Override
public boolean canShutdown() {
return true;
}

@Override
public IJob getJobFor(IBaseTask task) {
String jobName = StringUtils.substringAfter(task.getType(), "|");
jobName = StringUtils.substringBefore(jobName, "|");
JobContainer jobContainer = cronTasksMap.get(jobName);
if (jobContainer == null) {
return null;
}
return jobContainer.job;
}

protected void initJobs(boolean silent) {
List<IJob> availableCronTasks = new ArrayList<>(applicationContext.getBeansOfType(IJob.class).values());

Expand All @@ -71,11 +92,6 @@ protected void initJobs(boolean silent) {
registerCronTasks(silent);
}

@Override
public boolean canShutdown() {
return true;
}

private void validateState() {
jobContainers.forEach(c -> {
if (c.getUniqueName() == null) {
Expand Down Expand Up @@ -133,7 +149,13 @@ protected void registerCronTasks(boolean silent) {
log.info("Job '{}' registered with task id '{}'. It will be run at {}.", jobContainer.getUniqueName(), cronTask.getTaskId(), nextRuntime);
}
} else {
if (jobsProperties.isTestMode() || silent) {
FullTaskRecord alreadyScheduledTask = taskDao.getTask(cronTask.getTaskId(), FullTaskRecord.class);

if (alreadyScheduledTask.getStatus().equals(TaskStatus.ERROR.name()) && !silent) {
log.error("Job '{}' was not registered with task id '{}', because the task already exists and is in ERROR state.",
jobContainer.getUniqueName(),
cronTask.getTaskId());
} else if (jobsProperties.isTestMode() || silent) {
// We don't want to see this every time a new test runs while tasks are not cleaned.
log.debug("Job '{}' was not registered with task id '{}', because the task already exists.", jobContainer.getUniqueName(),
cronTask.getTaskId());
Expand All @@ -145,17 +167,6 @@ protected void registerCronTasks(boolean silent) {
}
}

@Override
public IJob getJobFor(IBaseTask task) {
String jobName = StringUtils.substringAfter(task.getType(), "|");
jobName = StringUtils.substringBefore(jobName, "|");
JobContainer jobContainer = cronTasksMap.get(jobName);
if (jobContainer == null) {
return null;
}
return jobContainer.job;
}

@Data
@Accessors(chain = true)
private static class JobContainer {
Expand Down

0 comments on commit b576433

Please sign in to comment.