在clojure中实现cron类型调度程序 [英] Implementing a cron type scheduler in clojure
问题描述
我正在寻找任何可以在给定时间触发事件的clojure方法,
例如:我想要一个特定的进程从9开始: 30分钟,然后我可以触发另一个过程,半小时后开始运行等。
提前感谢!
更新2:
感谢@ arthur-ulfeoldt和@ unknown-person,并建议使用 https://github.com/samaaron/at-at ,然后再删除答案。
(使用'overtone.at-at)
(def my-pool(mk-pool))
; =>创建一个线程池
(每1000个(printlnI am super cool! pool:initial-delay 2000)
; =>在延迟2秒后每1秒开始打印一次函数
(停止* 1)
; =>停止
b
因此,为了让它从9开始,间隔半小时,我会这样做:
(require'[clj-time.core:as t])
(require'[clj-time.coerce:as c])
'overtone.at-at)
;使线程池
(def my-pool(mk-pool))
(def current-time(t /现在))
(当前日期(t)/日期时间
(t /年当前时间)
(t /当前时间)
(t / day current-time))
(def next-9-oclock
(if(> 9(t / hour current-time))
(t /加上当前日期(t /小时9))
(t /加当前日期(t /天1)(t /小时9))))
( - (c / to-long next-9-oclock)(c / to-long current-time))
(每1800000
# super cool!)
my-pool
:initial-delay
initial-delay)
$ b b
更新:
@ arthur-ulfeoldt,我不知道如何将一些Java代码翻译成clojure。
http://docs.oracle.com/ javase / 1.5.0 / docs / api / java / util / concurrent / ScheduledExecutorService.html
like:
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper,10,10,SECONDS);
和:
final Runnable beeper = new Runnable(){
public void run(){System.out.println(beep); }
};
整个范例
类BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour(){
final Runnable beeper = new Runnable(){
public void run(){System.out.println(beep); }
};
final ScheduledFuture beeperHandle =
scheduler.scheduleAtFixedRate(beeper,10,10,SECONDS);
scheduler.schedule(new Runnable(){
public void run(){beeperHandle.cancel(true);}
},60 * 60,SECONDS)
}
}
解决方案
- 以整秒间隔启动计划任务,而不是具有高系统时间精度的整分钟间隔。
完成后,可以根据需要生成尽可能多的线程,以便以较早的间隔启动的任务可以存在。对于at-at,Monotony和Quartzite的源代码读取,我觉得他们不符合我的要求(这是真的是一个更裸的骨头,所以我写了我自己的 - cronj
使用示例。
(require'[cronj.core:as cj])
(cj / schedule-task!{:id 0:desc 0
: handler#(printlnjob 0:%)
:tab/ 5 * * * * * *});;每5秒
(cj / schedule-task!{:id 1:desc 1
:handler#(printlnjob 1:%)
:tab/ 3 * * * * *});;每3秒
(cj / start!)
;;等待调度程序做它的事情......
(cj / stop!)
I'm looking for any way for clojure that can trigger an event at a given time,
For example: I want a particular process to start at 9:30am and then I can trigger another process to start running half an hour later etc.
Thanks in advance!
Updated 2:
Thanks @arthur-ulfeoldt and @unknown-person who also suggested using https://github.com/samaaron/at-at before deleting his answer. The documentation is a little out of date but here's how I got going.
(use 'overtone.at-at) (def my-pool (mk-pool)) ;=> make a thread pool (every 1000 #(println "I am super cool!") my-pool :initial-delay 2000) ;=> starts print function every 1 sec after a 2 sec delay (stop *1) ;=> stops it
So to make it start at exactly 9, with an interval of half an hour, I would do:
(require '[clj-time.core :as t]) (require '[clj-time.coerce :as c]) (use 'overtone.at-at) ;Make Thread Pool (def my-pool (mk-pool)) (def current-time (t/now)) (def current-date (t/date-time (t/year current-time) (t/month current-time) (t/day current-time))) (def next-9-oclock (if (> 9 (t/hour current-time)) (t/plus current-date (t/hours 9)) (t/plus current-date (t/days 1) (t/hours 9)))) (def initial-delay (- (c/to-long next-9-oclock) (c/to-long current-time)) (every 1800000 #(println "I am super cool!") my-pool :initial-delay initial-delay)
Updated:
@arthur-ulfeoldt, I am not sure how to translate some of the java code into clojure. http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html
like:
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
and:
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
Entire Example
class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS); } }
I needed something that
- started scheduled tasks at whole-second intervals as opposed to whole minute intervals having high system-time accuracy.
- would spawn as many threads as needed, so that tasks started at earlier intervals could exist along side tasks started at later intervals.
After doing a bit of source code reading for at-at, Monotony and Quartzite, I felt that they did not fit the requirements that I was after (which was really something more bare bone) so I wrote my own - cronj
An example of the usage.
(require '[cronj.core :as cj])
(cj/schedule-task! {:id 0 :desc 0
:handler #(println "job 0:" %)
:tab "/5 * * * * * *"}) ;; every 5 seconds
(cj/schedule-task! {:id 1 :desc 1
:handler #(println "job 1:" %)
:tab "/3 * * * * * *"}) ;; every 3 seconds
(cj/start!)
;; wait for scheduler to do its thing ......
(cj/stop!)
这篇关于在clojure中实现cron类型调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!