PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/clj_drmaa/core.clj

http://github.com/leadtune/clj-drmaa
Clojure | 54 lines | 44 code | 10 blank | 0 comment | 2 complexity | 98a0156ecbc96f6710906bdfd15e6a5a MD5 | raw file
  1. (ns clj-drmaa.core
  2. (use [clojure.contrib
  3. [def :only [defn-memo defnk]]])
  4. (import [org.ggf.drmaa DrmaaException Session SessionFactory JobTemplate]))
  5. (defn at-exit
  6. "Similar to Ruby's at_exit hook. The given f will be ran will the process closes."
  7. [f]
  8. (.addShutdownHook (Runtime/getRuntime) (Thread. f)))
  9. (defn-memo get-session
  10. "Returns an initialized session for DRMAA.
  11. From what I can tell you are only allowed one session per process (singleton).
  12. Additionaly, you need to ensure that the session resources are properly
  13. cleaned up. This function handles all of this for you."
  14. []
  15. (let [session (doto (.getSession (SessionFactory/getFactory)) (.init ""))]
  16. (at-exit #(.exit session))
  17. session))
  18. (defnk block-on-jobs
  19. "Blocks until the given job ids complete or fail.
  20. Options and defaults:
  21. - :timeout, like it sounds, but it is unclear from the DRMAA docs if this is really supported.
  22. default: Will run until completion.
  23. - :dispose, removes the jobs from the system once it is complete, meaning you can not query about them.
  24. default: true"
  25. [job-ids :timeout (Session/TIMEOUT_WAIT_FOREVER) :dispose true]
  26. (.synchronize (get-session) job-ids timeout dispose))
  27. (defnk run-jobs
  28. "Starts multiple jobs with cmd and for each args vector found in job-args.
  29. Options and defaults:
  30. - :block?, boolean indicating if this fn should block until jobs are complete
  31. default: false
  32. - :block-options, options passed to block-on-jobs if block? is true
  33. default: {}
  34. Example:
  35. (run-jobs \"sleep\" [[\"5\"] [\"10\"]] :block? true)"
  36. [cmd job-args :block? false :block-options {}]
  37. (let [session (get-session)
  38. jt (doto (.createJobTemplate session) (.setRemoteCommand cmd))
  39. job-ids (doall (for [args job-args]
  40. (do (.setArgs jt args)
  41. (.runJob session jt))))]
  42. (.deleteJobTemplate session jt)
  43. (when block? (apply block-on-jobs job-ids block-options))
  44. job-ids))