|
1 | 1 | # Choria Lifecycle Events
|
2 | 2 |
|
3 |
| -This package create and view Choria Lifecycle Events |
| 3 | +This package create and view Choria Lifecycle Events |
| 4 | + |
| 5 | +These lifecycle events are published to the `choria.lifecycle.event.<type>.<component>` topic structure of the middleware and contains small JSON documents that informs listeners about significant life cycle events of Choria components. |
| 6 | + |
| 7 | +[](https://godoc.org/github.com/choria-io/go-lifecycle) [](https://circleci.com/gh/choria-io/go-lifecycle/tree/master) |
| 8 | + |
| 9 | +## Supported Events |
| 10 | + |
| 11 | +|Event|Description| |
| 12 | +|-----|-----------| |
| 13 | +|Startup|Event to emit when components start, requires `Identity()`, `Component()` and `Version()` options| |
| 14 | +|Shutdown|Event to emit when components shut down, requires `Identity()` and `Component()` options| |
| 15 | + |
| 16 | +#### Sample Events |
| 17 | +### Schemas |
| 18 | + |
| 19 | +Event Schemas are stored in the [Choria Schemas repository](https://github.com/choria-io/schemas/tree/master/choria/lifecycle). |
| 20 | + |
| 21 | +#### Startup |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "protocol":"choria:lifecycle:startup:1", |
| 26 | + "identity":"c1.example.net", |
| 27 | + "version":"0.6.0", |
| 28 | + "timestamp":1535369537, |
| 29 | + "component":"server" |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### Shutdown |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "protocol":"choria:lifecycle:shutdown:1", |
| 38 | + "identity":"c1.example.net", |
| 39 | + "component":"server", |
| 40 | + "timestamp":1535369536 |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Viewing events |
| 45 | + |
| 46 | +In a shell configured as a Choria Client run `choria tool event` to view events in real time. |
| 47 | + |
| 48 | +These events do not traverse Federation borders, so you have to view them in the network you care to observe. You can though configure a Choria Adapter to receive them and adapt them onto a NATS Stream from where you can replicate them to other data centers. |
| 49 | + |
| 50 | +## Emitting an event |
| 51 | + |
| 52 | +```go |
| 53 | +event, err := lifecycle.New(lifecycle.Startup, lifecycle.Identity("my.identity"), lifecycle.Component("my_app"), lifecycle.Version("0.0.1")) |
| 54 | +panicIfErr(err) |
| 55 | + |
| 56 | +// conn is a Choria connector |
| 57 | +err = lifecycle.PublishEvent(event, conn) |
| 58 | +``` |
| 59 | + |
| 60 | +If you are emitting `lifecycle.Shutdown` events right before exiting be sure to call `conn.Close()` so the buffers are flushed prior to shutdown. |
| 61 | + |
| 62 | +## Receiving a events |
| 63 | + |
| 64 | +These events are used to orchestrate associated tools like the [Provisioning Server](https://github.com/choria-io/provisioning-agent) that listens for these events and immediately add a new node to the provisioning queue. |
| 65 | + |
| 66 | +To receive `startup` events for the `server`: |
| 67 | + |
| 68 | +```go |
| 69 | +events := make(chan *choria.ConnectorMessage, 1000) |
| 70 | + |
| 71 | +// conn is a choria framework connector |
| 72 | +// fw is the choria framework |
| 73 | +err = conn.QueueSubscribe(ctx, fw.NewRequestID(), "choria.lifecycle.event.startup.server", "", events) |
| 74 | +panicIfError(err) |
| 75 | + |
| 76 | +for { |
| 77 | + select { |
| 78 | + case e := <-events: |
| 79 | + event, err := lifecycle.NewFromJSON(e.Data) |
| 80 | + if err != nil { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + fmt.Printf("Received a startup from %s", event.Identity()) |
| 85 | + case <-ctx.Done(): |
| 86 | + return |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
0 commit comments