Skip to content

Commit

Permalink
Add some basic support for reading tasks from configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Feb 21, 2016
1 parent b067ff0 commit 0e732cd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
20 changes: 12 additions & 8 deletions grails-app/init/BootStrap.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ class BootStrap {
def tasks

def init = { servletContext ->
String externalConfig = System.getenv("DUGA_CONFIG");
if (externalConfig) {
File file = new File(externalConfig)
if (file.exists()) {

}
}

def users = User.list()
if (users.isEmpty()) {
def roleUser = new Authority(authority: 'ROLE_USER').save(failOnError: true)
Expand All @@ -31,6 +23,18 @@ class BootStrap {
UserAuthority.create(admin, roleAdmin, true)
}
tasks.initOnce()

//String externalConfig = System.getenv("DUGA_CONFIG");
URL config = getClass().getClassLoader().getResource('init-tasks.groovy')
if (config) {
tasks.fromGroovyDSL(config.text)
}
/* if (externalConfig) {
File file = new File(externalConfig)
if (file.exists()) {
}
}*/
}
def destroy = {
}
Expand Down
80 changes: 78 additions & 2 deletions grails-app/services/net/zomis/duga/DugaTasks.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.zomis.duga.tasks.UnansweredTask
import net.zomis.duga.tasks.UserRepDiffTask
import net.zomis.duga.tasks.qscan.QuestionScanTask
import org.apache.log4j.Logger
import org.codehaus.groovy.control.CompilerConfiguration
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.TaskScheduler
import org.springframework.scheduling.support.CronTrigger
Expand Down Expand Up @@ -53,10 +54,10 @@ class DugaTasks {

private class TaskRunner implements Runnable {

private final TaskData data;
private final Object data;
private final Runnable task;

public TaskRunner(TaskData data, Runnable runnable) {
public TaskRunner(Object data, Runnable runnable) {
this.data = data;
this.task = runnable;
}
Expand Down Expand Up @@ -101,4 +102,79 @@ class DugaTasks {
return new ArrayList<>(taskData);
}

public void fromGroovyDSL(String groovyCode) {
def cc = new CompilerConfiguration()
cc.setScriptBaseClass(TasksDelegate.class.name)
GroovyShell sh = new GroovyShell(cc);
println "parsing script"
TasksDelegate script = (TasksDelegate) sh.parse(groovyCode, 'InitTasks.groovy')
println "setting script outer to $this on $script"
script.setOuter(this);
println "outer is now $script.outer called from $this on $script, running script"
script.run()
println "script finished"
for (CronRunnable cronRunnable : script.data) {
def runner = new TaskRunner(cronRunnable, cronRunnable.runnable)
ScheduledFuture<?> future = scheduler.schedule(runner, cronRunnable.trigger)
tasks.add(future)
System.out.println("Added task: $cronRunnable with cron $cronRunnable.trigger")
}
}

public static class CronRunnable {
CronTrigger trigger;
Runnable runnable;
}

public static abstract class TasksDelegate extends Script {

private final List<CronRunnable> data = [];
CronTrigger trigger;
private DugaTasks outer;

public List<CronRunnable> getData() {
return this.@data
}

public void tasks(Closure closure) {
println "tasks run closure with " + this.@outer
closure.setDelegate(this)
closure.run()
}

public void cron(String cron, Closure closure) {
println "cron run closure with " + this.@outer
trigger = new CronTrigger(cron, TimeZone.getTimeZone("UTC"))
closure.delegate = this
closure.run()
}

public void message(String room, String message) {
println "get outer returns: " + getOuter()
createTask(new MessageTask(this.@outer.chatBot, room, message))
}

public void repdiff(String room, String site, int user1, int user2) {
createTask(new UserRepDiffTask(this.@outer.stackAPI, room, this.@outer.chatBot, "$user1,$user2", site))
}

private void createTask(Runnable runnable) {
CronRunnable cronRunnable = new CronRunnable()
cronRunnable.trigger = trigger;
cronRunnable.runnable = runnable;
data.add(cronRunnable)
}

public void setOuter(DugaTasks outer) {
println "Set outer to $outer on $this"
this.@outer = outer
}

public DugaTasks getOuter() {
println "get outer for $this"
this.@outer
}

}

}

0 comments on commit 0e732cd

Please sign in to comment.