class UrlTracker::Periodic
Small class wrapping EventMachine calls to programatically execute code blocks.
Constants
- TIME_UNITS
maybe consider :day in the future
Public Class Methods
new()
click to toggle source
Creates a new instance of UrlTracker::Periodic
and starts the event loop.
# File lib/url_tracker/periodic.rb, line 18 def initialize @named_tasks = {} start_event_loop end
Public Instance Methods
every(*args, &block)
click to toggle source
Register a new task to be executed in a specified amount of time. Examples:
p = UrlTracker::Periodic.new p.every(:minute) { do_something } #=> executed every minute p.every(2, :minutes) { do_something } #=> executed every 2 minutes p.every(4, :hours) { do_something } #=> executed every 4 hours
# File lib/url_tracker/periodic.rb, line 30 def every(*args, &block) time = 1 case args.first when Integer then time = args[0]*seconds_for(args[1]) when Symbol then time *= seconds_for(args[0]) else raise "Invalid period #{args[0].inspect}" end task = { every: time, task: block } task.merge!(name: @name) if named_task? schedule_task(task) @name = nil time end
named_tasks()
click to toggle source
Returns named tasks registered. Example
p = UrlTracker::Periodic.new p.task(:foo).every(:minute) { do_something} p.task(:bar).every(2, :minute) { do_other_thing } p.named_tasks #=> [:foo, :bar]
# File lib/url_tracker/periodic.rb, line 55 def named_tasks task_names end
remove_task(name)
click to toggle source
Removes a task named name
, so that it will no longer run
# File lib/url_tracker/periodic.rb, line 60 def remove_task(name) raise "Unregistered task #{name.inspect}" unless @named_tasks.include?(name) unschedule_task(name) end
restart()
click to toggle source
Restarts the event loop
# File lib/url_tracker/periodic.rb, line 67 def restart stop if running? start_event_loop end
running?()
click to toggle source
Checks if the tasks are running
# File lib/url_tracker/periodic.rb, line 73 def running? @event_thread.alive? end
stop()
click to toggle source
Stop all scheduled tasks
# File lib/url_tracker/periodic.rb, line 78 def stop @event_thread.terminate @event_thread.join end
task(name)
click to toggle source
Used for creating named tasks or, in other words, tasks that can be removed later using remove
# File lib/url_tracker/periodic.rb, line 85 def task(name) @name = name.to_s self end
Private Instance Methods
named_task?()
click to toggle source
# File lib/url_tracker/periodic.rb, line 92 def named_task? !@name.nil? end
schedule_task(t)
click to toggle source
task
is expected to be in the format:
{ every: 60, task: #<Proc:0x...> }
for a task to be run every minute, for example.
# File lib/url_tracker/periodic.rb, line 106 def schedule_task(t) periodic_timer = EM.add_periodic_timer(t[:every], &t[:task]) @named_tasks[@name] = periodic_timer if named_task? end
seconds_for(time_unit)
click to toggle source
# File lib/url_tracker/periodic.rb, line 96 def seconds_for(time_unit) raise "Unkown time unit #{time_unit.inspect}" unless TIME_UNITS.include?(time_unit) TIME_UNITS[time_unit] end
start_event_loop()
click to toggle source
# File lib/url_tracker/periodic.rb, line 111 def start_event_loop # start the event loop in a separate thread @event_thread = Thread.new { EM.run } # Wait for the reactor to be ready while !@event_thread.stop?; end end
task_names()
click to toggle source
# File lib/url_tracker/periodic.rb, line 119 def task_names @named_tasks.keys end
unschedule_task(name)
click to toggle source
# File lib/url_tracker/periodic.rb, line 123 def unschedule_task(name) EM.cancel_timer(@named_tasks.delete(name)) end