Skip to content

Instantly share code, notes, and snippets.

@makoConstruct
Created April 16, 2015 00:08
Show Gist options
  • Save makoConstruct/277c5a432518ec501842 to your computer and use it in GitHub Desktop.
Save makoConstruct/277c5a432518ec501842 to your computer and use it in GitHub Desktop.
scheduled tasks functions for nouvion
#usage: schedule_scheduled_task(time:Time, task_name:String, task_parameter:String)
# runs the function task_name refers to(big trouble if it doesn't refer to a function) with task_parameter.
# Will record the task to DB if the bot is shut down. Will restore to the schedule when bot is restarted.
#TODO:
# implement schedule_task(time, callback) [where time is the time at which it should run, rather than how long till it should run]
# do we have simple memory access shims for mem_load_all(key), and mem_save(key, value)? If not, we should, please write them.
# mem_delete_k_v(key, value) , this is new functionality, but greatly needed
# run reinstate_scheduled_tasks on initialization (note, may execute tasks during the call, make sure initialization is at a stage where it's ready for that)
# should work at that point.
def reinstate_scheduled_tasks
@tasks_in_progress = {}
for task in mem_load_all("global timed tasks")
tstsp = task.split("::")
time = Time.new tstsp[0]
task = tstsp[1]
parameter = tstsp[2]
schedule_scheduled_task(time, task, parameter)
mem_delete_k_v("global timed tasks", task)
end
end
def schedule_scheduled_task(time, task, parameter)
now = Time.now
if time < now
@[task](parameter)
else
mem_save("global timed tasks", "#{time.to_s}::#{task}::#{parameter}")
schedule_task time, {|| @[task](parameter)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment