grailscronquartz-schedulergrails-plugingrails-config

Quartz job triggering from Config.groovy


I have the following Quartz job running in my application:

class ScraperJob {
    def scraperService

    static triggers = {
        cron name: 'scraperTrigger', cronExpression: "0 0 * * * ?"  // run every minute
    }

    def execute(){
        try {
            scraperService.storing()
            log.info "${new Date()} - Scraping went smoothly."
        }
        catch(IOException) { // Connexion problem
            log.error "${new Date()} - Method: parsing >> Connexion down or interrupted while parsing !"
        }
        catch(SAXException) { // Any SAXParser exception
            log.error "${new Date()} - Method: parsing >> Parser error."
        }
        finally { // if not closed, the application crashes when the connexion fails
            scraperService.slurper.finalize()
            scraperService.parser.finalize()
        }
  }
}

I would like to know if it is possible to set the triggers property from the Config.groovy file. If it is, could you explain how?


Solution

  • I have no idea if this would actually work because i'm not sure when the quartz jobs get configured but in theory it would seem to work. You could probably see how you could also make this much more dynamic if you have more than one job.

    Config.groovy

    quartz.yourCronJobName="0 0 * * * ?"
    

    BootStrap.groovy

    import org.codehaus.groovy.grails.commons.ConfigurationHolder as ConfigHolder
    ...
    def cronExpression = ConfigHolder.config.yourCronJobName
    ScraperJob.triggers.cronExpression = cronExpression 
    

    Good luck. Let me know if it helps.