Scheduler.scala (1474B)
1 package generator 2 3 import com.typesafe.config.{Config, ConfigFactory} 4 import org.quartz.CronScheduleBuilder.cronSchedule 5 import org.quartz.{CronTrigger, SchedulerException} 6 import org.quartz.JobBuilder.newJob 7 import org.quartz.TriggerBuilder.newTrigger 8 import org.quartz.impl.StdSchedulerFactory 9 10 import scala.concurrent.{Await, Future} 11 import scala.concurrent.ExecutionContext.Implicits.global 12 13 14 object Scheduler { 15 val conf: Config = ConfigFactory.load() 16 17 def main(): Unit = { 18 val cronString = conf.getString("generator.cronString") 19 try { // Grab the Scheduler instance from the Factory 20 val sf = new StdSchedulerFactory 21 val scheduler = sf.getScheduler 22 // and start it off 23 scheduler.start() 24 25 val job = newJob(classOf[GenerateNewsPaper]).withIdentity("generateNewsPaper", "generator").build 26 27 // Trigger the job to run now, and then repeat every 40 seconds 28 val trigger: CronTrigger = newTrigger.withIdentity("daily", "generator").withSchedule(cronSchedule(cronString)).build 29 30 // Tell quartz to schedule the job using our trigger 31 scheduler.scheduleJob(job, trigger) 32 33 /** 34 * Keep alive workaround 35 */ 36 val waitFunc = Future { 37 while (true) { 38 Thread.sleep(1000) 39 } 40 } 41 42 Await.result(waitFunc, scala.concurrent.duration.Duration.Inf) 43 44 scheduler.shutdown() 45 } catch { 46 case se: SchedulerException => 47 se.printStackTrace() 48 } 49 } 50 51 }