MichaelDrogalis / dire

Erlang-style supervisor error handling for Clojure
482 stars 20 forks source link

Can not get eager-pre-hook to run? #27

Closed lkrubner closed 10 years ago

lkrubner commented 10 years ago

I have an odd problem. I have hooked to vars in other namespaces before, but now I seem to be not matching, perhaps because of my use of Slingshot.

I have namespace which starts as:

(ns loupi.persistence-queue
(:require
 [loupi.persistence :as persistence]
 [slingshot.slingshot :as slingshot]
 [clojure.pprint :as pp]
 [lamina.core :as lamina]))

 (defn persist-this-item [context-wrapper-for-database-call]
   {:pre [(map? context-wrapper-for-database-call)]}
   (lamina/enqueue persistence-channel
              (fn []
                (pp/pprint context-wrapper-for-database-call)
                (persistence/make-consistent context-wrapper-for-database-call)
                )))

And then I have another namespace where I want to add an eager pre-hook. I tried linking to the other var in these ways:

 (dire/with-eager-pre-hook! #'loupi.persistence-queue/persist-this-item
   "An optional docstring."
   (fn [a b] (println "Logging something before preconditions are evaluated.")))

and I also tried:

 (:require
      [loupi.persistence-queue :as pq]

 (dire/with-eager-pre-hook! #'pq/persist-this-item
   "An optional docstring."
   (fn [a b] (println "Logging something before preconditions are evaluated.")))

or just:

 (:use 
 [loupi.persistence-queue]) 

 (dire/with-eager-pre-hook! #'persist-this-item
   "An optional docstring."
   (fn [a b] (println "Logging something before preconditions are evaluated.")))

None of these seem to work.

This line runs and prints to stdout so I can see it in the terminal:

                (pp/pprint context-wrapper-for-database-call)

but I can not get the pre-hook to work. Any suggestions?

If I look at RobertHooke:

https://github.com/technomancy/robert-hooke/

They offer this example:

(add-hook #'some.ns/target-var #'hook-function)

And I feel like what I'm doing is similar, so I am unsure what I am doing wrong.

lkrubner commented 10 years ago

I guess this would happen if there had already been an exception on that thread, which had gone uncaught?

MichaelDrogalis commented 10 years ago

That's definitely plausible. I don't see anything wrong with your usage of the library. Does it work when used within the same namespace?

lkrubner commented 10 years ago

Strangely, if I do this:

   (ns loupi.persistence-queue
   (:require
    [loupi.persistence :as persistence]
    [slingshot.slingshot :as slingshot]
    [lamina.core :as lamina]
    [dire.core :as dr]))

 (def ^:private persistence-channel (lamina/channel))

 (defn persist-this-item [context-wrapper-for-database-call]
   (lamina/enqueue persistence-channel
                   (fn []
                     (persistence/make-consistent context-wrapper-for-database-call))))

 (dr/with-eager-pre-hook! #'persist-this-item
   "An optional docstring."
   (fn [a] (println "Logging something before preconditions are evaluated." )
     (println (str a))))

Then the "Logging something before preconditions are evaluated." shows up in my terminal. But if move the pre-hook to its own namespace:

(ns loupi.supervisor
  (:require
   [loupi.perpetual :as perpetual]
   [loupi.startup :as startup]
   [loupi.server :as server]
   [loupi.dates :as dt]
   [clojure.string :as st]
   [clj-stacktrace.core :as stack]
   [dire.core :as dire]
   [taoensso.timbre :as timbre]))

(dire/with-eager-pre-hook! #'loupi.persistence-queue/persist-this-item
  "An optional docstring."
  (fn [a b] (println "Logging something before preconditions are evaluated.")))

Then this does not appear in my terminal output: "Logging something before preconditions are evaluated.")

And it makes no difference if I use :use to include the namespace:

(ns loupi.supervisor
  (:require
   [loupi.perpetual :as perpetual]
   [loupi.startup :as startup]
   [loupi.server :as server]
   [loupi.dates :as dt]
   [clojure.string :as st]
   [clj-stacktrace.core :as stack]
   [dire.core :as dire]
   [taoensso.timbre :as timbre]
   (:use
    [loupi.persistence-queue])))

(dire/with-eager-pre-hook! #'loupi.persistence-queue/persist-this-item
  "An optional docstring."
  (fn [a b] (println "Logging something before preconditions are evaluated.")))
MichaelDrogalis commented 10 years ago

That's really weird. Are you sure the namespace containing the hooks is getting required before the function call?

lkrubner commented 10 years ago

Damn, that was obvious. I'd changed the order of things being required, and I'd left out a crucial one.

MichaelDrogalis commented 10 years ago

No problem, happens all the time! Closing unless anything else arises.