Skip to content

Commit 3e881d4

Browse files
authored
feat: add RunVoid syntactic sugar (#118)
restate.RunVoid is a shorter alternative to restate.Run[restate.Void]
1 parent 40299bd commit 3e881d4

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

facilitators.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,22 @@ func RunAsync[T any](ctx Context, fn func(ctx RunContext) (T, error), options ..
344344
}, options...)}
345345
}
346346

347+
// RunVoid runs the function (fn), storing final results (including terminal errors)
348+
// durably in the journal, or otherwise for transient errors stopping execution
349+
// so Restate can retry the invocation. Replays will produce the same value, so
350+
// all non-deterministic operations (eg, generating a unique ID) *must* happen
351+
// inside RunVoid blocks.
352+
//
353+
// This is similar to Run, but for functions that don't return a value.
354+
func RunVoid(ctx Context, fn func(ctx RunContext) error, options ...options.RunOption) error {
355+
var output Void
356+
err := ctx.inner().Run(func(ctx RunContext) (any, error) {
357+
return nil, fn(ctx)
358+
}, &output, options...)
359+
360+
return err
361+
}
362+
347363
// RunAsyncFuture is a 'promise' for a RunAsync operation.
348364
type RunAsyncFuture[T any] interface {
349365
// Result blocks on receiving the RunAsync result, returning the value it was

test-services/failing.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ func init() {
3939
})).
4040
Handler("terminallyFailingSideEffect", restate.NewObjectHandler(
4141
func(ctx restate.ObjectContext, errorMessage string) (restate.Void, error) {
42-
return restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) {
43-
return restate.Void{}, restate.TerminalErrorf("%s", errorMessage)
42+
err := restate.RunVoid(ctx, func(ctx restate.RunContext) error {
43+
return restate.TerminalErrorf("%s", errorMessage)
4444
})
45+
return restate.Void{}, err
4546
})).
4647
Handler("sideEffectSucceedsAfterGivenAttempts", restate.NewObjectHandler(
4748
func(ctx restate.ObjectContext, minimumAttempts int32) (int32, error) {

test-services/interpreter.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ func interpret(ctx restate.ObjectContext, layer uint32, value Program) error {
128128
}
129129
break
130130
case SlowSideEffect:
131-
_, err := restate.Run[restate.Void](ctx, func(ctx restate.RunContext) (restate.Void, error) {
131+
err := restate.RunVoid(ctx, func(ctx restate.RunContext) error {
132132
time.Sleep(1 * time.Millisecond)
133-
return restate.Void{}, nil
133+
return nil
134134
})
135135
if err != nil {
136136
return err
@@ -145,11 +145,11 @@ func interpret(ctx restate.ObjectContext, layer uint32, value Program) error {
145145
}
146146
break
147147
case ThrowingSideEffect:
148-
_, err := restate.Run[restate.Void](ctx, func(ctx restate.RunContext) (restate.Void, error) {
148+
err := restate.RunVoid(ctx, func(ctx restate.RunContext) error {
149149
if rand.IntN(2) == 1 {
150-
return restate.Void{}, fmt.Errorf("Too many 'if err != nil', and no there's no feelings attached to it at all.")
150+
return fmt.Errorf("Too many 'if err != nil', and no there's no feelings attached to it at all.")
151151
}
152-
return restate.Void{}, nil
152+
return nil
153153
})
154154
if err != nil {
155155
return err

0 commit comments

Comments
 (0)