https://github.com/clj-sys/work.git
cant say i have used it, but i noticed it in there recently whilst
looking for other things.
here is the function:
(defn schedule-work
"schedules work. cron for clojure fns. Schedule a single fn with a
pool to run every n seconds,
where n is specified by the rate arg, or supply a vector of fn-rate
tuples to schedule a bunch of fns at once."
([f rate]
(let [pool (Executors/newSingleThreadScheduledExecutor)]
(.scheduleAtFixedRate
pool (with-log f) (long 0) (long rate) TimeUnit/SECONDS)
pool))
([jobs]
(let [pool (Executors/newSingleThreadScheduledExecutor)]
(doall (for [[f rate] jobs]
(schedule-work pool f rate)))
pool)))
hope that is useful,
cheers
gaz
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> 3. I could set a job-schedule using the OS to run a clojure script.
> I'd rather not, I would like to do things like send emails / check
> status via web app (making option 1 more appealing).
Could you elaborate on why a scheduled job to run a Clojure script would be inappropriate for those kinds of tasks? Anything you can do from a daemon, you should be able to do from a cron job.
Let's see. On the one hand, running a job scheduler from inside
Clojure results in cranking up a big, slow to start up, expensive JVM
process once a session, on startup, which then runs some tasks
periodically, and the scheduling itself can be done in a nice language
like, say, Clojure, with nice error reporting.
On the other hand, running a job scheduler from outside Clojure
results in cranking up a big, slow to start up, expensive JVM process
every single time a task needs to run, each of which runs one task
once, and the scheduling itself must be done in an icky language like
shell or cron's idiosyncratic "crontab" files with icky error
reporting (e.g., need to run a local mail *server* to receive error
notifications).
I think I can see why someone might prefer to have the scheduling done
inside the JVM. :)
> On the other hand, running a job scheduler from outside Clojure
> results in cranking up a big, slow to start up, expensive JVM process
> every single time a task needs to run, each of which runs one task
> once, and the scheduling itself must be done in an icky language like
> shell or cron's idiosyncratic "crontab" files with icky error
> reporting (e.g., need to run a local mail *server* to receive error
> notifications).
If you care about startup times, you can use nailgun. But that shouldn't matter unless you're running the job every minute or something.
As for scheduling, crontabs are really not hard to figure out. If you need more complex scheduling, you can do that from your Clojure script (essentially using cron to set the polling interval).
And what kinds of error reporting could you do from a persistent daemon that you couldn't also do from a cron job? Besides, most systems that have cron also come with postfix (though it's disabled by default on Mac OS X), so all you have to do is add your email address to /etc/aliases. Email-based error reporting for background tasks is really nice because you don't have to remember to check some log file or other task-specific status indicator periodically (which has burned me in the past).
But this is all somewhat beside the point. What Trevor said sounded as though the specific types of tasks he mentioned (sending emails and checking some kind of status via web app) were particularly unsuited to scheduled jobs; I was asking what it was about those tasks in particular that made him lean towards a daemon instead.
Obviously, that requires knowing about, and learning how to use,
nailgun. Solutions with a higher cost in
novel-tools-you-have-to-figure-out-how-to-use are not, all other
things being equal, superior ones.
> As for scheduling, crontabs are really not hard to figure out. If you need more complex scheduling, you can do that from your Clojure script (essentially using cron to set the polling interval).
If you're going to do that anyway, you might as well do the whole
thing from inside Clojure.
> And what kinds of error reporting could you do from a persistent daemon that you couldn't also do from a cron job? Besides, most
> systems that have cron also come with postfix (though it's disabled by default on Mac OS X), so all you have to do is add your email
> address to /etc/aliases. Email-based error reporting for background tasks is really nice because you don't have to remember to check
> some log file or other task-specific status indicator periodically (which has burned me in the past).
Well, both Windows and MacOS have variations on the nifty concept of
"tray notification".
> But this is all somewhat beside the point. What Trevor said sounded as though the specific types of tasks he mentioned (sending
> emails and checking some kind of status via web app) were particularly unsuited to scheduled jobs; I was asking what it was about
> those tasks in particular that made him lean towards a daemon instead.
Maybe he needs timely responses to something, so something more akin
to a web server than a periodically-run job?