Class: Gnomon

Inherits:
Object
  • Object
show all
Includes:
Runify
Defined in:
lib/gnomon.rb,
lib/gnomon/version.rb

Overview

Gnomon Class

Constant Summary collapse

DEFAULT_GRANULARITY =

Defaults

1
VERSION =

Version

'1.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(granularity = DEFAULT_GRANULARITY) ⇒ Gnomon

Construct: Creates a new Gnomon instance.

Parameters:

  • granularity (Float) (defaults to: DEFAULT_GRANULARITY)

    The desired resolution of the event queue, in seconds



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.

Parameters:

  • id (Object)

    The ID of the event(s) to remove (all events with this ID will be removed)

  • keep_running (Object) (defaults to: false)

    When true, the next trigger is maintained



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.

Parameters:

  • id (Object)

    An ID for later de-scheduling of this event

  • mode (Symbol)

    The mode of scheduling - can be any of [ :at, :in, :every ]

  • mode_options (Hash)

    Mode-specific options hash - See documentation for desired mode

  • timespec (Object)

    Mode-specific time-specification for ‘when’ to trigger the event

  • args (Object)

    Arguments to be passed as-is to the event’s block upon execution

  • block (Object)

    A block to be executed upon event trigger



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, mode_options, timespec, *args, &block

	# Synchronize
	@lock.synchronize do

		# Create Event
		e =  {
			id: id,
			ts: timespec,
			mode: mode,
			mopt: mode_options,
			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.

Parameters:

  • id (Object)
  • date_time (String)

    A date / time string

  • args (Object)
  • block (Object)


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.

Parameters:

  • id (Object)
  • interval (Float)
  • async (boolean)

    When true, schedule the next trigger before actually running the event’s block - otherwise, schedule the next trigger only once the event block completes

  • args (Object)
  • block (Object)


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.

Parameters:

  • id (Object)
  • time (Float)
  • args (Object)
  • block (Object)


87
88
89
# File 'lib/gnomon.rb', line 87

def schedule_in id, time, *args, &block
	schedule id, :in, {}, time, *args, &block
end