Tuesday, July 10, 2012

Kickstart TimeMachine Scheduler with a quick Groovy script

If you have Groovy installed, you can kick start a TimeMachine Scheduler job without any setup! Try this out:


// Create a repeating schedule job that runs every 3 seconds.
@Grab('org.slf4j:slf4j-simple:1.6.6')
@Grab('org.bitbucket.timemachine:timemachine-scheduler:1.2.2')
import timemachine.scheduler.*
scheduler = new SchedulerFactory().createScheduler()
scheduler.init()
jobDef = JobDefs.groovyJobDef('''
    println("Hello World.")
''')
jobDef.addSchedule(Schedules.secondly(3))
scheduler.schedule(jobDef)
scheduler.start()
addShutdownHook{ scheduler.destroy() }


4 comments:

  1. You may also grab the latest snapshot by adding this line:

    @GrabResolver(name='snapshot', root='http://oss.sonatype.org/content/groups/public')

    ReplyDelete
  2. Doesnt work at all..

    88 [main] INFO timemachine.scheduler.service.SchedulerEngine - TimeScheduler system services initialized: [
    scheduler: SchedulerData[id=1, name=TimeMachineScheduler],
    schedulerNode: SchedulerNode[nodeId=1, name=Jespers-MacBook-Air.local, ip=192.168.0.10],
    configProps: null,
    dataStore: MemoryDataStore[name=384414907],
    scheduleRunner: PollingScheduleRunner[name=779064443],
    classLoader: SimpleClassLoaderService[name=420626096],
    jobTaskFactory: SimpleJobTaskFactory[name=240567247],
    jobTaskPoolNameResolver: SimpleJobTaskPoolNameResolver[name=1646996211],
    jobTaskThreadPool: DynamicThreadPool[name=jobTaskThreadPool.DEFAULT],
    ]
    Caught: timemachine.scheduler.SchedulerException: Failed to read version file to get published date.
    timemachine.scheduler.SchedulerException: Failed to read version file to get published date.
    at timemachine.scheduler.support.VersionUtils.getSchedulerVersion(VersionUtils.java:73)
    at timemachine.scheduler.service.SchedulerEngine.afterInit(SchedulerEngine.java:166)
    at timemachine.scheduler.service.SchedulerEngine.init(SchedulerEngine.java:109)
    at timemachine.scheduler.Service$init.call(Unknown Source)
    at test.run(test.groovy:9)
    Caused by: java.text.ParseException: Unparseable date: "Mon Jun 18 13:50:47 EDT 2012"
    at timemachine.scheduler.support.VersionUtils.getSchedulerVersion(VersionUtils.java:64)
    ... 4 more

    ReplyDelete
    Replies
    1. Hi there, I fixed a bug that might cause this error. See https://bitbucket.org/timemachine/scheduler/issue/27

      You may try the 1.2.2 release and see if it works for you. (I just released it, so you might have to wait few hours before maven central gets it.)

      Delete
  3. Hello there, what version of groovy are you using? I just tried it and it works fine. If you needed further help, please try the user forum on the project.

    ZEMIANs-iMac:test zemian$ groovy -version
    Groovy Version: 2.0.0 JVM: 1.7.0_05 Vendor: Oracle Corporation OS: Mac OS X
    ZEMIANs-iMac:test zemian$ groovy test.groovy
    63 [main] INFO timemachine.scheduler.service.SchedulerEngine - TimeScheduler system services initialized: [
    scheduler: SchedulerData[id=1, name=TimeMachineScheduler],
    schedulerNode: SchedulerNode[nodeId=1, name=ZEMIANs-iMac.local, ip=192.168.1.135],
    configProps: null,
    dataStore: MemoryDataStore[name=253306491],
    scheduleRunner: PollingScheduleRunner[name=669892518],
    classLoader: SimpleClassLoaderService[name=1690712135],
    jobTaskFactory: SimpleJobTaskFactory[name=1295745749],
    jobTaskPoolNameResolver: SimpleJobTaskPoolNameResolver[name=1378268624],
    jobTaskThreadPool: DynamicThreadPool[name=jobTaskThreadPool.DEFAULT],
    ]
    71 [main] INFO timemachine.scheduler.service.SchedulerEngine - Scheduler[id=1, nodeId=1, nodeIp=192.168.1.135] initialized. Version=1.1.1.061820121350
    93 [main] INFO timemachine.scheduler.service.SchedulerEngine - Scheduled JobDef[id=1, name=null] with RepeatSchedule[id=1, nextRun=09/19/2012 18:41:13].
    94 [main] INFO timemachine.scheduler.service.SchedulerEngine - Scheduler[id=1, nodeId=1, nodeIp=192.168.1.135] started.
    Hello World.
    Hello World.
    Hello World.
    Hello World.
    Hello World.

    ReplyDelete