Class: Artery::NoBrainer::SubscriptionInfo

Inherits:
Object
  • Object
show all
Includes:
NoBrainer::Document
Defined in:
lib/artery/no_brainer/subscription_info.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_for_subscription(subscription) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/artery/no_brainer/subscription_info.rb', line 21

def find_for_subscription(subscription)
  params = {
    subscriber: subscription.subscriber.to_s,
    service: subscription.uri.service,
    model: subscription.uri.model
  }

  info = where(params).first || new(params)

  info.save! if info.new_record?
  info
end

Instance Method Details

#lock_for_message(message, &blk) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/artery/no_brainer/subscription_info.rb', line 58

def lock_for_message(message, &blk)
  if message.has_index? # only 'indexed' messages should lock
    with_lock(&blk)
  else
    yield
  end
end

#with_lockObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/artery/no_brainer/subscription_info.rb', line 35

def with_lock
  was_locked = @lock.present?

  if was_locked # only 'indexed' messages should lock
    yield
  else
    Artery.logger.debug "WAITING FOR LOCK... [LATEST_INDEX: #{latest_index}]"

    lock = ::NoBrainer::Lock.new("artery_subscription_info:#{model}")

    lock.synchronize do
      Artery.logger.debug "GOT LOCK! [LATEST_INDEX: #{latest_index}]"
      reload # need fresh record

      @lock = lock

      yield
    end
  end
ensure
  @lock = nil unless was_locked
end