Sha256: 3b7b1182cfc2d5faf3616283dd0033bab85e8ae757059a77440374329a19365f

Contents?: true

Size: 1.84 KB

Versions: 142

Compression:

Stored size: 1.84 KB

Contents

# #
# AUTO GENERATED LIVE EVENT
# This was auto generated by the CanvasSync Gem.
# You can customize it as needed, but make sure you test
# any changes you make to the auto generated methods.
#
# LiveEvent message formats can be found at https://canvas.instructure.com/doc/api/file.live_events.html
# In the general case, LiveEvent message content should not be trusted - it is highly possible that events may
# arrive out of order. Most of the CanvasSync LiveEvent templates solve this by always fetching the object from the API.
#


module LiveEvents
  class RetryLiveEventJob < StandardError; end

  class BaseEvent < CanvasSync::Job
    attr_accessor :raw_payload
    attr_accessor :payload
    attr_accessor :metadata

    def perform(event_payload)
      @raw_payload = event_payload
      @metadata = HashWithIndifferentAccess.new(event_payload["attributes"])
      @payload = HashWithIndifferentAccess.new(event_payload["body"])
      process_with_retry
    end

    def process
      raise "process must be implemented in your subclass"
    end

    # Live events will use a canvas global ID (cross shard) for any ID's provided. This method will return the local ID.
    def local_canvas_id(id)
      id.to_i % 10_000_000_000_000
    end

    private

    # Sometimes a creation and update event get sent by Canvas at almost the exact same time. This means
    # that there is sometimes a race condition that causes them to both try to save a new record at the same time.
    # If that happens, just wait a few seconds and try again -- the second try should be able to see that
    # the save already occured, and it will just update the row..
    def process_with_retry
      tries ||= 5
      process
    rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid, RetryLiveEventJob => e
      raise e if (tries -= 1).zero?
      sleep 5
      retry
    end
  end
end

Version data entries

142 entries across 142 versions & 1 rubygems

Version Path
canvas_sync-0.17.23.beta1 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.20 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.19 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.18 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.17.beta1 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.16 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.15 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.14 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.13 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.12 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.10.beta2 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.10.beta1 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.8 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.8.beta4 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.8.beta3 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.8.beta2 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.8.beta1 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.7 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.7.beta1 spec/dummy/app/services/live_events/base_event.rb
canvas_sync-0.17.6 spec/dummy/app/services/live_events/base_event.rb