Re: Compiling starts jetty server, so I can't complete Uberjar

155 views
Skip to first unread message

James Reeves

unread,
Aug 20, 2012, 4:20:15 AM8/20/12
to ring-c...@googlegroups.com
The run-jetty function does what the name suggests: it runs Jetty.

You're defining "server" with defonce, but then trying to use it as a
function. I'm also not sure why you have {:mode mode} in your code
snippet. What's that meant to do?

You want something more like:

(defn server [port]
(run-jetty #'app {:port port})

But you might want to look into Lein-Ring instead of starting the
server yourself via -main.

- James

On 20 August 2012 07:07, JR <james.r...@gmail.com> wrote:
> Hi:
>
> I'm a newbie to ring development, and I'm trying to uberjar up what little I've got developed so far so I can put it out on a demo server. But when I run lein1 uberjar, the process involves compiling the code, and when it compiles, for some reason it seems to automatically invoke the server, so the uberjar never completes.... everything just locks up while my server sits there listening on port 8080, from which I have to Ctrl-C to kill.
>
> My code looks like this: (snippet)
>
> (defonce server (ring.adapter.jetty/run-jetty
> #'app {:host "127.0.0.1" :port 8080 :join? false}))
>
> (defn -main
> [& m]
> (let [mode (keyword (or (first m) :dev))
> port (Integer. (get (System/getenv) "PORT" "8080"))]
> (server port {:mode mode
> })))
>
> And the message I get on compilation is:
> Jamess-MacBook-Pro-Retina:choozy.git jr$ lein1 uberjar
> Copying 40 files to /Users/jr/opt/choozy.git/lib
> Compiling me.choozy.www.web.server
> 2012-08-19 23:06:42.543:INFO:oejs.Server:jetty-7.6.1.v20120215
> 2012-08-19 23:06:42.571:INFO:oejs.AbstractConnector:Started SelectChann...@127.0.0.1:8080
>
> Any idea how to avoid this starting?
>
> Thanks in Advance!!

Shen, Feng

unread,
Aug 20, 2012, 9:37:25 PM8/20/12
to ring-c...@googlegroups.com
I also use lein uberjar to pack all deps in one file.

when file is loaded(when lein uberjar, all clj file should be load):
this form get executed:

(defonce server (ring.adapter.jetty/run-jetty
             #'app {:host "127.0.0.1" :port 8080 :join? false}))

which effectively start jetty server.

Here is a way to fix it:
(defonce server (atom nil))

in -main

(reset! server (ring.adapter.jetty/run-jetty #'app {:host "127.0.0.1" :port 8080 :join? false})

James Rothering

unread,
Aug 21, 2012, 5:54:08 PM8/21/12
to ring-c...@googlegroups.com
I guess I was surprised that "lein compile" was actually invoking the
top-level functions. When I finally wrapped my head around this, I
moved the run-jetty down below a top-level function in main -- if main
got called with no parms, then it did nothing; if it got called with a
parm, then it started jetty using that port. So now the uberjar works,
because it never invokes the main with a parm. Then everything gets
packaged up nicely. When I invoke from the command line, I pass in my
port and everything is kosher.

With your method, won't you run into the same problem? So if your main
method does a reset! on the atom, won't this get invoked on
compilation?

You see, the only problem is that I'm compiling & jar-ing, but
invoking the server interrupts the compilation/jar-ing, and starts
listening!!! Then I have to Ctrl-C to kill the listening Jetty so I
get my command line back. But then, I never got to the actual
uberjar-ing I needed.

Regards,

James
--
Regards,

James Rothering
------------------------
(Msg) 206-888-1776

Shen, Feng

unread,
Aug 21, 2012, 8:22:02 PM8/21/12
to ring-c...@googlegroups.com
--I guess I was surprised that "lein compile" was actually invoking the
top-level functions

Actually, `lein compile` will 'evaluate' all forms.  
(defonce server (ring.adapter.jetty/run-jetty
             #'app {:host "127.0.0.1" :port 8080 :join? false}))

when this get 'evaluated', function start. This is something like `compile time` evaluate found in other language, like c++

Functions are evaluate to functions, not call them, so main evaluate to main, it will not get called.

Clojure never interpret, all are compiled to JVM byte code,   `evaluate` is quoted.

hoping it helps.



沈锋
美味书签 http://mei.fm 

James Reeves

unread,
Aug 22, 2012, 4:17:36 AM8/22/12
to ring-c...@googlegroups.com
The -main function is not invoked during compilation. The problem with
your code is that you started the server *outside* the -main function.

- James
Reply all
Reply to author
Forward
0 new messages