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
37
38
39
40
41
42
43
# 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(on: [:create, :update]) do |object|
    producer.queue(object) if object.saved_changes.present?
  end

  after_commit(on: :destroy) do |object|
    producer.queue(object)
  end
end

#redstream_nameObject



45
46
47
# File 'lib/redstream/model.rb', line 45

def redstream_name
  name.pluralize.underscore
end