Class: Notifications

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/notifications.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) blocking_since(seq)



41
42
43
# File 'backend/app/lib/notifications.rb', line 41

def self.blocking_since(seq)
  @longpolling.blocking_updates_since(seq)
end

+ (Object) expire_old_notifications



46
47
48
49
50
# File 'backend/app/lib/notifications.rb', line 46

def self.expire_old_notifications
  DB.open do |db|
    db[:notification].where {time < (Time.now - ((AppConfig[:notifications_backlog_ms] / 1000.0) * 2))}.delete
  end
end

+ (Object) init



6
7
8
9
10
11
12
# File 'backend/app/lib/notifications.rb', line 6

def self.init
  @longpolling = LongPolling.new(AppConfig[:notifications_backlog_ms])

  @last_sequence = 0

  start_background_thread
end

+ (Object) notify(code, params = {}, immediate = true)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'backend/app/lib/notifications.rb', line 20

def self.notify(code, params = {}, immediate = true)
  DB.after_commit do
    DB.open do |db|
      db[:notification].insert(:code => code, :params => DB.blobify(params.to_json),
                               :time => Time.now)
    end

    if immediate
      # Fire it out straight away.  This will cause duplicates when the same
      # notification is read from the database, but that's OK.
      @longpolling.record_update(:code => code, :params => params)
    end
  end
end

+ (Object) shutdown



15
16
17
# File 'backend/app/lib/notifications.rb', line 15

def self.shutdown
  @longpolling.shutdown if @longpolling
end

+ (Object) since(seq)



36
37
38
# File 'backend/app/lib/notifications.rb', line 36

def self.since(seq)
  @longpolling.updates_since(seq)
end

+ (Object) start_background_thread



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'backend/app/lib/notifications.rb', line 53

def self.start_background_thread
  frequency = AppConfig[:notifications_poll_frequency_ms] / 1000.0

  Thread.new do
    while true
      begin
        sleep frequency

        DB.open do |db|
          last_sequence = @last_sequence
          db[:notification].where {id > last_sequence}.all.each do |row|
            @longpolling.record_update(:code => row[:code], :params => ASUtils.json_parse(row[:params]))
            @last_sequence = row[:id] if row[:id] > @last_sequence
          end
        end
      rescue
        Log.warn("#{$!}: #{$@}")
      end
    end
  end
end