Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A jobTimer in the Clowder calls JobsScheduler.runScheduledJobs() every minute. This is done from Akka.system().scheduler.schedule in app/Global.
This time is then split into minute, hour, day_of_week, day_of_month variables in the JobsScheduler (app/models/JobsScheduler.scala) for further use in the code and for jobs’ execution and maintenance.
The variables are stored as integers The time variables are compared with a set of integers stored in every job object in the MongoDB Collection named jobs and any new time is compared against these values as a set of new integer values(jobs). In other words the time comparison is done by integer equality. A job gets fired hourly when minute1minute=minute2minutejob if no other values of hour, day_of_week, day_of_month are defined. Similarly, the job gets executed with minutes and hours set (daily) when  minute1minute=minute2minutejob and hour1hour=hour2hourjob (no other values of day_of_week, day_of_month are defined) etc. Note that there is no verification of time inserted in the system, nor there is time/date object comparison.

A job model in Clowder is called TimerJob (app/models/TimerJob.scala). A programmer can create different job schema but the TimerJob is sufficient for the most repetitive tasks.
A full time, or only subset of it is set in the TimerJob with minute (0-59), hour (0-23), day_of_week (1-7 for Monday-Sunday) and day_of_month (1-31) precision. An option frequency is meant to be ‘hourly’, ‘daily’, ‘weekly’, ‘monthly’ but can be any descriptive string. The lastJobTime field is useful for  getting the time interval since the last job call (set by scheduler.updateLastRun(‘jobName’)). Additionally, parameters can be used for any object id, function is a string describing action (e.g. EmailDigest).

...

  1. A new TimerJob job is created and set in the MongoDB Collection by newly coded functions in app/services/ScheduleService and app/services/mongoldb/MongoDBSchedulerService.scala called for example  updateMyJob():

    Code Block
    languagescala
    def updateMyJob(id: UUID, name: String, setting: String)

    and

    Code Block
    languagescala
    def updateMyJob(id: UUID, name: String, setting: String) = {
      if (jobExists(name) == false) {
          Jobs.insert(new TimerJob(name, None, None, None, None, Option(‘function’), Option(id), None, Option(new Date())))
      }
      if (setting == "hourly"){
        updateJobTime(name, Option(0), None, None, Option(setting))
      }
      else if (setting == "daily"){
        updateJobTime(name, Option(0), Option(7), None, Option(setting))
      }
      else if (setting == "weekly"){
        updateJobTime(name, Option(0), Option(7), Option(1), Option(setting))
      }
      else {
        deleteJob(name)
      }
    }

    This is similar to a function updateEmailJob() already implemented in the Clowder where  updateJobTime(name, Option(0), Option(7), Option(1), Option(setting)) refers tot the time set to minute=0, hour=7 and day_of_week=1 (Monday) in the database as mentioned above. When Clowder time matches the values the job is returned from the database and Action is fired.
    You can either change time directly in the code or pass the time values from a Play template as additional parameters such as:

    Code Block
    languagescala
    def updateMyJob(id: UUID, name: String, setting: String, minute: Integer)= {
        updateJobTime(name, Option(minute), None, None, Option(setting))
    }
  2. from the Play request or directly in Scala on the server side call your job update:
    scheduler.updateMyJob(id, name, setting)
    here the id is a parameters id (parameters: Option[UUID], see TimerJob model), name is the job's name and settings are used to  distinguish time frequency (from options of pull down menus for example - hourly, weekly etc.)

Call and get job

...

  1. Create function getMyJobs() in app/models/JobsScheduler.scala to get your job (TimerJob) at a certain time

...

  1. Code Block
    languagescala
    def getMyJobs (minute: Integer, hour: Integer, day_of_week: Integer) = {
       var myJobs = scheduler.getJobByTime(minute, hour, day_of_week)
       myJobs
    }

    and register it with runScheduledJobs()

    Code Block
    languagescala
    var myJobs = getMyJobs(minute.toInt, hour.toInt, day_of_week.toInt)

...

  1. 
    myAction.myActionJob(myJobs)

...

  1. Create a new class myAction

...

  1. () for example

...

  1. in a package models

    Code Block
    languagescala
    collapsetrue
    package models
    
    import java.util.Date
    import services.SchedulerService
    import services.DI
    
    object myAction {
    val scheduler: SchedulerService =  DI.injector.getInstance(classOf[SchedulerService])
    val objects: ObjectService =  DI.injector.getInstance(classOf[ObjectService])
     /**
      * ‘Do something’ for each job returned by getMyJobs
      */
     def myActionJob(listJob: List[TimerJob]) = {
       for (job <- listJob){
         job.parameters match {
           case Some(id) => {
             objects.findById(id) match {
               case Some(object) => {
                 job.lastJobTime match {
                   case Some(date) => {
                     ‘Do something’
                   }
                   case None => Logger.debug("LastJobTime not found")
                 }
               }
               case None => Logger.debug(“Object not found")
             }
             scheduler.updateLastRun(‘jobName’) //sets job’s name for example ”myJob[“ + id + "]")
           }
           case None => Logger.debug("Parameters not found")
         }
       }
     }

...

  1. The ObjectService  above, for example called UserService in the case of sending email digest and the user id was used as a parameter in the TimerJob and MongoDB job objects.

  2. implement Do something in myAction(

...

  1. ) (smile)

Note

The day that ‘day_of_month’ month variable is part of the TimerJob model but it is not implemented used in the scheduler.getJobByTime()
Adding it is straightforward, check:1) in

  • app

...

  • /services

...

  • /SchedulerService.scala

...

...

  • app

...

  • /services

...

  • /mongoldb

...

  • /MongoDBSchedulerService.scala


2a) update

Code Block
languagescala
def getJobByTime(minute: Integer, hour: Integer, day_of_week: Integer, day: Integer): List[TimerJob] ={
    val jobs = Jobs.find(
      $and(
        // either day exists AND the value is 'day' OR day does not exist
        $or($and("day" $exists true, MongoDBObject("day" -> day)),
        // either day_of_week exists AND the value is 'day' OR day_of_week does not exist
        $or($and("day_of_week" $exists true, MongoDBObject("day_of_week" -> day_of_week)), "day_of_week" $exists false),
        // either hour exists AND the value is 'hour' OR hour does not exist
        $or($and("hour" $exists true, MongoDBObject("hour" -> hour)), "hour" $exists false),
        // either minute exists AND the value is 'minute' OR minute does not exist
        $or($and("minute" $exists true, MongoDBObject("minute" -> minute)), "minute" $exists false)
      )
    )

...