Skip to content

Instantly share code, notes, and snippets.

@flosell
Last active January 31, 2017 21:05
Show Gist options
  • Save flosell/ff9976e9744814d60fcbb7d06f488f71 to your computer and use it in GitHub Desktop.
Save flosell/ff9976e9744814d60fcbb7d06f488f71 to your computer and use it in GitHub Desktop.
Example on how one could implement notifications on build failures in LambdaCD
============ Build 2 failed! =============
Failed Step: (3 1 1 4) client-publish {:status :failure, :out Publishing to /tmp/mockrepo
cp: /tmp/mockrepo/client-snapshot.tar.gz: No such file or directory
, :exit 1}
Failed Step: (3 2 1 4) server-publish {:status :failure, :out Publishing to /tmp/mockrepo
cp: /tmp/mockrepo/server-snapshot.tar.gz: No such file or directory
, :exit 1}
(ns todopipeline.failure-notifications
"Example on how one could implement notifications on pipeline failure, e.g. to post to a slack channel"
(:require [clojure.core.async :as async]
[lambdacd.event-bus :as event-bus]
[lambdacd.presentation.pipeline-structure :as pipeline-structure]
[lambdacd.steps.result :as step-result]
[lambdacd.step-id :as step-id-ns]))
(defn- contains-child-of? [step-id potential-child-ids]
(some #(step-id-ns/parent-of? step-id %) potential-child-ids))
(defn- exclude-parents [step-ids]
(filter #(not (contains-child-of? % step-ids)) step-ids))
(defn- log-failure-event [event ctx]
(let [flattened-step-result-outputs (step-result/flatten-step-result-outputs (:outputs event))
failed-steps (->> flattened-step-result-outputs
(filter (fn [[_ result]] (not (= :success (:status result)))))
(map (fn [[step-id _]] step-id))
(exclude-parents)
(map #(pipeline-structure/step-display-representation-by-step-id (:pipeline-def ctx) %)))]
(println "============= Build" (:build-number event) "failed! =============")
(doall (for [failed-step failed-steps]
(println "Failed Step:"
(:step-id failed-step)
(:name failed-step)
(get flattened-step-result-outputs (:step-id failed-step)))))))
(defn start-failure-notifications [ctx]
(let [subscription (event-bus/subscribe ctx :pipeline-finished)
pipeline-finished-events (event-bus/only-payload subscription)]
(async/go-loop []
(let [event (async/<! pipeline-finished-events)]
(when (not (= :success (:status event)))
(log-failure-event event ctx)))
(recur))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment