Skip to content

ActiveSupport::CurrentAttributes gets reset in console #415

@LukeClancy

Description

@LukeClancy

This can be against expectations but is not necessarily a bug

What is CurrentAttributes:

CurrentAttributes is a class that is often used to keep track of data over a request's lifecycle. This often includes the signed in user. Its treated kind-of like a global variable

Problem Statement:

I have been using CurrentAttributes to pretend to be users during tests over the console. Recently, I noticed that TestJob.enqueue seems to reset CurrentAttributes, wiping that data. This does not happen during actual requests which go through the controller.

  1. CurrentAttributes is made for requests, so this could be seen as out of scope. In which case one could do this instead when using the console:
Class C
    class << self
        attr_accessor :test
    end
end
  1. Alternatively, regular sql requests do not reset CurrentAttributes, so it would be expected that neither would que. So it would be more user friendly to fix it.

Example of issue

class C < ActiveSupport::CurrentAttributes
	attribute :test
end
class ApplicationJob < Que::Job; end
class TestJob < ApplicationJob
	def run
		100.times{ Rails.logger.info "job" }
	end
end
def i_test_que
	C.test = 5
	Rails.logger.info C.test.inspect
	u = User.first
	Rails.logger.info C.test.inspect
	TestJob.enqueue
	Rails.logger.info C.test.inspect
end

#output when running i_test_que in console: 5, 5, nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions