Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) enable Spring Batch support, add a batch to backup users and m…
…ake some stats
- Loading branch information
1 parent
4fa667d
commit c0e3422
Showing
38 changed files
with
970 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package manon.app.batch.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import manon.app.batch.err.TaskNotFoundException; | ||
import manon.app.batch.model.TaskStatus; | ||
import manon.app.batch.service.TaskRunnerService; | ||
import manon.app.security.UserSimpleDetails; | ||
import org.springframework.batch.core.ExitStatus; | ||
import org.springframework.batch.core.JobParametersInvalidException; | ||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; | ||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; | ||
import org.springframework.batch.core.repository.JobRestartException; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static manon.app.config.API.API_SYS; | ||
|
||
/** Task API. */ | ||
@RestController | ||
@RequestMapping(value = API_SYS) | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class TaskRunnerWS { | ||
|
||
private final TaskRunnerService taskRunnerService; | ||
|
||
@PostMapping(value = "/batch/start/{task}") | ||
public TaskStatus startTask(@AuthenticationPrincipal UserSimpleDetails sys, @PathVariable("task") String task) | ||
throws TaskNotFoundException, JobParametersInvalidException, JobExecutionAlreadyRunningException, | ||
JobRestartException, JobInstanceAlreadyCompleteException { | ||
log.warn("admin {} starts task {}", sys.getUsername(), task); | ||
ExitStatus exitStatus = taskRunnerService.run(task); | ||
return TaskStatus.builder() | ||
.running(exitStatus.isRunning()) | ||
.exitCode(exitStatus.getExitCode()) | ||
.exitDescription(exitStatus.getExitDescription()) | ||
.build(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/manon/app/batch/err/BatchControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package manon.app.batch.err; | ||
|
||
import manon.app.config.ControllerAdviceBase; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
import java.util.Map; | ||
|
||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
@ControllerAdvice | ||
public class BatchControllerAdvice extends ControllerAdviceBase { | ||
|
||
@ExceptionHandler(TaskNotFoundException.class) | ||
@ResponseStatus(NOT_FOUND) | ||
@ResponseBody | ||
public Map<String, Object> handle(TaskNotFoundException error) { | ||
Map<String, Object> map = error(); | ||
map.put(FIELD_ERRORS, error.getClass().getSimpleName()); | ||
map.put(FIELD_MESSAGE, error.getName()); | ||
return map; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/manon/app/batch/err/TaskNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package manon.app.batch.err; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@SuppressWarnings("serial") | ||
@AllArgsConstructor | ||
@Getter | ||
public class TaskNotFoundException extends Exception { | ||
|
||
private final String name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package manon.app.batch.listener; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.listener.JobExecutionListenerSupport; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class TaskListener extends JobExecutionListenerSupport { | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
super.afterJob(jobExecution); | ||
log.info("job {} ended in state {}", | ||
jobExecution.getJobInstance().getJobName(), | ||
jobExecution.getStatus()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package manon.app.batch.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@EqualsAndHashCode | ||
@Getter | ||
@ToString | ||
@Builder(toBuilder = true) | ||
public class TaskStatus { | ||
|
||
private boolean running; | ||
private String exitCode; | ||
private String exitDescription; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/manon/app/batch/service/TaskRunnerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package manon.app.batch.service; | ||
|
||
import manon.app.batch.err.TaskNotFoundException; | ||
import org.springframework.batch.core.ExitStatus; | ||
import org.springframework.batch.core.JobParametersInvalidException; | ||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; | ||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; | ||
import org.springframework.batch.core.repository.JobRestartException; | ||
|
||
public interface TaskRunnerService { | ||
|
||
ExitStatus run(String task) throws TaskNotFoundException, | ||
JobParametersInvalidException, JobExecutionAlreadyRunningException, | ||
JobRestartException, JobInstanceAlreadyCompleteException; | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/manon/app/batch/service/TaskRunnerServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package manon.app.batch.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import manon.app.batch.err.TaskNotFoundException; | ||
import manon.user.batch.UserSnapshotTask; | ||
import org.springframework.batch.core.ExitStatus; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
import org.springframework.batch.core.JobParametersInvalidException; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; | ||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; | ||
import org.springframework.batch.core.repository.JobRestartException; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class TaskRunnerServiceImpl implements InitializingBean, TaskRunnerService { | ||
|
||
private final JobLauncher launcher; | ||
|
||
@Qualifier(UserSnapshotTask.JOB_NAME) | ||
private final Job userSnapshotJob; | ||
|
||
private Map<String, Job> jobs; | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
this.jobs = Collections.singletonMap(UserSnapshotTask.JOB_NAME, userSnapshotJob); | ||
} | ||
|
||
@Override | ||
public ExitStatus run(String task) throws TaskNotFoundException, | ||
JobParametersInvalidException, JobExecutionAlreadyRunningException, | ||
JobRestartException, JobInstanceAlreadyCompleteException { | ||
if (jobs.containsKey(task)) { | ||
return launcher.run(jobs.get(task), todayDateJobParameters()).getExitStatus(); | ||
} | ||
throw new TaskNotFoundException(task); | ||
} | ||
|
||
private JobParameters todayDateJobParameters() { | ||
return new JobParametersBuilder().addDate("START_DATE", new Date()).toJobParameters(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.