npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

marathon-event-bus-client

v0.1.7

Published

Listen to Marathon's Event Bus, and send events to event handler implementations

Downloads

3,537

Readme

marathon-event-bus-client

Package version Package downloads Package license Build Status

A generic client to listen to Marathon's (Server Sent) Event Bus.

Usage

Install as a dependency like this:

npm install marathon-event-bus-client --save

Events

Known Marathon events

As of the Marathon sources there are currently the following events:

  • pod_created_event
  • pod_updated_event
  • pod_deleted_event
  • scheduler_registered_event
  • scheduler_reregistered_event
  • scheduler_disconnected_event
  • subscribe_event
  • unsubscribe_event
  • event_stream_attached
  • event_stream_detached
  • add_health_check_event
  • remove_health_check_event
  • failed_health_check_event
  • health_status_changed_event
  • unhealthy_task_kill_event
  • group_change_success
  • group_change_failed
  • deployment_info
  • deployment_success
  • deployment_failed
  • deployment_step_success
  • deployment_step_failure
  • app_terminated_event
  • status_update_event
  • instance_changed_event
  • unknown_instance_terminated_event
  • instance_health_changed_event
  • framework_message_event

Internal events

The Marathon Event Bus Client itself emits the following events:

  • subscribed: Is emitted after a successful subscription to the Marathon Event Bus.
  • unsubscribed: Is emitted after unsubscribe() is called.
  • error: Is emitted in case of internal or upstream errors.

Using the client

Options

You can specify the following properties when instantiating the Marathon Event Bus Client:

  • marathonHost: The Marathon base URL. Default is master.mesos.
  • marathonPort: The Marathon port. Default is 8080.
  • marathonProtocol: The Marathon protocol (http or https). Default is http.
  • marathonUri: The relative path where the Marathon Event Bus endpoint can be found. Default is /v2/events.
  • marathonHeaders: A dict of http headers which should be added to Marathon's API requests , mainly for authentication/authorization (both Authorization and Content-Type are allowed). See the Marathon docs or the DC/OS docs for further info. Not used by default.
  • eventTypes: An array of event types emitted by Marathon (see above for a list). Default is ["deployment_info", "deployment_success", "deployment_failed"].
  • handlers: A map object consisting of handler functions for the individual Marathon events. See below for an explanation. No defaults.

Methods

The Marathon Event Bus Client only exposes the subscribe() and the unsubscribe() methods. You can catch all above events via on(<eventType>, function (data) { ... }.

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
    "deployment_info": function (name, data) {
        console.log("We have a new deployment info!");
    },
    "deployment_success": function (name, data) {
        console.log("Our deployment was successful!");
    }
}

The function arguments are:

  • name: The name of the emitted event
  • data: The emitted data for the event

Example code

For a complete example, have a look at examples/example.js. Also, for a "real-life example", you can refer to marathon-slack.

// Use the MarathonEventBusClient
const MarathonEventBusClient = require("marathon-event-bus-client");

// Define relevant event types
const eventTypes = ["deployment_info", "deployment_success", "deployment_failed"];

// Create MarathonEventBusClient instance
const mebc = new MarathonEventBusClient({
    marathonHost: "localhost", // Use SSE test server
    eventTypes: eventTypes,
    marathonHeaders: { // When using the Marathon Event Bus outside the cluster, otherwise just omit the marathonHeaders.
        "Authorization": "token=<authentication-token>" // Replace <authentication-token> with a real authentication token
    },
    handlers: { // Specify the custom event handlers
        "deployment_info": function (name, data) {
            console.log("Custom handler for " + name);
            // Send information of the "deployment_info" event to an external service (here: Just an echo service)
            request("https://echo.getpostman.com/get?name=" + name + "&startTime=" + data.timestamp, function (error, response, body) {
                body = JSON.parse(body);
                if (!error && response.statusCode == 200) {
                    console.log("Here's the data we have just sent to the echo service:");
                    console.log("--------------------");
                    console.log(JSON.stringify(body.args)); // Show the sent data
                    console.log("--------------------");
                }
            });
        },
        "deployment_success": function (name, data) {
            console.log("Custom handler for " + name);
        }
    }
});

// Wait for "connected" event
mebc.on("connected", function () {

    console.log("Subscribed to the Marathon Event Bus");

    // For example purposes: Log all events we receive
    // In real-world usage, you should define what needs to be done when
    // receiving specific events in the `handlers` property for each event type
    eventTypes.forEach(function (eventType) {
        mebc.on(eventType, function (data) {
            console.log("Caught '" + eventType + "' event!");
        });
    });

    // Shutdown after 30 seconds
    setTimeout(function () {
        console.log("Shutting down");
        // Unsubscribe from Event Bus
        mebc.unsubscribe();
    }, 30000);

});

// Wait for "unsubscribed" event
mebc.on("unsubscribed", function () {
    console.log("Unsubscribed from the Marathon Event Bus");
});

// Catch error events
mebc.on("error", function (errorObj) {
    console.log("Got an error on " + errorObj.timestamp + ":");
    console.log(JSON.stringify(errorObj.error));
});

// Subscribe to Marathon Event Bus
mebc.subscribe();