class Termtter::TaskManager
Public Class Methods
new(interval = 1)
click to toggle source
# File lib/termtter/task_manager.rb, line 5 def initialize(interval = 1) @interval = interval @tasks = {} @work = true @mutex = Mutex.new @pause = false end
Public Instance Methods
add_task(args = {}, &block)
click to toggle source
# File lib/termtter/task_manager.rb, line 54 def add_task(args = {}, &block) synchronize do task = Task.new(args, &block) @tasks[task.name || task.object_id] = task end end
delete_task(key)
click to toggle source
# File lib/termtter/task_manager.rb, line 67 def delete_task(key) synchronize do @tasks.delete(key) end end
get_task(key)
click to toggle source
# File lib/termtter/task_manager.rb, line 61 def get_task(key) synchronize do @tasks[key] end end
invoke_and_wait() { || ... }
click to toggle source
# File lib/termtter/task_manager.rb, line 48 def invoke_and_wait(&block) synchronize do yield end end
invoke_later() { || ... }
click to toggle source
# File lib/termtter/task_manager.rb, line 42 def invoke_later Thread.new do invoke_and_wait { yield } end end
kill()
click to toggle source
# File lib/termtter/task_manager.rb, line 21 def kill @work = false end
pause()
click to toggle source
# File lib/termtter/task_manager.rb, line 13 def pause @pause = true end
resume()
click to toggle source
# File lib/termtter/task_manager.rb, line 17 def resume @pause = false end
run()
click to toggle source
# File lib/termtter/task_manager.rb, line 25 def run Thread.new do while @work step unless @pause sleep @interval end end end
step()
click to toggle source
# File lib/termtter/task_manager.rb, line 34 def step pull_due_tasks.each do |task| invoke_and_wait do task.execute end end end
Private Instance Methods
pull_due_tasks()
click to toggle source
# File lib/termtter/task_manager.rb, line 86 def pull_due_tasks() synchronize do time_now = Time.now due_tasks = [] @tasks.delete_if do |key, task| if task.work && task.exec_at <= time_now due_tasks << task if task.interval task.exec_at = time_now + task.interval false else true end else false end end return due_tasks end end
synchronize() { || ... }
click to toggle source
# File lib/termtter/task_manager.rb, line 75 def synchronize unless Thread.current == @thread_in_sync @mutex.synchronize do @thread_in_sync = Thread.current yield end else yield end end