Skip to main content
Reference vendor/dbos 1 min read

comm-events


title: Use Events for Workflow Status Publishing impact: MEDIUM impactDescription: Enables real-time progress monitoring and interactive workflows tags: communication, events, status, key-value

Use Events for Workflow Status Publishing

Workflows can publish events (key-value pairs) with dbos.SetEvent. Other code can read events with dbos.GetEvent. Events are persisted and useful for real-time progress monitoring.

Incorrect (using external state for progress):

var progress int // Global variable - not durable!

func processData(ctx dbos.Context, input string) (string, error) {
	progress = 50 // Not persisted, lost on restart
	return input, nil
}

Correct (using events):

func processData(ctx dbos.Context, input string) (string, error) {
	dbos.SetEvent(ctx, "status", "processing")
	_, err := dbos.RunAsStep(ctx, stepOne, dbos.WithStepName("stepOne"))
	if err != nil {
		return "", err
	}
	dbos.SetEvent(ctx, "progress", 50)
	_, err = dbos.RunAsStep(ctx, stepTwo, dbos.WithStepName("stepTwo"))
	if err != nil {
		return "", err
	}
	dbos.SetEvent(ctx, "progress", 100)
	dbos.SetEvent(ctx, "status", "complete")
	return "done", nil
}

// Read events from outside the workflow
status, err := dbos.GetEvent[string](ctx, workflowID, "status", 60*time.Second)
progress, err := dbos.GetEvent[int](ctx, workflowID, "progress", 60*time.Second)

Events are useful for interactive workflows. For example, a checkout workflow can publish a payment URL for the caller to redirect to:

func checkoutWorkflow(ctx dbos.Context, order Order) (string, error) {
	paymentURL, err := dbos.RunAsStep(ctx, func(ctx context.Context) (string, error) {
		return createPayment(order)
	}, dbos.WithStepName("createPayment"))
	if err != nil {
		return "", err
	}
	dbos.SetEvent(ctx, "paymentURL", paymentURL)
	// Continue processing...
	return "success", nil
}

// HTTP handler starts workflow and reads the payment URL
handle, _ := dbos.RunWorkflow(ctx, checkoutWorkflow, order)
url, _ := dbos.GetEvent[string](ctx, handle.GetWorkflowID(), "paymentURL", 300*time.Second)

GetEvent blocks until the event is set or the timeout expires. It returns the zero value of the type and a timeout error if the timeout is reached. It takes a dbos.Client, so it also works from external applications using the DBOS Client (a dbos.Context satisfies Client).

Reference: Workflow Events