开发者

How should I schedule some simple delayed tasks in Scala?

开发者 https://www.devze.com 2023-03-01 02:20 出处:网络
I\'m making a Chaos Monkey program and I want it to clean up after itself after a certain period of time. I\'d like a simple way to queue up cleanup tasks to be called a set amoun开发者_JS百科t of tim

I'm making a Chaos Monkey program and I want it to clean up after itself after a certain period of time. I'd like a simple way to queue up cleanup tasks to be called a set amoun开发者_JS百科t of time in the future. I think I could do something with actors and a lot of hand-waving but that seems like the wrong approach. Is there a better tool for this task in the Scala standard library?


I have written a scheduling DSL in Scala called foil, which is freely available on Github. It will work with either of Java Calendar/Date, or the Joda library. The syntax looks like this:

schedule(f) now
schedule(f) onceAfter 5.minutes
schedule(f) onceAt inst
schedule(f) onceAtNext time
schedule(f) todayNoEarlierThan time

Where f is a closure (i.e. () => Unit). There's many more examples on the Wiki and an example REPL session with foil, showing how to use it (with both Java Date/Calendar and JODA).


There's not so much hand waving involved, reactWithin combined with the TIMEOUT message will let you do this.

You can also use Futures.alarm to create a waitable Future that will resolve after the specified time limit.

0

精彩评论

暂无评论...
验证码 换一张
取 消