cadence-workflowtemporal-workflow

What's the right way to interrupt a long running Uber Cadence activity?


If I have a long-running activity that does something like

func Activity(ctx context.Context) error {
    ticker := time.NewTicker(5 * time.Second)
    for {
        select {
        case <-ctx.Done():
            return ctx.Err()
        case <-ticker.C:
            if isServiceReady(ctx) {
                break
            }
        }
    }
    return nil
}

and I'd like to be able to cancel that from a workflow (without the entire workflow getting cancelled) how would I then do that?

I had hoped that you could receive on the context Done channel and then create a cancellable context in the workflow, but that apparently doesn't do anything.


Solution

  • Currently to be cancellable an activity has to heartbeat. The Cadence service piggybacks the cancellation status on the activity heartbeat return value. So if you start heartbeating on every tick and your heartbeat interval is small enough (as client doesn't make call the service on every heartbeat method call) then your code should start working.

    In the future we plan to add notion of a session with a worker specific task list to deliver cancellations. After it is added the cancellation wouldn't rely on heartbeats to be delivered.