Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crontab style scheduling in Play 2.4.x?

Technically I can install cron on the machine and curl the url, but I'm trying to avoid that. Any way to accomplish this?

Reason I want to avoid cron is so I can easily change the schedule or stop it completely without also ssh'ing into the machine to do so.

like image 476
iCodeLikeImDrunk Avatar asked Sep 14 '15 20:09

iCodeLikeImDrunk


2 Answers

Take a look at: https://github.com/enragedginger/akka-quartz-scheduler. Refer to http://quartz-scheduler.org/api/2.1.7/org/quartz/CronExpression.html for valid CronExpressions and examples.

An example taken from the docs:

An example schedule called Every-30-Seconds which, aptly, fires-off every 30 seconds:

akka {
  quartz {
    schedules {
      Every30Seconds {
        description = "A cron job that fires off every 30 seconds"
        expression = "*/30 * * ? * *"
        calendar = "OnlyBusinessHours"
      }
    }
  }
}

You can integrate this into your Play! application (probably in your Global application)

like image 69
Sudheer Aedama Avatar answered Sep 21 '22 16:09

Sudheer Aedama


You can use the Akka scheduler.

val scheduler = Akka.system(app).scheduler
scheduler.schedule(0 seconds, 1 hour) {
  // run this block every hour
}

The first parameter is a delay, so if you wanted to delay to a specific time you could easily calculate the target time with some simple date arithmetic.

like image 34
andyczerwonka Avatar answered Sep 20 '22 16:09

andyczerwonka