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

'0.1.2'

Instance Method Summary collapse

Constructor Details

#initialize(granularity = DEFAULT_GRANULARITY) ⇒ Gnomon

Construct



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gnomon.rb', line 22

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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gnomon.rb', line 77

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gnomon.rb', line 38

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



62
63
64
# File 'lib/gnomon.rb', line 62

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)



72
73
74
# File 'lib/gnomon.rb', line 72

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



67
68
69
# File 'lib/gnomon.rb', line 67

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