Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

108 lines (94 sloc) 4.695 kB
(ns org.runa.swarmiji.sevak.sevak-core)
(use 'org.runa.swarmiji.mpi.transport)
(use 'org.rathore.amit.utils.rabbitmq)
(use 'org.runa.swarmiji.client.client-core)
(use 'org.runa.swarmiji.config.system-config)
(use 'org.runa.swarmiji.utils.general-utils)
(use 'org.runa.swarmiji.sevak.bindings)
(use 'org.rathore.amit.utils.config)
(use 'org.rathore.amit.utils.logger)
(use 'org.rathore.amit.utils.clojure)
(use 'org.rathore.amit.medusa.core)
(def sevaks (ref {}))
(defmacro sevak-runner [sevak-name needs-response sevak-args]
`(fn ~sevak-args
(if (swarmiji-distributed-mode?)
(if ~needs-response
(apply on-swarm (cons ~sevak-name ~sevak-args))
(apply on-swarm-no-response (cons ~sevak-name ~sevak-args)))
(apply on-local (cons (@sevaks ~sevak-name) ~sevak-args)))))
(defmacro defsevak [service-name args & expr]
`(let [sevak-name# (keyword (str '~service-name))]
(dosync (ref-set sevaks (assoc @sevaks sevak-name# {:return Boolean/TRUE :fn (fn ~args (do ~@expr))})))
(def ~service-name (sevak-runner sevak-name# Boolean/TRUE ~args))))
(defmacro defseva [service-name args & expr]
`(let [seva-name# (keyword (str '~service-name))]
(dosync (ref-set sevaks (assoc @sevaks seva-name# {:return Boolean/FALSE :fn (fn ~args (do ~@expr))})))
(def ~service-name (sevak-runner seva-name# Boolean/FALSE ~args))))
(defn handle-sevak-request [service-name service-handler service-args]
(let [response-with-time (run-and-measure-timing
(apply (:fn service-handler) service-args))
value (response-with-time :response)
time-elapsed (response-with-time :time-taken)]
{:response value :status :success :sevak-time time-elapsed})
(catch InterruptedException ie
(throw ie))
(catch Exception e
(log-message "ERROR!" (class e) "detected while running" service-name "with args:" service-args)
(log-exception e)
{:exception (exception-name e) :stacktrace (stacktrace e) :status :error}))))
(defn async-sevak-handler [service-handler sevak-name service-args return-q]
(let [response (merge
{:return-q-name return-q :sevak-name sevak-name :sevak-server-pid (process-pid)}
(handle-sevak-request sevak-name service-handler service-args))]
(if (and return-q (:return service-handler))
(send-message-on-queue return-q response)))))
(defn sevak-request-handling-listener [req-str]
(let [req (read-string req-str)
service-name (req :sevak-service-name) service-args (req :sevak-service-args) return-q (req :return-queue-name)
service-handler (@sevaks (keyword service-name))]
(log-message "[ in-q pool completed" (number-of-queued-tasks) (current-pool-size) (completed-task-count) "]: Received request for" service-name "With args:" service-args)
(if (nil? service-handler)
(throw (Exception. (str "No handler found for: " service-name))))
(let [f (medusa-future-thunk return-q #(async-sevak-handler service-handler service-name service-args return-q))]
(when (> (number-of-queued-tasks) 10)
(.get f))
(catch Exception e
(log-message "Error in sevak-request-handling-listener:" (class e))
(log-exception e)))))
(defn boot-sevak-server []
(log-message "Starting sevaks in" *swarmiji-env* "mode")
(log-message "System config:" (operation-config))
(log-message "MPI transport Q:" (queue-sevak-q-name))
(log-message "MPI diagnostics Q:" (queue-diagnostics-q-name))
(log-message "Sevaks are offering the following" (count @sevaks) "services:" (keys @sevaks))
(init-medusa 300)
;(send-message-on-queue (queue-diagnostics-q-name) {:message_type START-UP-REPORT :sevak_server_pid (process-pid) :sevak_name SEVAK-SERVER})
(log-message "Listening for update broadcasts...")
(start-queue-message-handler (sevak-fanout-exchange-name) FANOUT-EXCHANGE-TYPE (random-queue-name) sevak-request-handling-listener)
(log-message "Done with broadcasts!")
(catch Exception e
(log-message "Error in update broadcasts future!")
(log-exception e)))))
(log-message "Starting to serve sevak requests...")
(start-queue-message-handler (queue-sevak-q-name) (queue-sevak-q-name) sevak-request-handling-listener)
(log-message "Done with sevak requests!")
(catch Exception e
(log-message "Error in sevak-servicing future!")
(log-exception e)))))
(log-message "Sevak Server Started!"))
Jump to Line
Something went wrong with that request. Please try again.