Mauro
Mauro

Reputation: 69

How can I check the current state of a flow by RPC?

I've found a method recordAuditEvent(...) inside the FlowLogic class, but I can't understand how I can use it and I don't know if it's useful for this use case.

Upvotes: 1

Views: 430

Answers (1)

Joel
Joel

Reputation: 23210

recordAuditEvent is for internal use only.

You can track the progress of a flow using CordaRPCOps.startTrackedFlowDynamic. This function returns a FlowHandle that has a progress property. progress is an Observable that emits an event for each progress tracker step in the flow. You could use it as follows:

val flowHandle = proxy.startTrackedFlowDynamic(MyFlow::class.java, arg1, arg2, ...)

flowHandle.progress.subscribe { progressTrackerLabel ->
    // Log the progress tracker label.
}

You can also get the flow's unique run ID via the FlowHandle's id property. You can use this ID to check whether the flow is still in progress by checking whether it is still present in the list of current state machines (i.e. flows):

val flowInProgress = flowHandle.id in cordaRPCOps.stateMachinesSnapshot().map { it.id }

You can also monitor the state machine manager to wait until the flow completes, then check whether it was successful and get its result:

val flowUpdates = cordaRPCOps.stateMachinesFeed().updates
flowUpdates.subscribe {
    if (it.id == flowHandle.id && it is StateMachineUpdate.Removed) {
        val int = it.result.getOrThrow()
        // Handle result.
    }
}

Upvotes: 3

Related Questions