()
| 30 | } |
| 31 | |
| 32 | func Example_server() { |
| 33 | if os.Getenv("ETCD_ADDR") == "" { |
| 34 | fmt.Println("set ETCD_ADDR to run this example") |
| 35 | return |
| 36 | } |
| 37 | c := core.Default(core.WithInline("log.level", "none")) |
| 38 | c.Provide(otetcd.Providers()) |
| 39 | c.Provide(leader.Providers()) |
| 40 | c.Invoke(func(dispatcher contract.Dispatcher) { |
| 41 | // This listener will be called twice. Once on becoming the leader and once on resigning the leader. |
| 42 | dispatcher.Subscribe(events.Listen(leader.OnStatusChanged, func(ctx context.Context, event interface{}) error { |
| 43 | fmt.Println(event.(leader.OnStatusChangedPayload).Status.IsLeader()) |
| 44 | return nil |
| 45 | })) |
| 46 | }) |
| 47 | c.Invoke(func(sts *leader.Status) { |
| 48 | c.AddModule(ServerModule{Sts: sts}) |
| 49 | }) |
| 50 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 51 | defer cancel() |
| 52 | |
| 53 | c.Serve(ctx) |
| 54 | |
| 55 | // Output: |
| 56 | // true |
| 57 | // false |
| 58 | } |
nothing calls this directly
no test coverage detected