pr1001
pr1001

Reputation: 21962

How should I schedule some simple delayed tasks in Scala?

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 amount 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?

Upvotes: 3

Views: 3170

Answers (2)

oxbow_lakes
oxbow_lakes

Reputation: 134270

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).

Upvotes: 9

Kevin Wright
Kevin Wright

Reputation: 49705

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.

Upvotes: 4

Related Questions