Class: UrlTracker::Periodic
- Inherits:
-
Object
- Object
- UrlTracker::Periodic
- Defined in:
- lib/url_tracker/periodic.rb
Overview
Small class wrapping EventMachine calls to programatically execute code blocks.
Constant Summary collapse
- TIME_UNITS =
maybe consider :day in the future
{ minute: 60, minutes: 60, hour: 60*60, hours: 60*60 }
Instance Method Summary collapse
-
#every(*args, &block) ⇒ Object
Register a new task to be executed in a specified amount of time.
-
#initialize ⇒ Periodic
constructor
Creates a new instance of UrlTracker::Periodic and starts the event loop.
-
#named_tasks ⇒ Object
Returns named tasks registered.
-
#remove_task(name) ⇒ Object
Removes a task named
name
, so that it will no longer run. -
#restart ⇒ Object
Restarts the event loop.
-
#running? ⇒ Boolean
Checks if the tasks are running.
-
#stop ⇒ Object
Stop all scheduled tasks.
-
#task(name) ⇒ Object
Used for creating named tasks or, in other words, tasks that can be removed later using #remove.
Constructor Details
#initialize ⇒ Periodic
Creates a new instance of UrlTracker::Periodic and starts the event loop.
18 19 20 21 |
# File 'lib/url_tracker/periodic.rb', line 18 def initialize @named_tasks = {} start_event_loop end |
Instance Method Details
#every(*args, &block) ⇒ Object
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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 ⇒ Object
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]
55 56 57 |
# File 'lib/url_tracker/periodic.rb', line 55 def named_tasks task_names end |
#remove_task(name) ⇒ Object
Removes a task named name
, so that it will no longer run
60 61 62 63 64 |
# 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 ⇒ Object
Restarts the event loop
67 68 69 70 |
# File 'lib/url_tracker/periodic.rb', line 67 def restart stop if running? start_event_loop end |
#running? ⇒ Boolean
Checks if the tasks are running
73 74 75 |
# File 'lib/url_tracker/periodic.rb', line 73 def running? @event_thread.alive? end |
#stop ⇒ Object
Stop all scheduled tasks
78 79 80 81 |
# File 'lib/url_tracker/periodic.rb', line 78 def stop @event_thread.terminate @event_thread.join end |
#task(name) ⇒ Object
Used for creating named tasks or, in other words, tasks that can be removed later using #remove
85 86 87 88 |
# File 'lib/url_tracker/periodic.rb', line 85 def task(name) @name = name.to_s self end |