Class: Gnomon
- Inherits:
-
Object
- Object
- Gnomon
- Includes:
- Runify
- Defined in:
- lib/gnomon.rb,
lib/gnomon/version.rb
Overview
Gnomon Class
Constant Summary collapse
- DEFAULT_GRANULARITY =
Defaults
1
- VERSION =
Version
'1.1.1'
Instance Method Summary collapse
-
#deschedule(id, keep_running = false) ⇒ Object
De-schedule: Removes one or more previously-scheduled events from the scheduler.
-
#initialize(granularity = DEFAULT_GRANULARITY) ⇒ Gnomon
constructor
Construct: Creates a new Gnomon instance.
-
#schedule(id, mode, mode_options, timespec, *args, &block) ⇒ Object
Schedule Event: Schedules a generic event.
-
#schedule_at(id, date_time, *args, &block) ⇒ Object
Schedule Event at a given date/time: Shortcut to schedule an event to be triggered once at a specific date/time.
-
#schedule_every(id, interval, async, *args, &block) ⇒ Object
Schedule Event at a given interval (seconds): Shortcut to schedule an event to be triggered periodically at a given time interval.
-
#schedule_in(id, time, *args, &block) ⇒ Object
Schedule Event in a given number of seconds: Shortcut to schedule an event to be triggered once after a given amount of time.
Constructor Details
#initialize(granularity = DEFAULT_GRANULARITY) ⇒ Gnomon
Construct: Creates a new Gnomon instance.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gnomon.rb', line 25 def initialize granularity = DEFAULT_GRANULARITY # Set Granularity @granularity = granularity # Create Event Queue @equeue = [] # Create Event Schedule @esched = [] # Create Lock @lock = Mutex.new end |
Instance Method Details
#deschedule(id, keep_running = false) ⇒ Object
De-schedule: Removes one or more previously-scheduled events from the scheduler.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/gnomon.rb', line 106 def deschedule id, keep_running = false # Synchronize @lock.synchronize do # De-schedule Event @esched.delete_if { |e| e[:id] == id } # De-queue any next run @equeue.delete_if { |e| e[:event][:id] == id } unless keep_running @equeue.each { |e| e[:event][:last_run] = true if e[:event][:id] == id } if keep_running end end |
#schedule(id, mode, mode_options, timespec, *args, &block) ⇒ Object
Schedule Event: Schedules a generic event.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gnomon.rb', line 48 def schedule id, mode, , timespec, *args, &block # Synchronize @lock.synchronize do # Create Event e = { id: id, ts: timespec, mode: mode, mopt: , args: args, blck: block } # Register Event Scheduling @esched << e # Enqueue Next Run enqueue_next_run e end end |
#schedule_at(id, date_time, *args, &block) ⇒ Object
Schedule Event at a given date/time: Shortcut to schedule an event to be triggered once at a specific date/time.
77 78 79 |
# File 'lib/gnomon.rb', line 77 def schedule_at id, date_time, *args, &block schedule id, :at, {}, date_time, *args, &block end |
#schedule_every(id, interval, async, *args, &block) ⇒ Object
Schedule Event at a given interval (seconds): Shortcut to schedule an event to be triggered periodically at a given time interval.
98 99 100 |
# File 'lib/gnomon.rb', line 98 def schedule_every id, interval, async, *args, &block schedule id, :every, { async: async }, interval, *args, &block end |
#schedule_in(id, time, *args, &block) ⇒ Object
Schedule Event in a given number of seconds: Shortcut to schedule an event to be triggered once after a given amount of time.
87 88 89 |
# File 'lib/gnomon.rb', line 87 def schedule_in id, time, *args, &block schedule id, :in, {}, time, *args, &block end |