Skip to content

Expose active workflow instances on WorkflowDefinition #1677

Description

@mcruzdev

What would you like to be added:

Expose the full set of active (non-terminal) WorkflowInstances tracked by a WorkflowDefinition, similar to the existing scheduledInstances() accessor.

WorkflowDefinition (impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDefinition.java) already keeps every running instance in an internal map:

private Map<String, WorkflowInstance> activeInstances = new ConcurrentHashMap<>();

but the only public accessor is a single-instance lookup:

public Optional<WorkflowInstance> activeInstance(String instanceId)

There's no way to enumerate all active instances for a definition. Meanwhile, scheduledInstances already has a bulk accessor that returns an unmodifiable view:

public Collection<WorkflowInstance> scheduledInstances() {
    return Collections.unmodifiableCollection(scheduledInstances);
}

I'd like to add an equivalent for active instances, e.g.:

public Collection<WorkflowInstance> activeInstances() {
    return Collections.unmodifiableCollection(activeInstances.values());
}

Why is this needed:

In quarkus-flow we're adding a REST endpoint that lists the in-memory active workflow instances on a runner pod, so a rebalancer service can tell which instances are actively being processed on a given pod vs. only persisted in the DB (see PR #892).

Since the SDK doesn't expose this today, the current implementation duplicates the tracking with a separate ActiveInstanceRegistry that listens to WorkflowExecutionListener lifecycle events and maintains its own copy of the same data WorkflowDefinition already holds internally.

As pointed out in review feedback by @fjtirado, this duplication isn't necessary — WorkflowDefinition is already the source of truth for active instances, it just isn't public. Exposing activeInstances() (mirroring scheduledInstances()) would let quarkus-flow (and other SDK consumers) query this directly instead of re-implementing the same bookkeeping, and let us drop ActiveInstanceRegistry in favor of the new method.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions