generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# FTL fsm example | ||
|
||
Run using: | ||
```sh | ||
> call fsm.sendEvent '{"id":"door3", "event":"open"}' | ||
debug:fsm:runner5: The door is open. | ||
|
||
> call fsm.sendEvent '{"id":"door3", "event":"jam"}' | ||
debug:fsm:runner5: The door is jammed. Fixing... | ||
|
||
> call fsm.sendEvent '{"id":"door3", "event":"unlock"}' | ||
debug:fsm:runner5: The door is unlocked. | ||
|
||
> call fsm.sendEvent '{"id":"door3", "event":"lock"}' | ||
debug:fsm:runner5: The door is locked. | ||
|
||
> call fsm.sendEvent '{"id":"door3", "event":"jam"}' | ||
debug:fsm:runner5: The door is jammed. Fixing... | ||
|
||
> call fsm.sendEvent '{"id":"door3", "event":"lock"}' | ||
error:fsm:runner5: Call to deployments dpl-fsm-7vye13artgx3hkx failed: call to verb fsm.sendEvent failed: failed to send fsm event: failed_precondition: no transition found from state fsm.openDoor for type fsm.Locked, candidates are unlockDoor, jamDoor | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package fsm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK. | ||
) | ||
|
||
type Opened struct{} | ||
type Unlocked struct{} | ||
type Locked struct{} | ||
type Jammed struct{} | ||
|
||
var door = ftl.FSM( | ||
"door", | ||
ftl.Start(OpenDoor), | ||
ftl.Transition(OpenDoor, UnlockDoor), | ||
ftl.Transition(OpenDoor, JamDoor), | ||
ftl.Transition(UnlockDoor, OpenDoor), | ||
ftl.Transition(UnlockDoor, LockDoor), | ||
ftl.Transition(UnlockDoor, JamDoor), | ||
ftl.Transition(LockDoor, UnlockDoor), | ||
ftl.Transition(LockDoor, JamDoor), | ||
ftl.Transition(JamDoor, OpenDoor), | ||
) | ||
|
||
//ftl:verb | ||
func OpenDoor(ctx context.Context, in Opened) error { | ||
fmt.Println("The door is open.") | ||
return nil | ||
} | ||
|
||
//ftl:verb | ||
func UnlockDoor(ctx context.Context, in Unlocked) error { | ||
fmt.Println("The door is unlocked.") | ||
return nil | ||
} | ||
|
||
//ftl:verb | ||
func LockDoor(ctx context.Context, in Locked) error { | ||
fmt.Println("The door is locked.") | ||
return nil | ||
} | ||
|
||
//ftl:verb | ||
func JamDoor(ctx context.Context, in Jammed) error { | ||
fmt.Println("The door is jammed. Fixing...") | ||
ftl.FSMNext(ctx, Opened{}) | ||
return nil | ||
} | ||
|
||
//ftl:enum | ||
type Event string | ||
|
||
const ( | ||
Open Event = "open" | ||
Unlock Event = "unlock" | ||
Lock Event = "lock" | ||
Jam Event = "jam" | ||
) | ||
|
||
type SendEventRequest struct { | ||
ID string | ||
Event Event | ||
} | ||
|
||
//ftl:verb export | ||
func SendEvent(ctx context.Context, req SendEventRequest) error { | ||
switch req.Event { | ||
case Open: | ||
return door.Send(ctx, req.ID, Opened{}) | ||
case Unlock: | ||
return door.Send(ctx, req.ID, Unlocked{}) | ||
case Lock: | ||
return door.Send(ctx, req.ID, Locked{}) | ||
case Jam: | ||
return door.Send(ctx, req.ID, Jammed{}) | ||
default: | ||
return fmt.Errorf("unknown event: %s", req.Event) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module = "fsm" | ||
language = "go" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module ftl/fsm | ||
|
||
go 1.23.0 | ||
|
||
replace github.com/TBD54566975/ftl => ../../.. | ||
|
||
require github.com/TBD54566975/ftl v0.0.0-00010101000000-000000000000 | ||
|
||
require ( | ||
connectrpc.com/connect v1.16.2 // indirect | ||
connectrpc.com/grpcreflect v1.2.0 // indirect | ||
connectrpc.com/otelconnect v0.7.1 // indirect | ||
github.com/XSAM/otelsql v0.34.0 // indirect | ||
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect | ||
github.com/alecthomas/concurrency v0.0.2 // indirect | ||
github.com/alecthomas/participle/v2 v2.1.1 // indirect | ||
github.com/alecthomas/types v0.16.0 // indirect | ||
github.com/alessio/shellescape v1.4.2 // indirect | ||
github.com/benbjohnson/clock v1.3.5 // indirect | ||
github.com/danieljoos/wincred v1.2.0 // indirect | ||
github.com/deckarep/golang-set/v2 v2.6.0 // indirect | ||
github.com/go-logr/logr v1.4.2 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/godbus/dbus/v5 v5.1.0 // indirect | ||
github.com/hashicorp/cronexpr v1.1.2 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect | ||
github.com/jackc/pgx/v5 v5.7.1 // indirect | ||
github.com/jackc/puddle/v2 v2.2.2 // indirect | ||
github.com/jpillora/backoff v1.0.0 // indirect | ||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/multiformats/go-base36 v0.2.0 // indirect | ||
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect | ||
github.com/swaggest/jsonschema-go v0.3.72 // indirect | ||
github.com/swaggest/refl v1.3.0 // indirect | ||
github.com/zalando/go-keyring v0.2.5 // indirect | ||
go.opentelemetry.io/otel v1.30.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.30.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.30.0 // indirect | ||
golang.org/x/crypto v0.27.0 // indirect | ||
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect | ||
golang.org/x/mod v0.21.0 // indirect | ||
golang.org/x/net v0.29.0 // indirect | ||
golang.org/x/sync v0.8.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
golang.org/x/text v0.18.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
) |
Oops, something went wrong.