Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PEOV-75] Log error on cron task register, if tw-task is registered and in error state #210

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading