diff --git a/src/main/java/org/onedatashare/transferservice/odstransferservice/controller/TransferController.java b/src/main/java/org/onedatashare/transferservice/odstransferservice/controller/TransferController.java index fcb2bce3..ac76457e 100644 --- a/src/main/java/org/onedatashare/transferservice/odstransferservice/controller/TransferController.java +++ b/src/main/java/org/onedatashare/transferservice/odstransferservice/controller/TransferController.java @@ -8,11 +8,10 @@ import org.onedatashare.transferservice.odstransferservice.service.VfsExpander; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.batch.core.Job; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.*; +import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.JobOperator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -48,6 +47,20 @@ public class TransferController { @Autowired VfsExpander vfsExpander; + @Autowired + JobExplorer jobExplorer; + + @Autowired + JobOperator jobOperator; + + Set jobIds; + + Long pausedJobId; + + public TransferController(Set jobIds){ + this.jobIds = jobIds; + } + @RequestMapping(value = "/start", method = RequestMethod.POST) @Async public ResponseEntity start(@RequestBody TransferJobRequest request) throws Exception { @@ -60,7 +73,52 @@ public ResponseEntity start(@RequestBody TransferJobRequest request) thr jc.setRequest(request); Job job = jc.concurrentJobDefinition(); JobExecution jobExecution = asyncJobLauncher.run(job, parameters); + this.jobIds.add(jobExecution.getJobId()); return ResponseEntity.status(HttpStatus.OK).body("Your batch job has been submitted with \n ID: " + jobExecution.getJobId()); } + + @RequestMapping(value = "/pause", method = RequestMethod.POST) + @Async + public ResponseEntity pause() throws Exception{ + logger.info("Pause Controller Entry point"); + Long runningJobId = null; + for(Long jobId : jobIds){ + JobExecution jobExecution = jobExplorer.getJobExecution(jobId); + if(jobExecution != null && jobExecution.isRunning()){ + runningJobId = jobId; + break; + } + } + + if(runningJobId == null){ + return ResponseEntity.status(HttpStatus.OK).body("No running job found"); + } + + pausedJobId = runningJobId; + jobOperator.stop(pausedJobId); + + return ResponseEntity.status(HttpStatus.OK).body("Your batch job with id "+runningJobId+"has been paused"); + } + + @RequestMapping(value = "/resume", method = RequestMethod.POST) + @Async + public ResponseEntity resume() throws Exception{ + logger.info("Pause Controller Entry point"); + if(pausedJobId == null){ + return ResponseEntity.status(HttpStatus.OK).body("No paused job found"); + } + + JobExecution jobExecution = jobExplorer.getJobExecution(pausedJobId); + if(jobExecution == null || !(jobExecution.getStatus().equals(BatchStatus.STOPPING) || jobExecution.getStatus().equals(BatchStatus.STOPPED))){ + return ResponseEntity.status(HttpStatus.OK).body("No paused job found with "+pausedJobId+" jobId"); + } + + jobOperator.restart(pausedJobId); + + Long resumedJobId = pausedJobId; + pausedJobId = null; + return ResponseEntity.status(HttpStatus.OK).body("Your batch job with id "+resumedJobId+"has been resumed"); + } + } diff --git a/src/test/java/org/onedatashare/transferservice/odstransferservice/OdsTransferServiceApplicationTests.java b/src/test/java/org/onedatashare/transferservice/odstransferservice/OdsTransferServiceApplicationTests.java index b10ee3cc..d6371fc7 100644 --- a/src/test/java/org/onedatashare/transferservice/odstransferservice/OdsTransferServiceApplicationTests.java +++ b/src/test/java/org/onedatashare/transferservice/odstransferservice/OdsTransferServiceApplicationTests.java @@ -6,8 +6,8 @@ @SpringBootTest class OdsTransferServiceApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() { + } -} +} \ No newline at end of file