Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This gets us closer to a workable api. It still has zero docs.
- Loading branch information
Showing
5 changed files
with
306 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
modules/messaging/src/main/clojure/immutant/messaging/pipeline.clj
This file was deleted.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
modules/messaging/src/main/clojure/immutant/pipeline.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
;; Copyright 2008-2012 Red Hat, Inc, and individual contributors. | ||
;; | ||
;; This is free software; you can redistribute it and/or modify it | ||
;; under the terms of the GNU Lesser General Public License as | ||
;; published by the Free Software Foundation; either version 2.1 of | ||
;; the License, or (at your option) any later version. | ||
;; | ||
;; This software is distributed in the hope that it will be useful, | ||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;; Lesser General Public License for more details. | ||
;; | ||
;; You should have received a copy of the GNU Lesser General Public | ||
;; License along with this software; if not, write to the Free | ||
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
;; 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
|
||
(ns immutant.pipeline | ||
(:use [immutant.util :only (mapply)]) | ||
(:require [clojure.tools.logging :as log] | ||
[immutant.messaging :as msg])) | ||
|
||
(def ^:dynamic *pipeline* nil) | ||
(def ^:dynamic *current-step* nil) | ||
(def ^:dynamic *next-step* nil) | ||
|
||
(def ^:private pipelines (atom #{})) | ||
|
||
(defn- wrap-error-handler [f {:keys [error-handler]}] | ||
(if error-handler | ||
(fn [m] | ||
(try | ||
(f m) | ||
(catch Exception e | ||
(error-handler e m)))) | ||
f)) | ||
|
||
(defn- wrap-step-bindings [f current next] | ||
(binding [*current-step* current | ||
*next-step* next] | ||
(bound-fn* f))) | ||
|
||
(defn- pipeline-listen [pl opts f] | ||
(let [{:keys [step next-step]} (meta f) | ||
opts (-> opts | ||
(merge (meta f)) | ||
(assoc :selector | ||
(str "step = '" step "'"))) | ||
wrapped-f (-> f | ||
(wrap-error-handler opts) | ||
(wrap-step-bindings step next-step))] | ||
(mapply msg/listen | ||
pl | ||
(if next-step | ||
#(msg/publish pl (wrapped-f %) | ||
:properties {"step" next-step}) | ||
wrapped-f) | ||
opts))) | ||
|
||
(defn- pipeline-fn [pl entry] | ||
(vary-meta | ||
(fn [m & {:keys [step] :or {step entry}}] | ||
(msg/publish pl m :properties {"step" step})) | ||
assoc | ||
:pipeline pl)) | ||
|
||
(defn- named-steps [fns] | ||
(let [step-names (-> (map-indexed (fn [n s] | ||
(str (or (:name (meta s)) n))) fns) | ||
vec | ||
(conj nil))] | ||
(map | ||
(fn [f [step next-step]] | ||
(vary-meta f assoc :step step :next-step next-step)) | ||
fns (partition 2 1 step-names)))) | ||
|
||
(defn pipeline [name & args] | ||
(let [steps (named-steps (take-while fn? args)) | ||
{:as opts} (drop-while fn? args) | ||
pl (str "queue.pipeline-" name) | ||
pl-fn (pipeline-fn pl (-> steps first meta :step))] | ||
(if (some #{pl} @pipelines) | ||
(throw (IllegalArgumentException. | ||
(str "A pipeline named " name " already exists.")))) | ||
(mapply msg/start pl opts) | ||
(binding [*pipeline* pl-fn] | ||
(let [listeners (->> steps | ||
(map (partial pipeline-listen pl opts)) | ||
doall)] | ||
(swap! pipelines conj pl) | ||
(vary-meta pl-fn assoc :listeners listeners))))) | ||
|
||
(defn step [f & {:as opts}] | ||
(vary-meta f merge opts)) | ||
|
||
(defn stop [pl & args] | ||
(let [{:keys [pipeline listeners]} (meta pl)] | ||
(swap! pipelines disj pipeline) | ||
(doseq [l listeners] | ||
(msg/unlisten l)) | ||
(apply msg/stop pipeline args))) |
Oops, something went wrong.