Search in sources :

Example 6 with Event

use of org.ystia.yorc.alien4cloud.plugin.rest.Response.Event in project yorc-a4c-plugin by ystia.

the class WorkflowTask method run.

public void run() {
    Throwable error = null;
    String paasId = ctx.getDeploymentPaaSId();
    String deploymentUrl = "/deployments/" + paasId;
    YorcRuntimeDeploymentInfo jrdi = orchestrator.getDeploymentInfo(paasId);
    log.info(paasId + " Execute workflow " + workflowName);
    String taskUrl;
    try {
        taskUrl = restClient.postWorkflowToYorc(deploymentUrl, workflowName, inputs);
    } catch (Exception e) {
        orchestrator.sendMessage(paasId, "Workflow not accepted by Yorc: " + e.getMessage());
        callback.onFailure(e);
        return;
    }
    String taskId = taskUrl.substring(taskUrl.lastIndexOf("/") + 1);
    synchronized (jrdi) {
        // In case we want to undeploy during the workflow.
        jrdi.setDeployTaskId(taskId);
    }
    orchestrator.sendMessage(paasId, "Workflow " + workflowName + " sent to Yorc. taskId=" + taskId);
    // wait for end of task
    boolean done = false;
    long timeout = System.currentTimeMillis() + YORC_OPE_TIMEOUT;
    Event evt;
    while (!done && error == null) {
        synchronized (jrdi) {
            // Check for timeout
            long timetowait = timeout - System.currentTimeMillis();
            if (timetowait <= 0) {
                log.warn("Timeout occured");
                error = new Throwable("Workflow timeout");
                break;
            }
            // Wait Events from Yorc
            log.debug(paasId + ": Waiting for workflow events.");
            try {
                jrdi.wait(timetowait);
            } catch (InterruptedException e) {
                log.error("Interrupted while waiting for task end");
                break;
            }
            // Check if we received a Workflow Event and process it
            evt = jrdi.getLastEvent();
            if (evt != null && evt.getType().equals(EventListenerTask.EVT_WORKFLOW) && evt.getTask_id().equals(taskId)) {
                jrdi.setLastEvent(null);
                switch(evt.getStatus()) {
                    case "failed":
                        log.warn("Workflow failed: " + paasId);
                        // workflowStep(paasId, workflowName, "TODO", "TODO", "TODO");
                        error = new Exception("Workflow " + workflowName + " failed");
                        break;
                    case "canceled":
                        log.warn("Workflow canceled: " + paasId);
                        // workflowStep(paasId, workflowName, "TODO", "TODO", "TODO");
                        error = new Exception("Workflow " + workflowName + " canceled");
                        break;
                    case "done":
                        log.debug("Workflow success: " + paasId);
                        // workflowStep(paasId, workflowName, "TODO", "TODO", "TODO");
                        done = true;
                        break;
                    case "initial":
                        // workflowStarted(paasId, workflowName, "TODO");
                        break;
                    case "running":
                        // workflowStep(paasId, workflowName, "TODO", "TODO", "TODO");
                        break;
                    default:
                        log.warn("An event has been ignored. Unexpected status=" + evt.getStatus());
                        break;
                }
                continue;
            }
        }
        // We were awaken for some bad reason or a timeout
        // Check Task Status to decide what to do now.
        String status;
        try {
            status = restClient.getStatusFromYorc(taskUrl);
            log.debug("Returned status:" + status);
        } catch (Exception e) {
            status = "FAILED";
        }
        switch(status) {
            case "DONE":
                // Task OK.
                log.debug("Workflow OK");
                done = true;
                break;
            case "FAILED":
                log.debug("Workflow failed");
                error = new Exception("Workflow failed");
                break;
            default:
                log.debug("Workflow Status is currently " + status);
                break;
        }
    }
    synchronized (jrdi) {
        // Task is ended: Must remove the taskId and notify a possible undeploy waiting for it.
        jrdi.setDeployTaskId(null);
        jrdi.notify();
    }
    // Return result to a4c
    if (error == null) {
        callback.onSuccess(null);
    } else {
        callback.onFailure(error);
    }
}
Also used : Event(org.ystia.yorc.alien4cloud.plugin.rest.Response.Event)

Aggregations

Event (org.ystia.yorc.alien4cloud.plugin.rest.Response.Event)6 Map (java.util.Map)2 YorcRestException (org.ystia.yorc.alien4cloud.plugin.rest.YorcRestException)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)1 InstanceInformation (alien4cloud.paas.model.InstanceInformation)1 PaaSInstancePersistentResourceMonitorEvent (alien4cloud.paas.model.PaaSInstancePersistentResourceMonitorEvent)1 ParsingException (alien4cloud.tosca.parser.ParsingException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 ZipException (java.util.zip.ZipException)1 EventResponse (org.ystia.yorc.alien4cloud.plugin.rest.Response.EventResponse)1