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

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

The newest version!
package com.fivefaces.structureclient.controller;

import com.fivefaces.cloud.workflow.WorkflowRepo;
import com.fivefaces.cloud.workflow.WorkflowService;
import com.fivefaces.cloud.workflow.awsonprem.model.WorkflowStructure;
import com.fivefaces.structure.query.builder.StructureQuery;
import com.fivefaces.structure.service.StructureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class ScheduleController {

    private final StructureService structureService;
    private final WorkflowService workflowService;
    private final WorkflowRepo workflowRepo;

    @Scheduled(fixedDelay = 30000)
    public void scheduleFixedDelayTask() {

        // todo this requires the time to execute to be added once we implement the thing
        StructureQuery json = StructureQuery.fromJson("""
                {
                  "type" : "delayedWorkflow",
                  "criteria" : []
                }""").build();

        final List> futureJobs = structureService.query(json);
        for (Map jsonObject : futureJobs) {
            final String functionName = (String) jsonObject.get("workflow");
            WorkflowStructure workflow = workflowRepo.findWorkflowByName(functionName);
            final String workflowResult = workflowService.instantiateSyncWorkflow(workflow, jsonObject.toString()).getStatus();
            if (workflowResult.contains("SUC")) {
                // delete the record
                StructureQuery deleteJson = StructureQuery.fromJson("{\n" +
                        "          \"type\": \"delayedWorkflow\",\n" +
                        "          \"criteria\": [\n" +
                        "            {\n" +
                        "              \"member\": \"$.id\",\n" +
                        "              \"type\": \"eq\",\n" +
                        "              \"string\": true,\n" +
                        "              \"value\": \"" + jsonObject.get("id") + "\"\n" +
                        "            }\n" +
                        "          ]\n" +
                        "        }").build();
                try {
                    structureService.delete(deleteJson);
                } catch (Exception exception) {
                    log.info(exception.getMessage());
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy