aboutsummaryrefslogtreecommitdiffstats
path: root/worker_node/lib/event_channel.rb
blob: c1c0d9c9230652948379b69141c8042e7af70097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class EventChannel
  class << self
    def setup
      return if @dalli
      @dalli = Dalli::Client.new(Settings.memcached, namespace: "aclog-worker-node")
      @channel = EM::Channel.new
    end

    def push(data)
      raise ScriptError, "Call EventChannel.setup first" unless @dalli
      if id = data[:identifier]
        key, val = id.split("#", 2)
        cur = @dalli.get(key)
        if cur && (!val || (cur <=> val) > -1)
          WorkerNode.logger.debug("UniqueChannel") { "Duplicate event: #{key}" }
          return
        else
          @dalli.set(key, val || true)
        end
      end
      @channel << data
    end
    alias << push

    def subscribe(&blk)
      raise ScriptError, "Call EventChannel.setup first" unless @channel
      @channel.subscribe &blk
    end
  end
end