package mainimport (amhelpam)func init() {// load .env _ = godotenv.Load()// am-dbg is required for debugging, go run it // go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest // amhelp.EnableDebugging(false) // amhelp.SetEnvLogLevel(am.LogOps)}func main() {// init the state machine := am.New(nil, am.Schema{"ProcessingFile": { // asyncRemove: am.S{"FileProcessed"}, },"FileProcessed": { // asyncRemove: am.S{"ProcessingFile"}, },"InProgress": { // syncAuto: true,Require: am.S{"ProcessingFile"}, }, }, &am.Opts{LogLevel: am.LogOps, Id: "raw-strings"})amhelp.MachDebugEnv() .BindHandlers(&Handlers{Filename: "README.md", })// change the state .Add1("ProcessingFile", nil)// wait for completedselect {case<-time.After(5 * time.Second):println("timeout")case<-.WhenErr(nil):println("err:", .Err())case<-.When1("FileProcessed", nil):println("done") }}typeHandlersstruct { Filename string}// negotiation handlerfunc ( *Handlers) ( *am.Event) bool {// read-only ops // decide if moving fwd is ok // no blocking // lock-free critical sectionreturntrue}// final handlerfunc ( *Handlers) ( *am.Event) {// read & write ops // no blocking // lock-free critical section := .Machine()// tick-based context := .NewStateCtx("ProcessingFile")gofunc() {// block in the background, locks neededif .Err() != nil {return// expired }// blocking call := processFile(.Filename, )if != nil { .AddErr(, nil)return }// re-check the tick ctx after a blocking callif .Err() != nil {return// expired }// move to the next state in the flow .Add1("FileProcessed", am.A{"beaver": "1"}) }()}func processFile( string, context.Context) error {time.Sleep(1 * time.Second)returnnil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.