Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple current attributes in middleware #5904

Merged
merged 1 commit into from May 8, 2023

Conversation

manu3569
Copy link
Contributor

@manu3569 manu3569 commented May 3, 2023

Currently, when calling Sidekiq::CurrentAttributes.persist(...) for multiple current attributes, the middleware doesn't correctly load the serialized data in the server middleware. In fact, it fails because the attributes from the first current attribute are used for "rehydrating" the second one.

The backwards compatible solution in this PR allows for a hash argument to be passed to the aforementioned method, with a key that is used in the jobs hash.

Sidekiq::CurrentAttributes.persist({
  cattr: "Myapp::Current",
  cattr_other: "Myapp::OtherCurrent"
})

@mperham
Copy link
Collaborator

mperham commented May 3, 2023

when calling Sidekiq::CurrentAttributes.persist(...) for multiple current attributes

But why do you have multiple?

@mperham
Copy link
Collaborator

mperham commented May 3, 2023

And your suggestion supports 2. What if I want 3?

@manu3569
Copy link
Contributor Author

manu3569 commented May 3, 2023

when calling Sidekiq::CurrentAttributes.persist(...) for multiple current attributes

But why do you have multiple?

In our case, we need to handle global state for two separate concerns, each with their own CurrentAttribute class.


And your suggestion supports 2. What if I want 3?

The proposed solution supports an arbitrary number of CurrentAttributes – one per key/value pair in the argument hash.

Sidekiq::CurrentAttributes.persist({
  cattr: "Myapp::Current",
  cattr_other: "Myapp::OtherCurrent",
  cattr_three: "Myapp::ThirdCurrent",
  cattr_fourth: "Myapp::FourthCurrent",
  ...
})

@mperham
Copy link
Collaborator

mperham commented May 4, 2023

I understand your usecase. I don't like making the user configure the attribute name. I'm wondering if it would be simpler to do something like:

Sidekiq::CurrentAttributes.persist("Myapp::Current", "Myapp::OtherCurrent")

The first would be stored in cattr so it is backwards compatible with jobs already persisted in Redis. The second and beyond would be stored at cattr_1, cattr_2, etc. Effectively zero-based array indexes. You could add a new CurrentAttribute instance to the list and existing jobs data will remain backwards compatible. You could not change the ordering of attributes without breaking existing data.

WDYT?

@manu3569
Copy link
Contributor Author

manu3569 commented May 4, 2023

You could not change the ordering of attributes without breaking existing data.

That was the main reason I proposed the user defined key, but maybe documenting the caveat about changing order is good enough.

Do you prefer the suggested variable length method signature over accepting an array in the first position? We currently have the signature below, and we would need to type check the last argument to see if it's a config of a class string.

def persist(klass, config = Sidekiq.default_configuration)

Let me know how to proceed, and I'll make the changes. Thanks!

@mperham
Copy link
Collaborator

mperham commented May 4, 2023

I think a single klass_or_array argument would be my initial thought but push back if there’s a good reason to.

@manu3569 manu3569 force-pushed the multiple-current-attributes branch 2 times, most recently from b432026 to 1f1ec73 Compare May 5, 2023 21:41
@manu3569 manu3569 force-pushed the multiple-current-attributes branch from 1f1ec73 to da4ccc2 Compare May 5, 2023 21:42
@manu3569
Copy link
Contributor Author

manu3569 commented May 8, 2023

Made the suggested updates. Please, let me know if it's acceptable for merging now or if further tweaks are needed. Thanks!

@mperham mperham merged commit a701b71 into sidekiq:main May 8, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants