I have a simple workflow that returns Google credentials. I want to ensure that only one of these workflows is running at the same time (user is asked once) but many other workflows can run it and await on the result of it.
It seems there is no way to subscribe on a running workflow. If I run it as Workflow.newChildWorkflowStub then it fails with an error that workflow WorkflowExecutionAlreadyStarted
when there is a running one. Should I use some kind of proxy workflows or activities to get similar results. How would it look like?
class GetGoogleCredentialsWorkflowImpl : GetGoogleCredentialsWorkflow {
private val userActivities = activities<UserActivities>()
private var codesMap = mutableMapOf<Long, String>()
// workflow method
override fun getCredentials(userId: Long): GoogleCredentials? {
val credentials: GoogleCredentials? = userActivities.getGoogleCredentials(userId)
val now = Workflow.currentTimeMillis()
return if (credentials == null || credentials.expired(now)) {
userActivities.sendAuthorizationLinkToUser(userId)
Workflow.await(Duration.ofHours(24)) { codesMap.containsKey(userId) }
userActivities.processAuthorizationCode(userId, codesMap[userId]!!)
} else {
credentials
}
}
// workflow signal
override fun processCode(userId: Long, code: String) {
codesMap[userId] = code
}
}