Module: Redstream::Model::ClassMethods

Defined in:
lib/redstream/model.rb

Instance Method Summary collapse

Instance Method Details

#redstream_callbacks(producer: Producer.new) ⇒ Object

Adds after_save, after_touch, after_destroy and, most importantly, after_commit callbacks. after_save, after_touch and after_destroy write a delay message to a delay stream. The delay messages are exactly like other messages, but will be read and replayed by a Redstream::Delayer only after a certain amount of time has passed (5 minutes usually) to fix potential inconsistencies which result from network or other issues in between database commit and the rails after_commit callback.

Parameters:

  • producer (Redstream::Producer) (defaults to: Producer.new)

    A Redstream::Producer that is responsible for writing to a redis stream



31
32
33
34
35
36
# File 'lib/redstream/model.rb', line 31

def redstream_callbacks(producer: Producer.new)
  after_save { |object| producer.delay(object) if object.saved_changes.present? }
  after_touch { |object| producer.delay(object) }
  after_destroy { |object| producer.delay(object) }
  after_commit { |object| producer.queue(object) if object.saved_changes.present? }
end

#redstream_nameObject



38
39
40
# File 'lib/redstream/model.rb', line 38

def redstream_name
  name.pluralize.underscore
end