com.fivefaces.structureclient.controller.ScheduleController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-structure-client Show documentation
Show all versions of common-structure-client Show documentation
structure Client for Five Faces
package com.fivefaces.structureclient.controller;
import com.fivefaces.cloud.workflow.WorkflowService;
import com.fivefaces.common.util.JsonUtils;
import com.fivefaces.structure.service.StructureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
@Slf4j
public class ScheduleController {
private final StructureService structureService;
private final WorkflowService workflowService;
@Scheduled(fixedDelay = 30000)
public void scheduleFixedDelayTask() {
// todo this requires the time to execute to be added once we implement the thing
JSONObject json = new JSONObject("{\n" +
" \"type\" : \"delayed_workflow\",\n" +
" \"criteria\" : []\n" +
"}");
final String futureJobs = structureService.query(json);
final String futureJobsAsString = new JsonUtils().unmarshall(futureJobs);
final JSONArray jsonArray = new JSONArray(futureJobsAsString);
for (Object o : jsonArray) {
JSONObject jsonObject = (JSONObject) o;
final String functionName = jsonObject.getString("workflow");
final String workflowResult = workflowService.instantiateSyncWorkflow(functionName, jsonObject.toString()).getStatus();
if (workflowResult.contains("SUC")) {
// delete the record
JSONObject deleteJson = new JSONObject("{\n" +
" \"type\": \"delayed_workflow\",\n" +
" \"criteria\": [\n" +
" {\n" +
" \"member\": \"$.id\",\n" +
" \"type\": \"eq\",\n" +
" \"string\": true,\n" +
" \"value\": \"" + jsonObject.getString("id") + "\"\n" +
" }\n" +
" ]\n" +
" }");
try {
structureService.delete(deleteJson);
} catch (Exception exception) {
log.info(exception.getMessage());
}
}
}
}
}