-
Notifications
You must be signed in to change notification settings - Fork 329
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
Pass a String to ActiveJob (rather than a SafeBuffer) #624
Conversation
This provides compatibility with Sidekiq as an ActiveJob adapter (because [Sidekiq only allows native JSON types][1] to be passed as job arguments). All credit goes to [@jdelStrother] who [suggested this solution][2]. Fixes [hotwired#522] and [hotwired#535] [1]: https://github.com/sidekiq/sidekiq/wiki/Best-Practices#1-make-your-job-parameters-small-and-simple [2]: hotwired#535 (comment) [@jdelStrother]: https://github.com/jdelStrother [hotwired#522]: hotwired#522 [hotwired#535]: hotwired#535
By the way, @jdelStrother, I'd be happy to close this PR if you want to create another one to be the author of the commit. |
No worries, happy to leave you as the author |
I could really use this, are there plans for it to be merged? |
Needs an explanation otherwise its a bit of a mystery method. |
Would an explanatory comment suffice? def self.perform_later(stream, content:)
super(stream, content: serialize_for_compatibility(content))
end
# Sidekiq requires job arguments to be valid JSON types, such as String
def serialize_for_compatibility safe_buffer
safe_buffer.to_str
end |
Actually, there's something that rubs my a bit wrong about this overwrite. Feels like we should just ensure that we're calling this perform_later method with the right type. Rather than leave it this late to do the conversion. |
In that case, the correct patch for this issue would be in def broadcast_refresh_later_to(*streamables, request_id: Turbo.current_request_id, **opts)
refresh_debouncer_for(*streamables, request_id: request_id).debounce do
content = turbo_stream_refresh_tag(request_id: request_id, **opts)
Turbo::Streams::BroadcastStreamJob.perform_later stream_name_from(streamables), content: serialize_for_compatibility(content)
end
end
# Sidekiq requires job arguments to be valid JSON types, such as String
def serialize_for_compatibility safe_buffer
safe_buffer.to_str
end Or if we skip the descriptive method: def broadcast_refresh_later_to(*streamables, request_id: Turbo.current_request_id, **opts)
refresh_debouncer_for(*streamables, request_id: request_id).debounce do
Turbo::Streams::BroadcastStreamJob.perform_later stream_name_from(streamables), content: turbo_stream_refresh_tag(request_id: request_id, **opts).to_str
end
end |
That's just the refresh methods? Wouldn't this be an issue for all the _later methods? |
The OP issue does not appear to affect the other The underlying problem is that we're passing a rendered SafeBuffer to ActionJob, which is rejected by Sidekiq.
The other methods do their rendering inside the job, so the SafeBuffer never hits the Sidekiq serializer. (Sidebar: Technical detailsAt the moment there are seven
The difference between the methods can be demonstrated in the Rails console by running: user = User.first
user.broadcast_update_later
# => Enqueued Turbo::Streams::ActionBroadcastJob
user.broadcast_refresh_later
# => :Failed enqueuing Turbo::Streams::BroadcastStreamJob to Sidekiq(default): ArgumentError (Job arguments to Turbo::Streams::BroadcastStreamJob must be native JSON types, but "<turbo-stream action=\"refresh\"></turbo-stream>" is a ActiveSupport::SafeBuffer. Bottom line: |
Great. So let's just do a PR where we call to_str at the call site rather than overwrite the perform_later method. |
@michaelbaudino @jdelStrother would you like to author the new PR? |
No that's fine. Think you've done most of the work in this PR 🙂 |
This provides compatibility with Sidekiq as an ActiveJob adapter (because Sidekiq only allows native JSON types to be passed as job arguments).
All credit goes to @jdelStrother who suggested this solution.
Fixes #522, fixes #535