Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Quickstart — Resonate example

examples-ci status

Quickstart | Resonate Go SDK

A durable countdown: register a function, invoke it from the CLI, and watch it tick down — surviving restarts along the way. This is the Go side of the cross-language quickstart.

resonate-sdk-go is pre-release. The SDK has no semver tag yet, so this example pins to a specific commit. Expect API changes until v0.1.0.

The function you are about to activate

A countdown as a loop — plain Go, a for that prints each tick and sleeps between them. What makes it durable is that the loop can run for minutes, hours, or days, and resume exactly where it left off if the worker process dies and comes back.

What this demonstrates

  • Registering a Go function as a named, durable workflow with resonate.Register.
  • Persisting each step with ctx.Run and suspending between ticks with ctx.Sleep — both durable across crashes.
  • Invoking a registered function by name from the Resonate CLI with resonate invoke.

These primitives are the foundation every Resonate program is built on, from a simple countdown to distributed sagas and durable sleep.

The code

func countdown(ctx *resonate.Context, args []int) (struct{}, error) {
    count, delay := args[0], args[1]
    for i := count; i > 0; i-- {
        // Run a function, persist its result
        f, _ := ctx.Run(notify, i)
        f.Await(nil)
        // Sleep
        s, _ := ctx.Sleep(time.Duration(delay) * time.Second)
        s.Await(nil)
    }
    fmt.Println("Done!")
    return struct{}{}, nil
}

func notify(i int) (struct{}, error) {
    fmt.Printf("Countdown: %d\n", i)
    return struct{}{}, nil
}

func main() {
    r, _ := resonate.New(resonate.Config{URL: "http://localhost:8001"})
    resonate.Register(r, "countdown", countdown)
    // ... keep the worker alive to receive CLI-invoked work
}

Two things worth knowing:

  • The workflow's args arrive as a []int slice — the CLI passes each --arg as one element of a JSON array, so --arg 5 --arg 60 becomes args[0]=5, args[1]=60.
  • notify is wrapped in ctx.Run so its result is recorded in a durable promise. On resume after a crash, already-printed ticks short-circuit instead of re-printing.

The durable call graph

Every iteration of the loop creates two durable promises — one for the ctx.Run step, one for the ctx.Sleep. While the countdown is running, inspect them with resonate tree:

resonate tree countdown.1
countdown.1
├── countdown.1.1 🟢 (run)
└── countdown.1.2 🟡 (sleep)

countdown.1 is the root invocation. countdown.1.1 is the resolved notify run; countdown.1.2 is the sleep still in progress. Kill the worker now and the green promise stays resolved — Resonate never re-runs work it has already recorded, so the countdown resumes from the pending sleep rather than starting over.

Prerequisites

  • Go 1.22+
  • The Resonate CLI (it ships the dev server and the invoke command):
brew install resonatehq/tap/resonate

Prefer a prebuilt binary? Download one from the releases page.

Setup

git clone https://github.com/resonatehq-examples/example-quickstart-go.git
cd example-quickstart-go
go mod download

Run it

Start the server:

resonate dev

Start the worker (it registers countdown and waits for work):

go run .

Invoke the function with execution ID countdown.1:

resonate invoke countdown.1 --func countdown --arg 5 --arg 60

Expected worker output:

Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Done!

What to try next

Once the countdown is working, here are natural next steps in rough order of complexity:

File structure

example-quickstart-go/
├── main.go        program entry point
├── go.mod         module declaration + SDK pin
├── go.sum         checksums
├── LICENSE        Apache-2.0
└── README.md

Community

License

Apache-2.0

About

Get started with the Resonate Go SDK — register a function, run it durably with no external server.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors

Languages