-
Notifications
You must be signed in to change notification settings - Fork 12
Publishing: Publishers
Ivan Chernov edited this page Nov 11, 2022
·
1 revision
There are three publishers you can use to send data.
-
TableSync::Publishing::Single- sends one row with initialization. -
TableSync::Publishing::Batch- sends a batch of rows with initialization. -
TableSync::Publishing::Raw- sends raw data without checks.
TableSync::Publishing::Single - sends one row with initialization.
This is a publisher called by TableSync.sync(self).
-
object_class- class (model) used to initialize published object -
original_attributes- attributes used to initializeobject_classwith -
debounce_time- minimum allowed time between delayed publishings -
event- type of event that happened to the published object (create,update,destroy);updateby default
- takes in the
original_attributes,object_class,event - constantizes
object_class - extracts the primary key (
needle) of theobject_classfrom theoriginal_attributes - queries the database for the object with the
needle(forupdateandcreate) or initializes theobject_classwithoriginal_attributes(fordestroy) - constructs routing_key using
routing_key_callableand#attributes_for_routing_key(if defined) - constructs headers using
headers_callableand#attributes_for_headers(if defined) - publishes Rabbit message (uses attributes from queried/initialized object as data)
- sends notification (if set up)
- takes in the
original_attributes,object_class,debounce_time,event - serializes the
original_attributes, silently filters out unserializable keys/values - enqueues (or skips) the job with the
serialized_original_attributesto be performed in time according to debounce - job (if enqueued) calls
TableSync::Publishing::Single#publish_nowwithserialized_original_attributesand the sameobject_class,debounce_time,event
Currently allowed key/values are:
NilClass, String, TrueClass, FalseClass, Numeric, Symbol.
Job is defined in TableSync.single_publishing_job_class_callable as a proc. Read more in Configuration.
TableSync::Publishing::Single.new(
object_class: "User",
original_attributes: { id: 1, name: "Mark" },
debounce_time: 60, # useless for #publish _now, can be skipped
event: :create,
).publish_now TableSync::Publishing::Single.new(
object_class: "User",
original_attributes: { id: 1, name: "Mark" }, # will be serialized!
debounce_time: 60,
event: :update,
).publish_later-
TableSync::Publishing::Batch- sends a batch of rows with initialization.
-
object_class- class (model) used to initialize published objects -
original_attributes- array of attributes used to initializeobject_classwith -
event- type of event that happened to the published objects (create,update,destroy);updateby default -
routing_key- custom routing_key -
headers- custom headers
- takes in the
original_attributes,object_class,event,routing_key,headers - constantizes
object_class - extracts primary keys (
needles) of theobject_classfrom the array oforiginal_attributes - queries the database for the objects with
needles(forupdateandcreate) or initializes theobject_classwithoriginal_attributes(fordestroy) - constructs routing_key using
routing_key_callable(ignores#attributes_for_routing_key) or usesrouting_keyif given - constructs headers using
headers_callable(ignores#attributes_for_headers) or usesheadersif given - publishes Rabbit message (uses attributes from queried/initialized objects as data)
- sends notification (if set up)
- takes in the
original_attributes,object_class,event,routing_key,headers - serializes the array of
original_attributes, silently filters out unserializable keys/values - enqueues the job with the
serialized_original_attributes - job calls
TableSync::Publishing::Batch#publish_nowwithserialized_original_attributesand the sameobject_class,event,routing_key,headers
Currently allowed key/values are:
NilClass, String, TrueClass, FalseClass, Numeric, Symbol.
Job is defined in TableSync.batch_publishing_job_class_callable as a proc. Read more in Configuration.
TableSync::Publishing::Batch.new(
object_class: "User",
original_attributes: [{ id: 1, name: "Mark" }, { id: 2, name: "Bob" }],
event: :create,
routing_key: :custom_key, # optional
headers: { type: "admin" }, # optional
).publish_now TableSync::Publishing::Batch.new(
object_class: "User",
original_attributes: [{ id: 1, name: "Mark" }, { id: 2, name: "Bob" }],
event: :create,
routing_key: :custom_key, # optional
headers: { type: "admin" }, # optional
).publish_later-
TableSync::Publishing::Raw- sends raw data without checks.
Be carefull with this publisher. There are no checks for the data sent. You can send anything.
-
object_class- model -
model_name- name of model, which will be insterted to the message payload. Ifnil,object_classwill be used instead -
original_attributes- raw data that will be sent -
event- type of event that happened to the published objects (create,update,destroy);updateby default -
routing_key- custom routing_key -
headers- custom headers
- takes in the
original_attributes,object_class,event,routing_key,headers - constantizes
object_class - constructs routing_key using
routing_key_callable(ignores#attributes_for_routing_key) or usesrouting_keyif given - constructs headers using
headers_callable(ignores#attributes_for_headers) or usesheadersif given - publishes Rabbit message (uses
original_attributesas is) - sends notification (if set up)
TableSync::Publishing::Raw.new(
object_class: "User",
original_attributes: [{ id: 1, name: "Mark" }, { id: 2, name: "Bob" }],
event: :create,
routing_key: :custom_key, # optional
headers: { type: "admin" }, # optional
).publish_now