|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/cschleiden/go-workflows/backend" |
| 12 | + "github.com/cschleiden/go-workflows/backend/sqlite" |
| 13 | + "github.com/cschleiden/go-workflows/client" |
| 14 | + "github.com/cschleiden/go-workflows/web" |
| 15 | + |
| 16 | + // "github.com/cschleiden/go-workflows/web" |
| 17 | + "github.com/cschleiden/go-workflows/worker" |
| 18 | + |
| 19 | + "github.com/google/uuid" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + ctx, cancel := context.WithCancel(context.Background()) |
| 24 | + |
| 25 | + // b := sqlite.NewSqliteBackend("simple.sqlite") |
| 26 | + b := sqlite.NewInMemoryBackend() |
| 27 | + |
| 28 | + //b := mysql.NewMysqlBackend("localhost", 3306, "root", "test", "simple") |
| 29 | + |
| 30 | + // b, err := redis.NewRedisBackend("localhost:6379", "", "RedisPassw0rd", 0) |
| 31 | + // if err != nil { |
| 32 | + // panic(err) |
| 33 | + // } |
| 34 | + |
| 35 | + // Start diagnostic server |
| 36 | + go http.ListenAndServe(":8080", web.NewMux(b)) |
| 37 | + |
| 38 | + // Run worker |
| 39 | + w := RunWorker(ctx, b) |
| 40 | + |
| 41 | + // Start workflow via client |
| 42 | + c := client.New(b) |
| 43 | + |
| 44 | + runWorkflow(ctx, c) |
| 45 | + |
| 46 | + sigint := make(chan os.Signal, 1) |
| 47 | + signal.Notify(sigint, os.Interrupt) |
| 48 | + <-sigint |
| 49 | + |
| 50 | + cancel() |
| 51 | + |
| 52 | + if err := w.WaitForCompletion(); err != nil { |
| 53 | + panic("could not stop worker" + err.Error()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func runWorkflow(ctx context.Context, c client.Client) { |
| 58 | + wf, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{ |
| 59 | + InstanceID: uuid.NewString(), |
| 60 | + }, Workflow1, "Hello world"+uuid.NewString(), 42, Inputs{ |
| 61 | + Msg: "", |
| 62 | + Times: 0, |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + log.Fatal(err) |
| 66 | + panic("could not start workflow") |
| 67 | + } |
| 68 | + |
| 69 | + result, err := client.GetWorkflowResult[int](ctx, c, wf, time.Second*10) |
| 70 | + if err != nil { |
| 71 | + log.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + log.Println("Workflow finished. Result:", result) |
| 75 | +} |
| 76 | + |
| 77 | +func RunWorker(ctx context.Context, mb backend.Backend) worker.Worker { |
| 78 | + w := worker.New(mb, nil) |
| 79 | + |
| 80 | + w.RegisterWorkflow(Workflow1) |
| 81 | + |
| 82 | + w.RegisterActivity(Activity1) |
| 83 | + |
| 84 | + if err := w.Start(ctx); err != nil { |
| 85 | + panic("could not start worker") |
| 86 | + } |
| 87 | + |
| 88 | + return w |
| 89 | +} |
0 commit comments