Class: Artery::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/artery/sync.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sync_id) ⇒ Sync

Returns a new instance of Sync.



7
8
9
# File 'lib/artery/sync.rb', line 7

def initialize(sync_id)
  @sync_id = sync_id
end

Instance Attribute Details

#sync_idObject

Returns the value of attribute sync_id.



5
6
7
# File 'lib/artery/sync.rb', line 5

def sync_id
  @sync_id
end

Class Method Details

.run(subscriptions) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/artery/sync.rb', line 26

def self.run(subscriptions)
  sync_id = SecureRandom.hex
  Artery.logger.tagged('Sync', sync_id) do
    Artery::Sync.new(sync_id).execute subscriptions
  ensure
    Artery.clear_synchronizing_subscriptions!
  end
end

Instance Method Details

#execute(services = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/artery/sync.rb', line 11

def execute(services = nil)
  services = Array.wrap(services).map(&:to_sym)
  subscriptions_on_services = services.blank? ? Artery.subscriptions : Artery.subscriptions_on(services)

  if subscriptions_on_services.blank?
    Artery.logger.warn 'No suitable subscriptions defined, exiting...'
    return
  end

  @sync_fiber = Fiber.new do # all synchronization inside must be synchronous
    subscriptions_on_services.values.flatten.uniq.each(&:synchronize!)
  end
  @sync_fiber.resume
end