wrap-reload Not Reloading Files #72

Closed
jeregrine opened this Issue May 28, 2012 · 2 comments

2 participants

@jeregrine

I am wrapping my response handler with wrap-reload but when I change one of my route files it does not reload the code as a result of it. Not sure if this is an issue with ns-tracker or wrap-reload.

@weavejester This one might be an issue with ns-tracker and since you made that and this I figured I'd just tag you, let me know if this is in poor taste. :)

OS: OSX Lion 10.7.4
Command to start process using foreman: lein trampoline run -m gameapi.core
Example Code:

(ns gameapi.core
    (:gen-class)
    (:use [ring.adapter.jetty]
          [ring.middleware.reload]
          [ring.middleware.stacktrace])
    (:require [com.tnrglobal.bishop.core :as bishop]
              [gameapi.routes :as routes]))

(def env 
    "Global Environment: DEV or PROD"
    (or (System/getenv "ENV") "DEV"))

(defn main
    "Exposes the main function for bootstrapping the application."
    [& args]
    (let [port (Integer/parseInt (or (System/getenv "PORT") "3000"))
          base-handler (bishop/handler routes/routes)
          handler (if (= env "DEV") (wrap-reload base-handler)
                      base-handler)]
        (println handler)
        (println (str "Webserver starting on port " port " with env " env))
        (run-jetty handler {:port port})))

(defn -main
    [& args]
    (main args))
@weavejester
Collaborator

The wrap-reload middleware will reload your Clojure source files, but you still need to reference your handler as a var. In your example, route/routes is resolved once when the main function is evaluated. You need to ensure that you're referencing the var so that the reference is resolved each time the handler is called. In other words:

(bishop/handler #'routes/routes)

That should fix your problem.

@jeregrine

Thanks for the tip! Unfortunately the var reader syntax would not work because routes/routes is something unique bishop/handler takes, but bishop/handler returns a ring function. So I attempted to do

(defn main
    "Exposes the main function for bootstrapping the application."
    [& args]
    (let [port (Integer/parseInt (get (System/getenv) "PORT" "3000"))
                  base-handler #'(bishop/handler routes/routes)
          handler (if (= env "DEV") (wrap-stacktrace (wrap-reload base-handler))
                      base-handler)]
        (println handler)
        (println (str "Webserver starting on port " port " with env " env))
        (run-jetty handler {:port port})))

Which gave me an error, which is obvious now :)

So I ended up with this solution.

(def base-handler 
    (bishop/handler routes/routes))

(defn main
    "Exposes the main function for bootstrapping the application."
    [& args]
    (let [port (Integer/parseInt (get (System/getenv) "PORT" "3000"))
          handler (if (= env "DEV") (wrap-stacktrace (wrap-reload base-handler))
                      base-handler)]
        (println handler)
        (println (str "Webserver starting on port " port " with env " env))
        (run-jetty handler {:port port})))

Which is not ideal but its what I've got. Thanks for your help! Really appreciate it!

Maybe I can patch bishop to return a var for handlers so it plays nicer with ring middleware.

@jeregrine jeregrine closed this May 28, 2012
@trevor trevor referenced this issue in ring-clojure/ring Nov 18, 2013
Open

documentation for using wrap-reload #104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment