Skip to content

Instantly share code, notes, and snippets.

@alassek
Created October 24, 2021 00:37
Show Gist options
  • Select an option

  • Save alassek/4e80c33faa71cd8f3ab19e03362db752 to your computer and use it in GitHub Desktop.

Select an option

Save alassek/4e80c33faa71cd8f3ab19e03362db752 to your computer and use it in GitHub Desktop.

Revisions

  1. alassek created this gist Oct 24, 2021.
    44 changes: 44 additions & 0 deletions sidekiq_globalid.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    require "global_id"

    module Sidekiq
    module GlobalIDMiddleware
    class Processor
    def call(_worker_class, job, *)
    job["args"].map!(&method(:process))
    yield
    end

    def process(argument)
    argument
    end
    end

    class Deserializer < Processor
    def process(argument)
    if argument.to_s.start_with?("gid://")
    GlobalID::Locator.locate(argument)
    else
    argument
    end
    end
    end

    class Serializer < Processor
    def process(argument)
    if argument.respond_to?(:to_global_id)
    argument.to_global_id.to_s
    else
    argument
    end
    end
    end
    end

    server_middleware do |chain|
    chain.prepend GlobalIDMiddleware::Deserializer
    end

    client_middleware do |chain|
    chain.prepend GlobalIDMiddleware::Serializer
    end
    end