mesos-operator-api-client v0.3.0
mesos-operator-api-client
A generic client to listen to the Mesos Operator API's events.
Usage
Install as a dependency like this:
npm install mesos-operator-api-client --saveMaster client
The Master client can be used to connect to the Mesos Operator API like.
Events
Known Mesos Operator API events
As of the Mesos Operator API documentation and the Mesos master.proto there are currently the following events:
SUBSCRIBEDTASK_ADDEDTASK_UPDATEDAGENT_ADDEDAGENT_REMOVED
Those events will be emitted by this client as well if they occur.
Internal events
The Mesos Operator API events slient itself emits the following events:
subscribed: Is emitted after a successful subscription to the Mesos Operator API.unsubscribed: Is emitted afterunsubscribe()is called.reconciled: Is emitted afterreconcile()is called. This queries the Operator API with a separate call to theGET_STATEmethod.error: Is emitted in case of internal or upstream errors.
Using the client
Options
You can specify the following properties when instantiating the Mesos Operator API events client:
masterHost: The Mesos Master hostname (or ip address). Default isleader.mesos.masterPort: The Mesos Master port. Default is5050.masterProtocol: The Mesos Operator API protocol (httporhttps). Default ishttp.masterApiUri: The relative path where the Mesos Operator API endpoint can be found. Default is/api/v1.masterConnectionTimeout: The time in milliseconds after which the connection to the Mesos Master is deemed as timed out. Default is5000.eventTypes: Anarrayof event types emitted by the Mesos Master (see above for a list). Default is["SUBSCRIBED", "TASK_ADDED", "TASK_UPDATED", "AGENT_ADDED", "AGENT_REMOVED"].handlers: A map object consisting of handler functions for the individual Mesos Operator API events. See below for an explanation. No defaults.
Methods for events
The Mesos Operator API events client only exposes the subscribe() and the unsubscribe() methods. You can catch all above events via on(<eventType>, function (data) { ... }.
Supported methods for specific Operator API calls
The callback(error, data) function is optional, you need to add it only if you directly want to handle the results. Otherwise, those methods will trigger an event (starting with received_ appended by the lowercase method name, e.g. for GET_STATE is received_get_state), which applications can listen to to receive the responses in an asynchronous way.
getHealth(callback): This calls theGET_HEALTHmethod.getFlags(callback): This calls theGET_FLAGSmethod.getVersion(callback): This calls theGET_VERSIONmethod.getMetrics(callback): This calls theGET_METRICSmethod.getLoggingLevel(callback): This calls theGET_LOGGING_LEVELmethod.getState(callback): This calls theGET_STATEmethod.getAgents(callback): This calls theGET_AGENTSmethod.getFrameworks(callback): This calls theGET_FRAMEWORKSmethod.getExecutors(callback): This calls theGET_EXECUTORSmethod.getTasks(callback): This calls theGET_TASKSmethod.getRoles(callback): This calls theGET_ROLESmethod.getWeights(callback): This calls theGET_WEIGHTSmethod.getMaster(callback): This calls theGET_MASTERmethod.getMaintenanceStatus(callback): This calls theGET_MAINTENANCE_STATUSmethod.getMaintenanceSchedule(callback): This calls theGET_MAINTENANCE_SCHEDULEmethod.getQuota(callback): This calls theGET_QUOTAmethod.
Event handler functions
The custom event handler functions can be configured by setting a map object as handlers property during the instantiation. Each map object's property represents a event handling function. The property name needs to match on of the Marathon event types from the list of known Marathon events.
This is an example handlers map object:
{ // Specify the custom event handlers
"TASK_ADDED": function (data) {
console.log("We have a new TASK_ADDED event!");
},
"TASK_UPDATED": function (data) {
console.log("We have a new TASK_UPDATED event!");
}
}The function arguments are:
data: The emitted data for the respective event
Example code
For a complete example, have a look at examples/masterExample.js.
"use strict";
// Use the MesosOperatorApiClient
const MasterClient = require("mesos-operator-api-client");
// Create MesosOperatorApiClient instance
const eventsClient = new MasterClient({
masterHost: "172.17.11.101" // Replace with your Mesos Leader hostname or ip address
});
// Wait for "subscribed" event
eventsClient.on("subscribed", function () {
console.log("Subscribed to the Mesos Operator API events!");
// Call GET_AGENTS
eventsClient.getAgents(function (err, data) {
console.log("Got result for GET_AGENTS");
console.log(JSON.stringify(data));
});
// Do a reconcile after 3000ms. Demo!
setTimeout(function () {
eventsClient.reconcile();
}, 3000);
});
// Wait for "unsubscribed" event
eventsClient.on("unsubscribed", function () {
console.log("Unsubscribed from the Mesos Operator API events!");
});
// Catch error events
eventsClient.on("error", function (errorObj) {
console.log("Got an error");
console.log(JSON.stringify(errorObj));
});
// Log SUBSCRIBED event
eventsClient.on("SUBSCRIBED", function (eventObj) {
console.log("Got SUBSCRIBED");
console.log(JSON.stringify(eventObj));
});
// Log TASK_ADDED event
eventsClient.on("TASK_ADDED", function (eventObj) {
console.log("Got TASK_ADDED");
console.log(JSON.stringify(eventObj));
});
// Log TASK_UPDATED event
eventsClient.on("TASK_UPDATED", function (eventObj) {
console.log("Got TASK_UPDATED");
console.log(JSON.stringify(eventObj));
});
// Log AGENT_ADDED event
eventsClient.on("AGENT_ADDED", function (eventObj) {
console.log("Got AGENT_ADDED");
console.log(JSON.stringify(eventObj));
});
// Log AGENT_REMOVED event
eventsClient.on("AGENT_REMOVED", function (eventObj) {
console.log("Got AGENT_REMOVED");
console.log(JSON.stringify(eventObj));
});
// Subscribe to Mesos Operator API events
eventsClient.subscribe();
// Unsubscribe after 10sec. Demo!
setTimeout(function () {
eventsClient.unsubscribe();
}, 10000);Agent client
Using the client
Options
You can specify the following properties when instantiating the Mesos Operator API events client:
agentHost: The Mesos Agent hostname (or ip address). Default is127.0.0.1.agentPort: The Mesos Agent port. Default is5051.agentProtocol: The Mesos Operator API protocol (httporhttps). Default ishttp.agentApiUri: The relative path where the Mesos Operator API endpoint can be found. Default is/api/v1.agentConnectionTimeout: The time in milliseconds after which the connection to the Mesos Agent is deemed as timed out. Default is5000.
Example code
For a complete example, have a look at examples/agentExample.js.
"use strict";
// Use the agentClient
const AgentClient = require("mesos-operator-api-client").agentClient;
// Create agentClient instance
const agent = new AgentClient({
agentHost: "172.17.11.102"
});
// Call GET_HEALTH
agent.getHealth(function (err, data) {
console.log(JSON.stringify(data));
});Supported methods for specific Operator API calls
The callback(error, data) function is optional, you need to add it only if you directly want to handle the results. Otherwise, those methods will trigger an event (starting with received_ appended by the lowercase method name, e.g. for GET_STATE is received_get_state), which applications can listen to to receive the responses in an asynchronous way.
getHealth(callback): This calls theGET_HEALTHmethod.getFlags(callback): This calls theGET_FLAGSmethod.getVersion(callback): This calls theGET_VERSIONmethod.getMetrics(callback): This calls theGET_METRICSmethod.getState(callback): This calls theGET_STATEmethod.getContainers(callback): This calls theGET_CONTAINERSmethod.getFrameworks(callback): This calls theGET_FRAMEWORKSmethod.getExecutors(callback): This calls theGET_EXECUTORSmethod.getTasks(callback): This calls theGET_TASKSmethod.getAgent(callback): This calls theGET_AGENTmethod.