Skip to content

Commit

Permalink
#38 Testing Framework - background execution of approve and import jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
spuliaiev-sfdc committed Aug 5, 2018
1 parent 82ecee9 commit 1a62a55
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -153,17 +142,21 @@ public Object get(@PathVariable("id") String id) {
@GetMapping("/approveImport/{importId}")
@ResponseBody
public Object approveImport(@PathVariable("importId") String importRequestId,
@RequestAttribute("background") Optional<Boolean> backgroundOption,
@RequestAttribute("tentativeOnly") Optional<Boolean> tentativeOnlyOption,
@RequestAttribute("subFolders") Optional<Boolean> 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("approveImport")
.put("importId", importRequestId)
.put("tentativeOnly", tentativeOnly)
.put("subFoldersOption", subFoldersOption)
.put("background", background)
.put("importId", subFolders)
.build();
}
Expand All @@ -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();
}
Expand All @@ -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<Boolean> tentativeOnlyOption,
@RequestAttribute("subFolders") Optional<Boolean> subFoldersOption) {
@RequestAttribute("background") Optional<Boolean> backgroundOption,
@RequestAttribute("tentativeOnly") Optional<Boolean> tentativeOnlyOption,
@RequestAttribute("subFolders") Optional<Boolean> 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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 1a62a55

Please sign in to comment.