Skip to content

Commit

Permalink
cyclic execution of scripts, fixes #264
Browse files Browse the repository at this point in the history
  • Loading branch information
mdzio committed Aug 27, 2022
1 parent 30b4ae1 commit e4011bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
8 changes: 6 additions & 2 deletions ccu-historian/src/mdz/ccuhistorian/Configuration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@ class Configuration {
} catch (FileNotFoundException ex) {
throw new Exception("Can't read configuration file $cmdLineConfig.configFileName", ex)
}


ImportCustomizer customizer=[]
customizer.addStaticStars 'java.lang.Math'
customizer.addImport 'PreprocType', 'mdz.ccuhistorian.eventprocessing.Preprocessor.Type'
customizer.addImports 'mdz.ccuhistorian.TrendDesign', 'java.util.logging.Level',
'java.awt.Color', 'org.jfree.chart.ChartColor', 'java.awt.BasicStroke',
'java.awt.GradientPaint', 'org.jfree.chart.title.TextTitle'
'java.awt.GradientPaint', 'org.jfree.chart.title.TextTitle',
'mdz.hc.DataPoint', 'mdz.hc.DataPointIdentifier', 'mdz.hc.Event',
'mdz.hc.ProcessValue', 'mdz.hc.RawEvent', 'mdz.hc.timeseries.TimeSeries'
customizer.addStaticStars 'mdz.ccuhistorian.ManagerConfigurator.DeviceTypes'
customizer.addStaticStars 'mdz.ccuhistorian.ManagerConfigurator.PlugInTypes'
CompilerConfiguration config=[]
Expand Down
40 changes: 24 additions & 16 deletions ccu-historian/src/mdz/ccuhistorian/DatabaseSystem.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,42 @@ public class DatabaseSystem extends BaseSystem {
}

def binding=new Binding()
binding.setVariable("log", Logger.getLogger("mdz.task.$name"))
def scriptLog=Logger.getLogger("mdz.task.$name")
binding.setVariable("log", scriptLog)
binding.setVariable("database", extendedStorage)

task.script.delegate=binding
task.script.resolveStrategy=Closure.DELEGATE_ONLY

// setup cron scheduler
Cron cron=cparser.parse(task.cron)
ExecutionTime execTime=ExecutionTime.forCron(cron)
scheduleNext(execTime, task)
// schedule task
if (task.cron=="@start") {
// run once
executeTask(task, 0, {})
} else {
// setup cron
Cron cron=cparser.parse(task.cron)
ExecutionTime execTime=ExecutionTime.forCron(cron)
executeCyclic(execTime, task)
}
}
}
}

private scheduleNext(ExecutionTime execTime, DatabaseConfig.Task task) {
def now=ZonedDateTime.now()
def nextExec=execTime.nextExecution(now).get()
log.fine "Next execution of task $task.name: ${nextExec.toLocalDateTime()}"
def delay=ChronoUnit.MILLIS.between(now, nextExec)

private executeTask(DatabaseConfig.Task task, long delay, Closure onCompleted) {
base.executor.schedule({
log.fine "Executing task: $task.name"
// execute script
Exceptions.catchToLog(log) {
task.script.call()
}

// reschedule
scheduleNext(execTime, task)
onCompleted()
} as Runnable, delay, TimeUnit.MILLISECONDS)
}
}

private executeCyclic(ExecutionTime execTime, DatabaseConfig.Task task) {
def now=ZonedDateTime.now()
def nextExec=execTime.nextExecution(now).get()
log.fine "Next execution of task $task.name: ${nextExec.toLocalDateTime()}"
def delay=ChronoUnit.MILLIS.between(now, nextExec)
executeTask(task, delay, { executeCyclic(execTime, task) })
}
}

0 comments on commit e4011bc

Please sign in to comment.