diff --git a/core/libs/image-importer/src/main/java/gallerymine/backend/pool/BackgroundJobRunner.java b/core/libs/image-importer/src/main/java/gallerymine/backend/pool/BackgroundJobRunner.java new file mode 100644 index 0000000..0741fc0 --- /dev/null +++ b/core/libs/image-importer/src/main/java/gallerymine/backend/pool/BackgroundJobRunner.java @@ -0,0 +1,15 @@ +package gallerymine.backend.pool; + +public abstract class BackgroundJobRunner implements Runnable { + + private String name = null; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/core/libs/image-importer/src/main/java/gallerymine/backend/pool/MiscBackgroundJobsPool.java b/core/libs/image-importer/src/main/java/gallerymine/backend/pool/MiscBackgroundJobsPool.java new file mode 100644 index 0000000..1e14b5d --- /dev/null +++ b/core/libs/image-importer/src/main/java/gallerymine/backend/pool/MiscBackgroundJobsPool.java @@ -0,0 +1,61 @@ +package gallerymine.backend.pool; + +import gallerymine.backend.beans.AppConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.stereotype.Component; + +/** + * Manages Pool of BackgroundJobs + * Created by sergii_puliaiev on 8/03/18. + */ +@Component +public class MiscBackgroundJobsPool { + + private static Logger log = LoggerFactory.getLogger(MiscBackgroundJobsPool.class); + + @Autowired + private ApplicationContext context; + + @Autowired + private AppConfig appConfig; + + private ThreadPoolTaskExecutor pool; + + public MiscBackgroundJobsPool() { + pool = new ThreadPoolTaskExecutor(); + pool.setCorePoolSize(10); + pool.setMaxPoolSize(10); + pool.setWaitForTasksToCompleteOnShutdown(true); + + pool.setThreadGroupName("MiscBackJobPool"); + pool.setThreadNamePrefix("MiscBackJobPool_"); + + pool.initialize(); + } + + public void executeRequest(String name, Runnable runnerJob) { + if (runnerJob == null) { + return; + } + BackgroundJobRunner runner = new BackgroundJobRunner() { + @Override + public void run() { + Thread.currentThread().setName(Thread.currentThread().getName()+"-"+getName()); + try { + runnerJob.run(); + } catch (Exception e){ + log.error("Failed back job with name {}. Reason: {}", getName(), e.getMessage(), e); + } + } + }; + runner.setName(name); + log.info(" Misc Background Job request execution id={} and path={}", name); + pool.execute(runner); + log.info(" Misc Background Job scheduled id={} and path={}", name); + } + +} diff --git a/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java b/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java index 4630d88..d3acb4f 100755 --- a/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java +++ b/ui/web/src/main/java/gallerymine/frontend/mvc/ImportRequestsController.java @@ -17,13 +17,11 @@ package gallerymine.frontend.mvc; import gallerymine.backend.beans.AppConfig; -import gallerymine.backend.beans.repository.CustomerRepository; import gallerymine.backend.beans.repository.ImportRequestRepository; -import gallerymine.backend.beans.repository.ProcessRepository; import gallerymine.backend.importer.ImportProcessor; import gallerymine.backend.pool.ImportRequestPoolManager; +import gallerymine.backend.pool.MiscBackgroundJobsPool; import gallerymine.backend.services.ImportService; -import gallerymine.backend.utils.ImportUtils; import gallerymine.model.importer.ImportRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,23 +51,14 @@ public class ImportRequestsController { public AppConfig appConfig; @Autowired - private CustomerRepository messageRepository; + private MiscBackgroundJobsPool miscBackgroundJobsPool; @Autowired private ImportRequestRepository requestRepository; - @Autowired - private ProcessRepository processRepository; - - @Autowired - private ImportProcessor requestProcessor; - @Autowired private ImportRequestPoolManager requestPool; - @Autowired - private ImportUtils importUtils; - @Autowired private ImportService importService; @@ -153,10 +142,12 @@ public Object get(@PathVariable("id") String id) { @GetMapping("/approveImport/{importId}") @ResponseBody public Object approveImport(@PathVariable("importId") String importRequestId, + @RequestAttribute("background") Optional backgroundOption, @RequestAttribute("tentativeOnly") Optional tentativeOnlyOption, @RequestAttribute("subFolders") Optional subFoldersOption) { boolean tentativeOnly = tentativeOnlyOption.orElse(true); boolean subFolders = subFoldersOption.orElse(true); + boolean background = backgroundOption.orElse(false); ImportRequest request = requestRepository.findOne(importRequestId); if (request == null) { @@ -164,6 +155,8 @@ public Object approveImport(@PathVariable("importId") String importRequestId, .op("approveImport") .put("importId", importRequestId) .put("tentativeOnly", tentativeOnly) + .put("subFoldersOption", subFoldersOption) + .put("background", background) .put("importId", subFolders) .build(); } @@ -173,14 +166,22 @@ public Object approveImport(@PathVariable("importId") String importRequestId, || request.getStatus().equals(ImportRequest.ImportStatus.APPROVED) ) { - log.info("Approving node requestId={} status={}", request.getId(), request.getStatus()); - importService.approveImportRequest(request, tentativeOnly, subFolders); + log.info("Approving node background={} requestId={} status={}", background, request.getId(), request.getStatus()); + if (background) { + miscBackgroundJobsPool.executeRequest("ApproveImportRequest_" + request.getPath(), + () -> importService.approveImportRequest(request, tentativeOnly, subFolders) + ); + } else { + importService.approveImportRequest(request, tentativeOnly, subFolders); + } } else { log.warn("Wrong status - not APPROVING or APPROVED for requestId={} status={}", request.getId(), request.getStatus()); return responseError("Wrong status - not APPROVING or APPROVED") .op("approveImport") .put("importId", importRequestId) .put("tentativeOnly", tentativeOnly) + .put("subFoldersOption", subFoldersOption) + .put("background", background) .put("importId", subFolders) .build(); } @@ -189,22 +190,29 @@ public Object approveImport(@PathVariable("importId") String importRequestId, .op("approveImport") .put("importId", importRequestId) .put("tentativeOnly", tentativeOnly) + .put("subFoldersOption", subFoldersOption) + .put("background", background) .put("importId", subFolders) .build(); } + @GetMapping("/rematchImport/{importId}") @ResponseBody public Object rematchImport(@PathVariable("importId") String importRequestId, - @RequestAttribute("tentativeOnly") Optional tentativeOnlyOption, - @RequestAttribute("subFolders") Optional subFoldersOption) { + @RequestAttribute("background") Optional backgroundOption, + @RequestAttribute("tentativeOnly") Optional tentativeOnlyOption, + @RequestAttribute("subFolders") Optional subFoldersOption) { boolean tentativeOnly = tentativeOnlyOption.orElse(true); boolean subFolders = subFoldersOption.orElse(true); + boolean background = backgroundOption.orElse(false); ImportRequest request = requestRepository.findOne(importRequestId); if (request == null) { return responseErrorNotFound("Not found") .op("rematchImport") .put("importId", importRequestId) + .put("background", background) + .put("subFolders", subFolders) .put("tentativeOnly", tentativeOnly) .put("importId", subFolders) .build(); @@ -216,10 +224,18 @@ public Object rematchImport(@PathVariable("importId") String importRequestId, request.getStatus().equals(ImportRequest.ImportStatus.APPROVED) ) { log.info("Re-match node requestId={} status={}", request.getId(), request.getStatus()); - importService.rematchImportRequest(request, tentativeOnly, subFolders); + if (background) { + miscBackgroundJobsPool.executeRequest("ApproveImportRequest_" + request.getPath(), + () -> importService.rematchImportRequest(request, tentativeOnly, subFolders) + ); + } else { + importService.rematchImportRequest(request, tentativeOnly, subFolders); + } return responseOk() .op("rematchImport") .put("importId", importRequestId) + .put("background", background) + .put("subFolders", subFolders) .put("tentativeOnly", tentativeOnly) .put("importId", subFolders) .build(); @@ -229,6 +245,8 @@ public Object rematchImport(@PathVariable("importId") String importRequestId, return responseError("Wrong status - not APPROVING or APPROVED") .op("rematchImport") .put("importId", importRequestId) + .put("background", background) + .put("subFolders", subFolders) .put("tentativeOnly", tentativeOnly) .put("importId", subFolders) .build();