package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/pancsta/asyncmachine-go/examples/arpc/states"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
arpc "github.com/pancsta/asyncmachine-go/pkg/rpc"
)
var ss = states .ExampleStates
func init() {
}
func main() {
ctx , cancel := context .WithCancel (context .Background ())
defer cancel ()
sigChan := make (chan os .Signal , 1 )
signal .Notify (sigChan , syscall .SIGINT , syscall .SIGTERM )
for i := range 3 {
worker , err := newWorker (ctx , i )
if err != nil {
log .Print (err )
}
time .Sleep (100 * time .Millisecond )
fmt .Printf ("start %d\n" , i )
worker .Log ("hello" )
}
<-sigChan
fmt .Println ("bye" )
}
func newWorker(ctx context .Context , num int ) (*am .Machine , error ) {
handlers := &workerHandlers {}
id := fmt .Sprintf ("worker%d" , num )
worker , err := am .NewCommon (ctx , id , states .ExampleSchema ,
ss .Names (), handlers , nil , nil )
if err != nil {
return nil , err
}
handlers .Mach = worker
amhelp .MachDebugEnv (worker )
worker .SemLogger ().SetLevel (am .LogChanges )
arpc .MachRepl (worker , "" , "tmp" , nil , nil )
return worker , nil
}
type workerHandlers struct {
*am .ExceptionHandler
Mach *am .Machine
}
func (h *workerHandlers ) FooState (e *am .Event ) {
fmt .Println ("FooState" )
h .Mach .Log ("FooState" )
}
func (h *workerHandlers ) BarState (e *am .Event ) {
fmt .Println ("BarState" )
h .Mach .Log ("BarState" )
}
func (h *workerHandlers ) BazState (e *am .Event ) {
fmt .Println ("BazState" )
h .Mach .Log ("BarState" )
}
The pages are generated with Golds v0.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 .