luminus-template icon indicating copy to clipboard operation
luminus-template copied to clipboard

Code reloading--everything except DB?

Open paultopia opened this issue 9 years ago • 7 comments

Here's something odd I noticed. I've been using the template with mongodb hook in, and it's all running fine, I've verified that I can add to and fetch from the database, etc. But while all the other namespaces happily hot-reload when I have everything running and edit the file, the db.core namespace does not: if I add a function to it, I have to restart in order to make it accessible from routes and such. Without restart I get compilation errors of the "symbol not found" variety whenever trying to call anything from db.core

Is this expected behavior?

paultopia avatar Jul 04 '16 16:07 paultopia

Hmm seems like the db.core isn't being reloaded, although not sure why that should be special. As a workaround in the meantime, you could connect the editor to the nREPL started by the app and reload the namespace from the REPL when you make changes. The app should run a REPL on localhost:7000 by default when you do lein run.

yogthos avatar Jul 04 '16 18:07 yogthos

Thanks! And yeah, I've been poking around the code and can't figure out why this is happening either. Maybe something about having an open database connection is keeping mount from reloading the code (??)

paultopia avatar Jul 04 '16 18:07 paultopia

Mount should be trying to reload the connection when a namespace reloads. When reload happens the :stop and :start hooks should fire.

yogthos avatar Jul 05 '16 03:07 yogthos

Ah I think I know what the problem there might be. The reloading is accomplished using the wrap-reload middleware in the handler. This middleware is only triggered when a request is submitted to the server. So, you have to make a change in the namespace and then call one of the routes in the handler for the reload to trigger.

yogthos avatar Jul 05 '16 03:07 yogthos

Hmm. I seem to be having a different experience. (Maybe I've broken something...)

It doesn't look like calling the routes makes a difference. t've been poking around a bit--- in each of the mywebsite.db.core and mywebsite.ws namespaces I inserted a dummy function that just returns a string. And then in the defrosts in my routes.home namespace I have the following two routes:

 (GET "/dummy2" request (db/dummy-function))
  (GET "/wsdummy" request (ws/ws-dummy))

When I navigate to either route in the browser without manually reloading, I get a compilation error No such var: db/dummy-function.

But commenting out the first line and then going to the second route works as expected. And then when I manually reload the db.core namespace from the repl with `(require ... reload) then both work fine.

Really odd!

paultopia avatar Jul 06 '16 04:07 paultopia

Yeah, that is kinda strange.

yogthos avatar Jul 06 '16 12:07 yogthos

I think we've got a workaround by adding this to the restart function in user.clj:

(require '[project-name.db.core] :reload)

Though, this might be even better: https://github.com/clojure/tools.namespace#reloading-code-motivation

dijonkitchen avatar Sep 14 '19 15:09 dijonkitchen