Commit 045f591d authored by Enriko Käsper's avatar Enriko Käsper
Browse files

Merge branch 'develop' into 'master'

Release: merge 'develop' into 'master' created by Enriko Käsper

See merge request teis/scheduler-service!13
parents 69a840ab 5313bb5f
Subproject commit 4cc0c5b4d4bdbda2ae6a33ba337e3be218799eeb
Subproject commit ea6a5ec3ed936121c971ce84c1cc87ade857c3e1
theGroup=ee.sm.ti.teis
theVersion=1.3.0
theVersion=1.4.0
commonsVersion=1.4.0
commonApiGatewayVersion=1.4.0
adminApiGatewayVersion=1.3.0
pluginVersion=1.0.0
pluginVersion=1.0.1
mapstructVersion=1.3.0.Final
......@@ -7,7 +7,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
......@@ -22,10 +21,9 @@ import java.util.UUID;
@ActiveProfiles("integtest")
@Testcontainers
@ContextConfiguration(initializers = {SchedulerAppTestBase.Initializer.class})
@DirtiesContext
public abstract class SchedulerAppTestBase {
private static GenericContainer rabbitMQContainer;
private static final GenericContainer rabbitMQContainer;
static {
rabbitMQContainer = new GenericContainer("rabbitmq:latest").withExposedPorts(5672);
......
package ee.sm.ti.teis.scheduler.admin;
import ee.sm.ti.teis.admingateway.systemjobs.JobExecutionDto;
import ee.sm.ti.teis.exceptions.TeisBusinessException;
import ee.sm.ti.teis.exceptions.TeisResourceNotFoundException;
......@@ -9,27 +8,19 @@ import ee.sm.ti.teis.scheduler.job.execution.JobExecutionEntity;
import ee.sm.ti.teis.scheduler.job.execution.JobExecutionMapper;
import ee.sm.ti.teis.scheduler.job.execution.JobExecutionRepository;
import ee.sm.ti.teis.scheduler.processor.JobLauncherService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class AdminBusinessService {
private final JobLauncherService jobLauncherService;
private final JobExecutionMapper jobExecutionMapper;
private final JobExecutionRepository jobExecutionRepository;
private final JobDefinitionRepository jobDefinitionRepository;
public AdminBusinessService(JobLauncherService jobLauncherService,
JobExecutionMapper jobExecutionMapper,
JobExecutionRepository jobExecutionRepository,
JobDefinitionRepository jobDefinitionRepository) {
this.jobLauncherService = jobLauncherService;
this.jobExecutionMapper = jobExecutionMapper;
this.jobExecutionRepository = jobExecutionRepository;
this.jobDefinitionRepository = jobDefinitionRepository;
}
public void startSystemJob(String jobId) {
if (jobLauncherService.isAnyProcessRunning(jobId)) {
throw new TeisBusinessException(SystemjobErrorCode.JOB_ALREADY_STARTED, "The process is already running. JobId=" + jobId);
......
package ee.sm.ti.teis.scheduler.admin;
import ee.sm.ti.teis.admingateway.systemjobs.GetSystemJobsRequest;
import ee.sm.ti.teis.admingateway.systemjobs.GetSystemJobsResponse;
import ee.sm.ti.teis.admingateway.systemjobs.JobExecutionDto;
import ee.sm.ti.teis.admingateway.systemjobs.StartSystemJobRequest;
import ee.sm.ti.teis.admingateway.systemjobs.StartSystemJobResponse;
import ee.sm.ti.teis.admingateway.systemjobs.SystemJobDto;
import ee.sm.ti.teis.admingateway.systemjobs.*;
import ee.sm.ti.teis.scheduler.config.QueueConfig;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
......@@ -15,15 +10,11 @@ import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Slf4j
@RequiredArgsConstructor
public class AdminJobListener {
private final AdminBusinessService adminBusinessService;
public AdminJobListener(AdminBusinessService adminBusinessService) {
this.adminBusinessService = adminBusinessService;
}
@RabbitListener(queues = {QueueConfig.GW_START_ADMIN_JOB})
@PreAuthorize("@accessController.hasAnyPrivilege({'TI_MANAGE_SYSTEM_JOBS'}, null, null, null)")
public StartSystemJobResponse jobStarted(StartSystemJobRequest request) {
......
package ee.sm.ti.teis.scheduler.config;
import ee.sm.ti.teis.scheduler.processor.TriggerInitializerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class JobsTriggerInitializer {
......
......@@ -13,12 +13,12 @@ import java.util.ArrayList;
import static java.util.OptionalInt.of;
@Configuration
public class QueueConfig implements QueueList {
public static final String MS_SYSTEM_JOB_STARTED_QUEUE = "scheduler-service.ms.systemJobStartedEvent";
public static final String MS_SYSTEM_JOB_FINISHED_QUEUE = "scheduler-service.ms.systemJobFinishedEvent";
public static final String GW_START_ADMIN_JOB = "scheduler-service.gw.startSystemJobRequest";
public static final String GW_GET_JOB_EXECUTIONS_JOB = "scheduler-service.gw.getSystemJobExecutionsRequest";
......@@ -27,11 +27,12 @@ public class QueueConfig implements QueueList {
@Override
public void updateQueues(ArrayList<TeisQueue> queues) {
String queueName = "scheduler-service" + TestDataImportService.RESET_TEST_DATA_QUEUE_SUFIX;
addGwQueue(queues, "gwResetTestData", TestDataImportService.RESET_TEST_DATA_ROUTING_KEY, queueName);
addMsQueue(queues, "msSystemJobStartedQueue", new SystemJobStartedRequest().routingKey(), MS_SYSTEM_JOB_STARTED_QUEUE, of(JOBS_EVENT_QUEUE_TTL));
addMsQueue(queues, "msSystemJobFinishedQueue", new SystemJobFinishedRequest().routingKey(), MS_SYSTEM_JOB_FINISHED_QUEUE, of(JOBS_EVENT_QUEUE_TTL));
addGwQueue(queues, "gwStartSystemJobQueue", new StartSystemJobRequest().routingKey(), GW_START_ADMIN_JOB);
addGwQueue(queues, "geGetSystemJobExecutionsQueue", new GetSystemJobsRequest().routingKey(), GW_GET_JOB_EXECUTIONS_JOB);
addGwQueue(queues, "gwGetSystemJobExecutionsQueue", new GetSystemJobsRequest().routingKey(), GW_GET_JOB_EXECUTIONS_JOB);
addGwQueue(queues, "gwResetTestData", TestDataImportService.RESET_TEST_DATA_ROUTING_KEY, queueName);
}
}
package ee.sm.ti.teis.scheduler.job.definition;
import ee.sm.ti.teis.exceptions.TeisResourceNotFoundException;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -12,15 +12,11 @@ import static java.util.List.of;
@Service
@Validated
@Slf4j
@RequiredArgsConstructor
public class JobDefinitionService {
private final JobDefinitionRepository jobDefinitionRepository;
public JobDefinitionService(JobDefinitionRepository jobDefinitionRepository) {
this.jobDefinitionRepository = jobDefinitionRepository;
}
public JobDefinitionEntity getJob(String id) {
return jobDefinitionRepository.findById(id)
.orElseThrow(() -> new TeisResourceNotFoundException("Job not found", of(id)));
......
package ee.sm.ti.teis.scheduler.job.definition;
import ee.sm.ti.teis.scheduler.processor.ScheduledJobLauncher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.utils.Key;
......@@ -14,15 +15,12 @@ import static org.quartz.impl.matchers.GroupMatcher.groupEquals;
@Service
@Slf4j
@RequiredArgsConstructor
public class JobSchedulerService {
private static final String DEFAULT_GROUP_NAME = "teis-system";
private final Scheduler scheduler;
public JobSchedulerService(Scheduler scheduler) {
this.scheduler = scheduler;
}
private static ScheduleBuilder createSchedule(String cronExpression) {
return CronScheduleBuilder.cronSchedule(cronExpression);
}
......
package ee.sm.ti.teis.scheduler.job.execution;
import ee.sm.ti.teis.exceptions.TeisResourceNotFoundException;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -14,15 +14,11 @@ import static java.util.List.of;
@Service
@Validated
@Slf4j
@RequiredArgsConstructor
public class JobExecutionService {
private final JobExecutionRepository jobExecutionRepository;
public JobExecutionService(JobExecutionRepository jobExecutionRepository) {
this.jobExecutionRepository = jobExecutionRepository;
}
public JobExecutionEntity getExecution(UUID id) {
return jobExecutionRepository.findById(id)
.orElseThrow(() -> new TeisResourceNotFoundException("Job not found", of(id)));
......
package ee.sm.ti.teis.scheduler.job.initlock;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
......@@ -10,16 +11,13 @@ import java.util.Optional;
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class JobInitLockService {
private static final Integer LOCK_ID = 1;
private static final Integer LOCK_TIMEOUT_SECONDS = 120;
private final JobInitLockRepository jobInitLockRepository;
public JobInitLockService(JobInitLockRepository jobInitLockRepository) {
this.jobInitLockRepository = jobInitLockRepository;
}
public boolean isInitLocked() {
Optional<JobInitLockEntity> lock = jobInitLockRepository.findById(LOCK_ID);
if (lock.isPresent() && lock.get().getLockedAt().isBefore(LocalDateTime.now().minusSeconds(LOCK_TIMEOUT_SECONDS))) {
......
......@@ -4,6 +4,7 @@ import ee.sm.ti.teis.exceptions.TeisIllegalArgumentException;
import ee.sm.ti.teis.scheduler.domain.SystemJob;
import ee.sm.ti.teis.scheduler.job.definition.JobDefinitionEntity;
import ee.sm.ti.teis.scheduler.job.definition.JobDefinitionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RuntimeService;
import org.springframework.retry.annotation.Backoff;
......@@ -20,16 +21,12 @@ import static ee.sm.ti.teis.scheduler.processor.camunda.JobProcessInstanceContex
@Service
@Slf4j
@Transactional
@RequiredArgsConstructor
public class JobLauncherService {
private final RuntimeService camundaRuntimeService;
private final JobDefinitionService jobDefinitionService;
public JobLauncherService(RuntimeService camundaRuntimeService, JobDefinitionService jobDefinitionService) {
this.camundaRuntimeService = camundaRuntimeService;
this.jobDefinitionService = jobDefinitionService;
}
@Retryable(
value = {TeisIllegalArgumentException.class},
maxAttempts = 10,
......
......@@ -8,6 +8,7 @@ import ee.sm.ti.teis.scheduler.job.execution.JobExecutionMapper;
import ee.sm.ti.teis.scheduler.job.execution.JobExecutionService;
import ee.sm.ti.teis.types.enums.SystemJobResponseCode;
import ee.sm.ti.teis.types.enums.SystemJobStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
......@@ -20,6 +21,7 @@ import static java.time.LocalDateTime.now;
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class JobProcessingService {
private final JobDefinitionService jobDefinitionService;
......@@ -27,15 +29,6 @@ public class JobProcessingService {
private final JobExecutionMapper jobExecutionMapper;
private final SystemJobNotificationService systemJobNotificationService;
public JobProcessingService(JobDefinitionService jobDefinitionService,
JobExecutionService jobExecutionService, JobExecutionMapper jobExecutionMapper,
SystemJobNotificationService systemJobNotificationService) {
this.jobDefinitionService = jobDefinitionService;
this.jobExecutionService = jobExecutionService;
this.jobExecutionMapper = jobExecutionMapper;
this.systemJobNotificationService = systemJobNotificationService;
}
public void startJob(SystemJob systemJob) {
JobDefinitionEntity jobEntity = jobDefinitionService.getJob(systemJob.getId());
jobEntity.setLastStartedAt(now());
......
package ee.sm.ti.teis.scheduler.processor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
......@@ -7,17 +8,13 @@ import org.springframework.stereotype.Component;
@Component
@Slf4j
@RequiredArgsConstructor
public class ScheduledJobLauncher extends QuartzJobBean {
private final JobLauncherService jobLauncherService;
public ScheduledJobLauncher(JobLauncherService jobLauncherService) {
this.jobLauncherService = jobLauncherService;
}
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
log.info("Launch Job with jobId='{}'", jobExecutionContext.getJobDetail().getKey());
jobLauncherService.launchJob(jobExecutionContext.getJobDetail().getKey().getName());
}
......
......@@ -6,6 +6,7 @@ import ee.sm.ti.teis.scheduler.domain.SystemJob;
import ee.sm.ti.teis.scheduler.message.SystemJobFinishedRequest;
import ee.sm.ti.teis.scheduler.message.SystemJobStartedRequest;
import ee.sm.ti.teis.scheduler.processor.camunda.MessageCorrelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.MismatchingMessageCorrelationException;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
......@@ -16,14 +17,11 @@ import static java.text.MessageFormat.format;
@Component
@Slf4j
@RequiredArgsConstructor
public class SystemJobEventsListener {
private final MessageCorrelationService messageCorrelationService;
public SystemJobEventsListener(MessageCorrelationService messageCorrelationService) {
this.messageCorrelationService = messageCorrelationService;
}
@RabbitListener(queues = {QueueConfig.MS_SYSTEM_JOB_STARTED_QUEUE})
public void jobStarted(SystemJobStartedRequest request) {
processMessage(JOB_STARTED_MESSAGE_ID, request.getPayload());
......
......@@ -6,6 +6,7 @@ import ee.sm.ti.teis.scheduler.domain.SystemJob;
import ee.sm.ti.teis.scheduler.message.ExecuteSystemJobStartRequest;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.servicerequest.UserType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
......@@ -13,6 +14,7 @@ import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class SystemJobNotificationService {
@Value("${teis.system.name}")
......@@ -20,10 +22,6 @@ public class SystemJobNotificationService {
private final ApplicationEventPublisher eventPublisher;
public SystemJobNotificationService(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
void notifyExecuteJob(SystemJob payload) {
SystemJobStartExecutionMessageDto requestDTO = new ExecuteSystemJobStartRequest();
requestDTO.setPayload(payload, createRequestMeta());
......
......@@ -4,6 +4,7 @@ import ee.sm.ti.teis.scheduler.job.definition.JobDefinitionEntity;
import ee.sm.ti.teis.scheduler.job.definition.JobDefinitionService;
import ee.sm.ti.teis.scheduler.job.definition.JobSchedulerService;
import ee.sm.ti.teis.scheduler.job.initlock.JobInitLockService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
......@@ -13,20 +14,13 @@ import java.util.Set;
@Service
@Slf4j
@RequiredArgsConstructor
public class TriggerInitializerService {
private final JobDefinitionService jobDefinitionService;
private final JobSchedulerService jobSchedulerService;
private final JobInitLockService jobInitLockService;
public TriggerInitializerService(JobDefinitionService jobDefinitionService,
JobSchedulerService jobSchedulerService,
JobInitLockService jobInitLockService) {
this.jobDefinitionService = jobDefinitionService;
this.jobSchedulerService = jobSchedulerService;
this.jobInitLockService = jobInitLockService;
}
public void initJobs() {
log.info("Initialize scheduled jobs");
......
......@@ -2,6 +2,7 @@ package ee.sm.ti.teis.scheduler.processor.camunda;
import ee.sm.ti.teis.scheduler.domain.SystemJob;
import ee.sm.ti.teis.scheduler.processor.JobProcessingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
......@@ -11,14 +12,11 @@ import static ee.sm.ti.teis.scheduler.processor.camunda.JobProcessInstanceContex
@Slf4j
@Component
@RequiredArgsConstructor
public class JobFinishedTaskService implements JavaDelegate {
private final JobProcessingService jobProcessingService;
public JobFinishedTaskService(JobProcessingService jobProcessingService) {
this.jobProcessingService = jobProcessingService;
}
@Override
public void execute(DelegateExecution execution) {
String businessKey = execution.getBusinessKey();
......
......@@ -2,6 +2,7 @@ package ee.sm.ti.teis.scheduler.processor.camunda;
import ee.sm.ti.teis.scheduler.domain.SystemJob;
import ee.sm.ti.teis.scheduler.processor.JobProcessingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
......@@ -11,13 +12,10 @@ import static ee.sm.ti.teis.scheduler.processor.camunda.JobProcessInstanceContex
@Slf4j
@Component
@RequiredArgsConstructor
public class JobStartedTaskService implements JavaDelegate {
private final JobProcessingService jobProcessingService;
public JobStartedTaskService(JobProcessingService jobProcessingService) {
this.jobProcessingService = jobProcessingService;
}
@Override
public void execute(DelegateExecution execution) {
String businessKey = execution.getBusinessKey();
......
......@@ -2,6 +2,7 @@ package ee.sm.ti.teis.scheduler.processor.camunda;
import ee.sm.ti.teis.exceptions.TeisIllegalArgumentException;
import ee.sm.ti.teis.scheduler.domain.SystemJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.MismatchingMessageCorrelationException;
import org.camunda.bpm.engine.RuntimeService;
......@@ -15,14 +16,11 @@ import static java.text.MessageFormat.format;
@Service
@Slf4j
@RequiredArgsConstructor
public class MessageCorrelationService {
private final RuntimeService camundaRuntimeService;
public MessageCorrelationService(RuntimeService camundaRuntimeService) {
this.camundaRuntimeService = camundaRuntimeService;
}
@Retryable(
value = {MismatchingMessageCorrelationException.class, TeisIllegalArgumentException.class},
maxAttempts = 10,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment