Class: Notifications

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

Class Method Summary collapse

Class Method Details

.blocking_since(seq) ⇒ Object



56
57
58
# File 'backend/app/lib/notifications.rb', line 56

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

.expire_old_notificationsObject



61
62
63
64
65
# File 'backend/app/lib/notifications.rb', line 61

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

.initObject



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

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'backend/app/lib/notifications.rb', line 20

def self.notify(code, params = {}, immediate = true)
  if DB.in_transaction?
    # Don't bother emitting a notification if we already have an identical one
    # queued one up for this database transaction anyway.  Prevents floods of
    # identical notifications during large imports.
    DB.session_storage[:emitted_notifications] ||= []
    notification = {:code => code, :params => params.to_json}

    if DB.session_storage[:emitted_notifications].include?(notification)
      # Already got this one
      return
    else
      DB.session_storage[:emitted_notifications] << notification
    end
  end

  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

.shutdownObject



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

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

.since(seq) ⇒ Object



51
52
53
# File 'backend/app/lib/notifications.rb', line 51

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

.start_background_threadObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'backend/app/lib/notifications.rb', line 68

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