All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.structureclient.controller.ScheduleController Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
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());
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy