Commit 389100ad authored by Oswaldo Ferreira's avatar Oswaldo Ferreira
Browse files

Overwrite caller_id at Sidekiq middleware

parent 0d95b370
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ module Labkit
      # be reinstantiated by Sidekiq-server when running the job.
      class Client
        def call(_worker_class, job, _queue, _redis_pool)
          Labkit::Context.with_context(caller_id: job["class"]) do |context|
          Labkit::Context.with_context do |context|
            job.merge!(context.to_h)

            yield
+3 −1
Original line number Diff line number Diff line
@@ -7,7 +7,9 @@ module Labkit
      # reinstantiate a context in which the job will run.
      class Server
        def call(_worker_class, job, _queue)
          Labkit::Context.with_context(job) do |_context|
          data = job.merge(Labkit::Context.log_key(:caller_id) => job["class"])

          Labkit::Context.with_context(data) do |_context|
            yield
          end
        end
+24 −5
Original line number Diff line number Diff line
@@ -27,18 +27,37 @@ describe Labkit::Middleware::Sidekiq::Server do
    end
  end

  it "sets the application context from job params and clears after running" do
    expected_metadata = { "meta.project" => "jane.doe/bookstore", "meta.user" => "jane.doe" }
    job = expected_metadata.merge("class" => TestWorker, "queue" => "default", "args" => ["do it"])
    fake_job = instance_double(TestWorker)
  context "application context" do
    let(:fake_job) do
      instance_double(TestWorker)
    end

    before do
      # Pushing the job when sidekiq is running inline sets the jid.
      allow(fake_job).to receive(:jid=)

      expect(TestWorker).to receive(:new).and_return(fake_job)
    end

    it "sets the application context from job params and clears after running" do
      expected_metadata = { "meta.project" => "jane.doe/bookstore", "meta.user" => "jane.doe" }
      job = expected_metadata.merge("class" => "TestWorker", "queue" => "default", "args" => ["do it"])

      expect(Labkit::Context).to receive(:with_context)
                                   .with(hash_including(job)).ordered.and_call_original
      expect(fake_job).to receive(:perform).with("do it").ordered

      Sidekiq::Client.push(job)
    end

    expect(Labkit::Context).to receive(:with_context).with(job).ordered.and_call_original
    it "merges the caller_id to the params and pass it to the application context" do
      job = { "class" => "TestWorker", "queue" => "default", "args" => ["do it"] }

      expect(Labkit::Context).to receive(:with_context)
                                   .with(hash_including(Labkit::Context.log_key(:caller_id) => "TestWorker")).ordered.and_call_original
      expect(fake_job).to receive(:perform).with("do it").ordered

      Sidekiq::Client.push(job)
    end
  end
end