use of io.automatiko.engine.api.workflow.ProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method marhsallProcessInstance.
public byte[] marhsallProcessInstance(ProcessInstance<?> processInstance) {
io.automatiko.engine.api.runtime.process.ProcessInstance pi = ((AbstractProcessInstance<?>) processInstance).internalGetProcessInstance();
if (pi == null) {
return null;
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(baos, ((io.automatiko.engine.workflow.base.instance.ProcessInstance) pi).getProcessRuntime(), null, env);
context.setProcessInstanceId(pi.getId());
context.setState(pi.getState());
String processType = pi.getProcess().getType();
context.stream.writeUTF(processType);
io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
Object result = marshaller.writeProcessInstance(context, pi);
if (marshaller instanceof ProtobufRuleFlowProcessInstanceMarshaller && result != null) {
AutomatikoMessages.ProcessInstance _instance = (AutomatikoMessages.ProcessInstance) result;
PersisterHelper.writeToStreamWithHeader(context, _instance);
}
context.close();
((WorkflowProcessInstanceImpl) pi).disconnect();
return baos.toByteArray();
} catch (Exception e) {
throw new RuntimeException("Error while marshalling process instance", e);
}
}
use of io.automatiko.engine.api.workflow.ProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method importProcessInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance importProcessInstance(ExportedProcessInstance<?> instance, Process process) {
List<Map<String, String>> timers = instance.convertTimers();
WorkflowProcessInstance wpi = importWorkflowProcessInstance((String) instance.getHeader(), (String) instance.getInstance(), timers, process);
Model model = ((AbstractProcess) process).createModel();
model.fromMap(wpi.getVariables());
ProcessInstance processInstance = ((AbstractProcess) process).createInstance(wpi, model, 1);
if (timers != null && !timers.isEmpty()) {
String parentProcessInstanceId = wpi.getParentProcessInstanceId();
if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
parentProcessInstanceId += ":";
} else {
parentProcessInstanceId = "";
}
JobsService jobService = ((WorkflowProcessInstanceImpl) wpi).getProcessRuntime().getJobsService();
Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) wpi).getNodeInstances(true);
// keeps iterator for statebased node instance timers
Map<String, Iterator<Timer>> nodeInstanceTimers = new LinkedHashMap<>();
for (Map<String, String> timer : timers) {
String nodeInstanceId = timer.get("nodeInstanceId");
for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
if (ni.getId().equals(nodeInstanceId)) {
ExpirationTime expirationTime = null;
long timerNodeId = 0;
if (((NodeInstanceImpl) ni).getRetryJobId() != null && ((NodeInstanceImpl) ni).getRetryJobId().equals(timer.get("timerId"))) {
jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), ni.getNodeId(), "retry:" + ni.getId(), expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), ni.getProcessInstance().getRootProcessInstanceId(), ni.getProcessInstance().getProcessId(), ni.getProcessInstance().getProcess().getVersion(), ni.getProcessInstance().getRootProcessId()));
break;
} else if (ni instanceof TimerNodeInstance) {
TimerNodeInstance nodeInstance = (TimerNodeInstance) ni;
timerNodeId = nodeInstance.getTimerNode().getTimer().getId();
expirationTime = nodeInstance.createTimerInstance(nodeInstance.getTimerNode().getTimer());
expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
} else if (ni instanceof StateBasedNodeInstance) {
StateBasedNodeInstance nodeInstance = (StateBasedNodeInstance) ni;
if (nodeInstance.getTimerInstances().contains(timer.get("timerId"))) {
Iterator<Timer> it = nodeInstanceTimers.computeIfAbsent(nodeInstanceId, k -> nodeInstance.getEventBasedNode().getTimers().keySet().iterator());
expirationTime = nodeInstance.createTimerInstance(it.next());
expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
}
}
// lastly schedule timer on calculated expiration time
jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), timerNodeId, expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), wpi.getRootProcessInstanceId(), wpi.getProcessId(), wpi.getProcess().getVersion(), wpi.getRootProcessId()));
break;
}
}
}
}
return processInstance;
}
use of io.automatiko.engine.api.workflow.ProcessInstance in project automatiko-engine by automatiko-io.
the class PublishEventTest method testProcessWithMilestoneEvents.
@Test
public void testProcessWithMilestoneEvents() throws Exception {
Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
assertThat(app).isNotNull();
TestEventPublisher publisher = new TestEventPublisher();
app.unitOfWorkManager().eventManager().setService("http://myhost");
app.unitOfWorkManager().eventManager().addPublisher(publisher);
UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
Process<? extends Model> p = app.processes().processById("TestCase.SimpleMilestone_1_0");
ProcessInstance<?> processInstance = p.createInstance(p.createModel());
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
uow.end();
List<DataEvent<?>> events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
DataEvent<?> event = events.get(0);
assertThat(event).isInstanceOf(ProcessInstanceDataEvent.class);
ProcessInstanceDataEvent processDataEvent = (ProcessInstanceDataEvent) event;
assertThat(processDataEvent.getSource()).isEqualTo("http://myhost/SimpleMilestone");
Set<MilestoneEventBody> milestones = ((ProcessInstanceDataEvent) event).getData().getMilestones();
assertThat(milestones).hasSize(2).extracting(e -> e.getName(), e -> e.getStatus()).containsExactlyInAnyOrder(tuple("AutoStartMilestone", Status.COMPLETED.name()), tuple("SimpleMilestone", Status.COMPLETED.name()));
}
use of io.automatiko.engine.api.workflow.ProcessInstance in project automatiko-engine by automatiko-io.
the class UserTaskTest method testApprovalWithReadonlyVariableTags.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testApprovalWithReadonlyVariableTags() throws Exception {
Application app = generateCodeProcessesOnly("usertask/approval-with-readonly-variable-tags.bpmn2");
assertThat(app).isNotNull();
Class<?> resourceClazz = Class.forName("org.acme.travels.ApprovalsModel", true, testClassLoader());
assertNotNull(resourceClazz);
Field approverField = resourceClazz.getDeclaredField("approver");
assertThat(approverField).isNotNull();
assertThat(approverField.getType().getCanonicalName()).isEqualTo(String.class.getCanonicalName());
Process<? extends Model> p = app.processes().processById("approvals");
Model m = p.createModel();
Map<String, Object> parameters = new HashMap<>();
parameters.put("approver", "john");
m.fromMap(parameters);
ProcessInstance processInstance = p.createInstance(m);
processInstance.start();
assertEquals(io.automatiko.engine.api.runtime.process.ProcessInstance.STATE_ACTIVE, processInstance.status());
final Model updates = p.createModel();
parameters = new HashMap<>();
parameters.put("approver", "mary");
updates.fromMap(parameters);
// updating readonly variable should fail
assertThrows(VariableViolationException.class, () -> processInstance.updateVariables(updates));
processInstance.abort();
assertEquals(io.automatiko.engine.api.runtime.process.ProcessInstance.STATE_ABORTED, processInstance.status());
}
use of io.automatiko.engine.api.workflow.ProcessInstance in project automatiko-engine by automatiko-io.
the class TimerEventTest method testStartTimerEvent.
@Test
public void testStartTimerEvent() throws Exception {
Application app = generateCodeProcessesOnly("timer/StartTimerDuration.bpmn2");
assertThat(app).isNotNull();
NodeLeftCountDownProcessEventListener listener = new NodeLeftCountDownProcessEventListener("timer fired", 1);
((DefaultProcessEventListenerConfig) app.config().process().processEventListeners()).register(listener);
Process<? extends Model> p = app.processes().processById("defaultPackage.TimerProcess");
// activate to schedule timers
p.activate();
boolean completed = listener.waitTillCompleted(5000);
assertThat(completed).isTrue();
Collection<?> instances = p.instances().values(1, 10);
if (instances.size() == 0) {
Thread.sleep(1000);
instances = p.instances().values(1, 10);
}
assertThat(instances).hasSize(1);
ProcessInstance<?> processInstance = (ProcessInstance<?>) instances.iterator().next();
assertThat(processInstance).isNotNull();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
processInstance.abort();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
instances = p.instances().values(1, 10);
assertThat(instances).hasSize(0);
}
Aggregations