How to prevent task from exiting
I have a long term task like watching file by myself, I just don't like it exits after my task is running. I tried wait but blocks the thread. Now I using repl to force the task running.
However repl turned out to cost me some resource. Is there a better solution?
To be more specific, I'm firing a ring server in my task, and it exits as the task finishes. Normally a ring server will make the process running. So it's a bit strange in Boot here.
wait task blocks a thread. But why is that a problem?
Did not read the code before, turned out there are two different behaviors.
(core/deftask wait
"Wait before calling the next handler.
Waits forever if the --time option is not specified."
[t time MSEC int "The interval in milliseconds."]
(if (zero? (or time 0))
(core/with-post-wrap [_] @(promise))
(core/with-pass-thru [fs] (Thread/sleep time))))
I'm writing ClojureScript all the time, not familiar with JVM... As I tried, 2 is never printed in my case:
(println 1)
(println @(promise))
(println 2) ; never printed
So, here's my problem, in my task, I don't save code in .cljs files. It's in the internal state of (start-stack-editor!), this task will call the task behind it for many times as I save a file:
(deftask dev! []
(set-env!
:asset-paths #{"assets/"})
(comp
(repl) ; <-------- place-1, replace with (wait), works fine
(start-stack-editor!)
(target :dir #{"src/"})
(html-file :data {:build? false})
(reload :on-jsload 'stack-workflow.main/on-jsload!
:cljs-asset-path ".")
(cljs :compiler-options {:language-in :ecmascript5})
(target :no-clean true)
; <------- place-2, add (wait), blocks my response from task boot-stack-editor!
))
Given this situation, I can explain some behaviors. Like when I put (wait) in place-1, it's fine. But when I put it in place-2, some of my code is blocked. I'm not sure why. Anyway it should be ok when put (wait) in the first of my tasks.
Also some difference from JavaScript, in Node.js , I can just make it running without block anything. It appears that I can never do that in Java. It always blocks something behind it. And I have to make sure the blocked part is not before my code.
I presume start-stack-editor works like watch and runs the rest of task pipeline after file edits.
If the wait task is first, wait task will call the following tasks first and then create promise and wait indefinitely for it to finish. It will first run the next tasks because it uses post-wrap.
start-stack-editor probably uses several threads so it will work even if there is wait task first.
If wait is last, is is obviously run after file changes and will block a thread created by start-stack-editor!.
I guess so. Threads are a bit unfamiliar since JavaScript runs thread in a different way, at least less powerful than Java or Clojure. Strange to me that (repl) just works fine even I called it so many times.
And does Boot designed (wait) to behave like this? Then it's only a misuse in my case.
@jiyinyiyong the wait task is designed to prevent the pipeline from exiting. This is handy in cases like yours where you want things to continue running even after you've reached the end of the pipeline.
@martinklepsch ...and need to be the first task in case of blocking something?
Sorry I oversaw earlier messages, probably my comment wasn't very helpful, sorry!