Handling events
In this recipe, we will learn how Mesos notifies frameworks about updates.
How to do it...
To handle events, we need to parse data that we obtain from Mesos. To do that, add the following code at the end of the loop in the subscribe()
function:
var event Event sonpb.UnmarshalString(data, &event) log.Printf("Got: [%s]", event.String())
After parsing, we need to handle events that can vary in type. To do this, we will use the switch
control statement on the event type:
switch *event.Type { case Event_SUBSCRIBED: log.Print("Subscribed") frameworkInfo.Id = event.Subscribed.FrameworkId mesosStreamID = res.Header.Get("Mesos-Stream-Id") case Event_HEARTBEAT: log.Print("PING") }
Remember to declare the global mesosStreamID
variable:
var mesosStreamID string
How it works...
Events come with persistent HTTP connection one-by-one. Protobuf binding comes with the method to parse the data into Golang structs. Then in our event loop, we can perform specific actions...