You don't need a global reference that lives in your code to be able to
interact with the scheduler at the REPL. I'd do it by breaking stuff into
more bits - it looks convoluted for this simple example, but hopefully the
idea makes sense:
(defn make-scheduler [] (qs/initialize))
(defn start-scheduler [scheduler] (qs/start scheduler))
(defn start-scheduler-system
[]
(-> (make-scheduler)
(start-scheduler)))
(defn -main [& args]
(start-scheduler-system))
The differences are: having a function to make a scheduler that returns the
scheduler; combining all scheduler related startup into one function; not
doing much in -main. In general I try to never call -main from the REPL, so
having as much of the functionality outside it helps.
>From the REPL, you can evaluate something like
(def my-scheduler (make-scheduler))
(start-scheduler my-scheduler)
if you want to be able to fiddle with the scheduler, or just
`(start-scheduler-system)` if you don't need access to it (say you were
interacting with something else).
Of course, you may find in doing this that you're on your way to inventing
a less good version of component ... but that's probably not such a bad way
to learn :-)
Jony
On Wednesday, 28 January 2015 03:43:39 UTC, Huey Petersen wrote:
>
> Hello,
>
> I just started writing my first non-toy clojure program and I'm curious
> how people handle global resources without component (or component like)
> systems. I plan to make use of component but I'm trying to add new
> concepts one by one.
>
> As an example I have a scheduler (quartzile):
>
> (def scheduler (qs/intialize))
>
> (defn -main [& args]
> (qs/start scheduler))
>
> So this works and I can access the scheduler instance in my REPL to poke
> at it while I'm developing.
>
> I then do lein uberjar and it won't compile :). The compiler is executing
> the qs/initialize function which is no good.
>
> So I could move that initialize inside of main:
>
> (def -main [& args]
> (-> (qs/intialize) (qs/start)))
>
> But I've now lost a global reference to poke at the scheduler.
>
> Instead I used alter-var-root, which I've seen before, but until now
> actually had no idea what it really did...
>
> (def scheduler)
>
> (def -main [& args]
> (alter-var-root #'scheduler (fn [old] (qs/initialize)))
> (qs/start scheduler))
>
> So this seems nasty, but lein uberjar is happy.
>
> Anyways, I'm just curious of what the alternatives are to manage this
> outside of a component type system (which I plan to use!).
>
> Thanks!
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.