-
Notifications
You must be signed in to change notification settings - Fork 12
Data Syncing: Publishing
TableSync can be used to send data using RabbitMQ.
You can do in two ways. Automatic and manual. Each one has its own pros and cons.
Automatic is used to publish changes in realtime, as soon as the tracked entity changes. Usually syncs one entity at a time.
Manual allows to sync a lot of entities per message. But demands greater amount of work and data preparation.
Include TableSync.sync(self)
into a Sequel or ActiveRecord model.
Options:
-
if:
andunless:
- Runs given proc in the scope of an instance. Skips sync onfalse
forif:
and ontrue
forunless:
. -
on:
- specify events (create
,update
,destroy
) to trigger sync on. Triggered for all of them without this option. -
debounce_time
- min time period allowed between synchronizations.
Functioning Rails.cache
is required.
How it works:
-
TableSync.sync(self)
- registers new callbacks (forcreate
,update
,destroy
) for ActiveRecord model, and definesafter_create
,after_update
andafter_destroy
callback methods for Sequel model. -
Callbacks call
TableSync::Publishing::Single#publish_later
with given options and object attributes. It enqueues a job which then publishes a message.
Example:
class SomeModel < Sequel::Model
TableSync.sync(self, { if: -> (*) { some_code }, unless: -> (*) { some_code }, on: [:create, :update] })
end
class SomeOtherModel < Sequel::Model
TableSync.sync(self)
end
Sometimes destroy event can happen for non persisted record. In this case we can expect the following:
For Sequel: 'Sequel::NoExistingObject' is raised. (This is default Sequel behaviour) For Active Record: Publishing is skipped.
Example:
# ActiveRecord
user = User.new.destroy! # Publishing is skipped.
# Sequel
user = User.new.destroy! # raise Sequel::NoExistingObject
Directly call one of the publishers. It's the best if you need to sync a lot of data. This way you don't even need for the changes to occur.
Example:
TableSync::Publishing::Batch.new(
object_class: "User",
original_attributes: [{ id: 1 }, { id: 2 }],
event: :update,
).publish_now