use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.
the class GFacUtils method saveJobStatus.
public static void saveJobStatus(ProcessContext processContext, JobModel jobModel) throws GFacException {
try {
// first we save job jobModel to the registry for sa and then save the job status.
JobStatus jobStatus = null;
if (jobModel.getJobStatuses() != null)
jobStatus = jobModel.getJobStatuses().get(0);
ExperimentCatalog experimentCatalog = processContext.getExperimentCatalog();
List<JobStatus> statuses = new ArrayList<>();
statuses.add(jobStatus);
jobModel.setJobStatuses(statuses);
if (jobStatus.getTimeOfStateChange() == 0 || jobStatus.getTimeOfStateChange() > 0) {
jobStatus.setTimeOfStateChange(AiravataUtils.getCurrentTimestamp().getTime());
} else {
jobStatus.setTimeOfStateChange(jobStatus.getTimeOfStateChange());
}
CompositeIdentifier ids = new CompositeIdentifier(jobModel.getTaskId(), jobModel.getJobId());
experimentCatalog.add(ExpCatChildDataType.JOB_STATUS, jobStatus, ids);
JobIdentifier identifier = new JobIdentifier(jobModel.getJobId(), jobModel.getTaskId(), processContext.getProcessId(), processContext.getProcessModel().getExperimentId(), processContext.getGatewayId());
JobStatusChangeEvent jobStatusChangeEvent = new JobStatusChangeEvent(jobStatus.getJobState(), identifier);
MessageContext msgCtx = new MessageContext(jobStatusChangeEvent, MessageType.JOB, AiravataUtils.getId(MessageType.JOB.name()), processContext.getGatewayId());
msgCtx.setUpdatedTime(AiravataUtils.getCurrentTimestamp());
processContext.getStatusPublisher().publish(msgCtx);
} catch (Exception e) {
throw new GFacException("Error persisting job status" + e.getLocalizedMessage(), e);
}
}
use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.
the class GFacUtils method saveAndPublishTaskStatus.
public static void saveAndPublishTaskStatus(TaskContext taskContext) throws GFacException {
try {
TaskState state = taskContext.getTaskState();
// first we save job jobModel to the registry for sa and then save the job status.
ProcessContext processContext = taskContext.getParentProcessContext();
ExperimentCatalog experimentCatalog = processContext.getExperimentCatalog();
TaskStatus status = taskContext.getTaskStatus();
if (status.getTimeOfStateChange() == 0 || status.getTimeOfStateChange() > 0) {
status.setTimeOfStateChange(AiravataUtils.getCurrentTimestamp().getTime());
} else {
status.setTimeOfStateChange(status.getTimeOfStateChange());
}
experimentCatalog.add(ExpCatChildDataType.TASK_STATUS, status, taskContext.getTaskId());
TaskIdentifier identifier = new TaskIdentifier(taskContext.getTaskId(), processContext.getProcessId(), processContext.getProcessModel().getExperimentId(), processContext.getGatewayId());
TaskStatusChangeEvent taskStatusChangeEvent = new TaskStatusChangeEvent(state, identifier);
MessageContext msgCtx = new MessageContext(taskStatusChangeEvent, MessageType.TASK, AiravataUtils.getId(MessageType.TASK.name()), taskContext.getParentProcessContext().getGatewayId());
msgCtx.setUpdatedTime(AiravataUtils.getCurrentTimestamp());
processContext.getStatusPublisher().publish(msgCtx);
} catch (Exception e) {
throw new GFacException("Error persisting task status" + e.getLocalizedMessage(), e);
}
}
use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.
the class GFacUtils method saveAndPublishProcessStatus.
public static void saveAndPublishProcessStatus(ProcessContext processContext) throws GFacException {
try {
// first we save job jobModel to the registry for sa and then save the job status.
ExperimentCatalog experimentCatalog = processContext.getExperimentCatalog();
ProcessStatus status = processContext.getProcessStatus();
if (status.getTimeOfStateChange() == 0 || status.getTimeOfStateChange() > 0) {
status.setTimeOfStateChange(AiravataUtils.getCurrentTimestamp().getTime());
} else {
status.setTimeOfStateChange(status.getTimeOfStateChange());
}
experimentCatalog.add(ExpCatChildDataType.PROCESS_STATUS, status, processContext.getProcessId());
ProcessIdentifier identifier = new ProcessIdentifier(processContext.getProcessId(), processContext.getProcessModel().getExperimentId(), processContext.getGatewayId());
ProcessStatusChangeEvent processStatusChangeEvent = new ProcessStatusChangeEvent(status.getState(), identifier);
MessageContext msgCtx = new MessageContext(processStatusChangeEvent, MessageType.PROCESS, AiravataUtils.getId(MessageType.PROCESS.name()), processContext.getGatewayId());
msgCtx.setUpdatedTime(AiravataUtils.getCurrentTimestamp());
processContext.getStatusPublisher().publish(msgCtx);
} catch (Exception e) {
throw new GFacException("Error persisting process status" + e.getLocalizedMessage(), e);
}
}
use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.
the class WorkflowInterpreter method publishNodeStatusChange.
private void publishNodeStatusChange(ProcessState state, String nodeId, String expId) throws AiravataException {
if (publisher != null) {
MessageContext msgCtx = new MessageContext(new ProcessStatusChangeEvent(state, new ProcessIdentifier(nodeId, expId, gatewayId)), MessageType.PROCESS, AiravataUtils.getId("NODE"), gatewayId);
msgCtx.setUpdatedTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
publisher.publish(msgCtx);
} else {
log.warn("Failed to publish workflow status change, publisher is null");
}
}
use of org.apache.airavata.messaging.core.MessageContext in project airavata by apache.
the class WorkflowInterpreter method scheduleDynamically.
/**
* @throws WorkflowException
* @throws RegistryException
*/
public void scheduleDynamically() throws WorkflowException, RegistryException, AiravataException {
try {
this.getWorkflow().setExecutionState(WorkflowExecutionState.RUNNING);
ArrayList<Node> inputNodes = this.getInputNodesDynamically();
List<InputDataObjectType> experimentInputs = experiment.getExperimentInputs();
Map<String, String> inputDataStrings = new HashMap<String, String>();
for (InputDataObjectType dataObjectType : experimentInputs) {
inputDataStrings.put(dataObjectType.getName(), dataObjectType.getValue());
}
for (Node node : inputNodes) {
publishNodeStatusChange(WorkflowNodeState.EXECUTING, node.getID(), experiment.getExperimentID());
if (inputDataStrings.containsKey(node.getID())) {
((InputNode) node).setDefaultValue(inputDataStrings.get(node.getID()));
} else {
log.warn("value for node not found " + node.getName());
}
}
for (int i = 0; i < inputNodes.size(); ++i) {
Node node = inputNodes.get(i);
invokedNode.add(node);
node.setState(NodeExecutionState.FINISHED);
publishNodeStatusChange(WorkflowNodeState.INVOKED, node.getID(), experiment.getExperimentID());
notifyViaInteractor(WorkflowExecutionMessage.NODE_STATE_CHANGED, null);
String portId = ((InputNode) node).getID();
Object portValue = ((InputNode) node).getDefaultValue();
DataType dataType = ((InputNode) node).getDataType();
// Saving workflow input Node data before running the workflow
WorkflowNodeDetails workflowNode = createWorkflowNodeDetails(node);
InputDataObjectType elem = new InputDataObjectType();
elem.setName(portId);
elem.setValue(portValue == null ? null : portValue.toString());
elem.setType(dataType);
workflowNode.addToNodeInputs(elem);
getExperimentCatalog().update(ExperimentCatalogModelType.WORKFLOW_NODE_DETAIL, workflowNode, workflowNode.getNodeInstanceId());
updateWorkflowNodeStatus(workflowNode, WorkflowNodeState.COMPLETED);
publishNodeStatusChange(WorkflowNodeState.COMPLETED, node.getID(), experiment.getExperimentID());
}
while (this.getWorkflow().getExecutionState() != WorkflowExecutionState.STOPPED) {
ArrayList<Thread> threadList = new ArrayList<Thread>();
if (getRemainNodesDynamically() == 0) {
notifyViaInteractor(WorkflowExecutionMessage.EXECUTION_STATE_CHANGED, WorkflowExecutionState.STOPPED);
}
// ok we have paused sleep
if (this.getWorkflow().getExecutionState() == WorkflowExecutionState.PAUSED) {
log.info("Workflow execution " + experiment.getExperimentID() + " is paused.");
while (this.getWorkflow().getExecutionState() == WorkflowExecutionState.PAUSED) {
try {
Thread.sleep(400);
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
}
if (this.getWorkflow().getExecutionState() == WorkflowExecutionState.STOPPED) {
continue;
}
log.info("Workflow execution " + experiment.getExperimentID() + " is resumed.");
}
// get task list and execute them
ArrayList<Node> readyNodes = this.getReadyNodesDynamically();
for (final Node node : readyNodes) {
if (node.isBreak()) {
this.notifyPause();
break;
}
if (this.getWorkflow().getExecutionState() == WorkflowExecutionState.PAUSED || this.getWorkflow().getExecutionState() == WorkflowExecutionState.STOPPED) {
break;
}
WorkflowNodeDetails workflowNodeDetails = createWorkflowNodeDetails(node);
// Since this is an independent node execution we can run these nodes in separate threads.
Thread th = new Thread() {
public synchronized void run() {
try {
executeDynamically(node);
} catch (WorkflowException e) {
log.error("Error execution workflow Node : " + node.getID());
return;
} catch (TException e) {
log.error(e.getMessage(), e);
} catch (RegistryException e) {
log.error(e.getMessage(), e);
} catch (AiravataException e) {
log.error(e.getMessage(), e);
}
}
};
updateWorkflowNodeStatus(workflowNodeDetails, WorkflowNodeState.INVOKED);
publishNodeStatusChange(WorkflowNodeState.INVOKED, node.getID(), experiment.getExperimentID());
threadList.add(th);
th.start();
if (this.getWorkflow().getExecutionState() == WorkflowExecutionState.STEP) {
this.getWorkflow().setExecutionState(WorkflowExecutionState.PAUSED);
// TODO update experiment state to suspend
break;
}
}
// This thread waits until parallel nodes get finished to send the outputs dynamically.
for (Thread th : threadList) {
try {
th.join();
} catch (InterruptedException e) {
// To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}
}
// Above statement set the nodeCount back to 0.
// TODO commented this for foreach, fix this.
sendOutputsDynamically();
// Dry run sleep a lil bit to release load
if (readyNodes.size() == 0) {
// so we should pause the execution
if (InterpreterUtil.getRunningNodeCountDynamically(this.getGraph()) == 0) /**
*&& InterpreterUtil.getFailedNodeCountDynamically(this.getGraph()) != 0*
*/
{
// Since airavata only support workflow interpreter server mode we do not want to keep thread in sleep mode
// continuously, so we make the workflow stop when there's nothing to do.
this.getWorkflow().setExecutionState(WorkflowExecutionState.STOPPED);
}
try {
Thread.sleep(400);
} catch (InterruptedException e) {
log.error("Workflow Excecution is interrupted !");
return;
}
}
}
if (InterpreterUtil.getFailedNodeCountDynamically(this.getGraph()) == 0) {
if (this.config.isActOnProvenance()) {
try {
// this.getConfig().getConfiguration().getAiravataAPI().getProvenanceManager().setWorkflowInstanceStatus(
// this.config.getTopic(), this.config.getTopic(), State.FINISHED);
updateExperimentStatus(ExperimentState.COMPLETED);
} catch (Exception e) {
throw new WorkflowException(e);
}
// System.out.println(this.config.getConfiguration().getJcrComponentRegistry().getExperimentCatalog().getWorkflowStatus(this.topic));
}
} else {
if (this.config.isActOnProvenance()) {
// this.getConfig().getConfiguration().getAiravataAPI().getProvenanceManager().
// setWorkflowInstanceStatus(this.config.getTopic(),this.config.getTopic(), State.FAILED);
updateExperimentStatus(ExperimentState.FAILED);
}
}
UUID uuid = UUID.randomUUID();
notifyViaInteractor(WorkflowExecutionMessage.EXECUTION_TASK_START, new WorkflowInterpreterInteractor.TaskNotification("Stop Workflow", "Cleaning up resources for Workflow", uuid.toString()));
// Send Notification for output values
finish();
// Sleep to provide for notification delay
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
notifyViaInteractor(WorkflowExecutionMessage.EXECUTION_TASK_END, new WorkflowInterpreterInteractor.TaskNotification("Stop Workflow", "Cleaning up resources for Workflow", uuid.toString()));
} catch (RuntimeException e) {
// we reset all the state
cleanup();
raiseException(e);
} finally {
cleanup();
this.getWorkflow().setExecutionState(WorkflowExecutionState.NONE);
ExperimentStatusChangeEvent event = new ExperimentStatusChangeEvent(ExperimentState.COMPLETED, experiment.getExperimentId(), gatewayId);
MessageContext msgCtx = new MessageContext(event, MessageType.EXPERIMENT, AiravataUtils.getId("EXPERIMENT"), gatewayId);
msgCtx.setUpdatedTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
publisher.publish(msgCtx);
}
}
Aggregations