clojusc / meson

Clojure Client Library for the Mesos HTTP API
Apache License 2.0
9 stars 10 forks source link

Define a best-practice and mechanism for event handlers #53

Closed oubiwann closed 8 years ago

oubiwann commented 8 years ago

This will essentially be how developers will define Mesos HTTP APIs in Meson for events. In particular, it needs to provide an example (e.g., defmulti and defmethods) for how to dispatch callbacks based on a message type parsed from the stream.

Subtask of feature #56.

oubiwann commented 8 years ago

We have adopted the same approach used by @pyr in Mesomatic: define callbacks for use in an async/reduce call by means of defmulti and defmethod.

How this gets included in the codebase is another matter. Perhaps default, no-op+logging methods that can be used by developers to get started, to serve as a starting point when they need to override one or more of the event-handling methods for any of the SUBSCRIBE-enabled APIs (scheduler, executor, operator).

Here is a sample of what I've been testing with in the scheduler HTTP API:

(defmulti handle-msg
  ""
  (comp :type last vector))

(defmethod handle-msg :subscribed
  [state msg]
  (log/debug "Got SUBSCRIBED message.")
  (log/trace msg))

(defmethod handle-msg :heartbeat
  [state msg]
  (log/debug "Got HEARTBEAT message.")
  (log/trace msg))

(defmethod handle-msg :offers
  [state msg]
  (log/debug "Got OFFERS message.")
  (log/trace msg))

(defmethod handle-msg :default
  [state msg]
  (log/error "Unknown type ...")
  (log/trace msg))

As the ultimate location for these methods will be defined in the individual API feature tasks, and as the best practice id defined, I'm closing this ticket now.