From df2ab3bea2cf28e3422ccc5a38ce66ce5b5f3739 Mon Sep 17 00:00:00 2001 From: Salajan Silviu Date: Fri, 1 Jul 2022 13:04:35 +0300 Subject: [PATCH 1/3] notification should be string when enqueue Fix this warning: ArgumentError: Job arguments to Devise::Async::Backend::Sidekiq must be native JSON types. https://github.com/mperham/sidekiq/wiki/Best-Practices --- lib/devise/async/model.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/devise/async/model.rb b/lib/devise/async/model.rb index 11b4f63..c81eba7 100644 --- a/lib/devise/async/model.rb +++ b/lib/devise/async/model.rb @@ -40,7 +40,7 @@ def send_devise_notification(notification, *args) # If the record isn't dirty (aka has already been saved) enqueue right away # because the callback has already been triggered. else - Devise::Async::Worker.enqueue(notification, self.class.name, self.id.to_s, *args) + Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end end @@ -49,7 +49,7 @@ def send_devise_pending_notifications devise_pending_notifications.each do |notification, args| # Use `id.to_s` to avoid problems with mongoid 2.4.X ids being serialized # wrong with YAJL. - Devise::Async::Worker.enqueue(notification, self.class.name, self.id.to_s, *args) + Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end @devise_pending_notifications = [] end From 6d0abd7bfb659d8632928da61eaac03c614c88e4 Mon Sep 17 00:00:00 2001 From: Salajan Silviu Date: Tue, 5 Jul 2022 14:52:55 +0300 Subject: [PATCH 2/3] stringify locale arguments on send devise notification stringify_locale_args(args) implemented in lib/devise/async/model.rb --- lib/devise/async/model.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/devise/async/model.rb b/lib/devise/async/model.rb index c81eba7..672e1f1 100644 --- a/lib/devise/async/model.rb +++ b/lib/devise/async/model.rb @@ -40,6 +40,7 @@ def send_devise_notification(notification, *args) # If the record isn't dirty (aka has already been saved) enqueue right away # because the callback has already been triggered. else + args = stringify_locale_args(args) Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end end @@ -49,6 +50,7 @@ def send_devise_pending_notifications devise_pending_notifications.each do |notification, args| # Use `id.to_s` to avoid problems with mongoid 2.4.X ids being serialized # wrong with YAJL. + args = stringify_locale_args(args) Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end @devise_pending_notifications = [] @@ -73,7 +75,17 @@ def add_current_locale_to_args(args) opts['locale'] = I18n.locale args.push(opts) end + + def stringify_locale_args(args) + args.each_with_object([]) do |a, acc| + arg = is_arg_locale?(a) ? a.deep_merge(a) {|_,_,v| v.to_s} : a + acc << arg + end + end + def is_arg_locale?(arg) + arg.present? && arg.is_a?(Hash) && arg['locale'].present? + end end end end From 3a242c9a3450a057367c48f06d797c9f576e9f8e Mon Sep 17 00:00:00 2001 From: Salajan Silviu Date: Wed, 6 Jul 2022 16:57:39 +0300 Subject: [PATCH 3/3] some refactorings on /lib/devise/async/model.rb stringify_args_values it's a more general solution than stringify only locales. --- lib/devise/async/model.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/devise/async/model.rb b/lib/devise/async/model.rb index 672e1f1..272328b 100644 --- a/lib/devise/async/model.rb +++ b/lib/devise/async/model.rb @@ -2,6 +2,8 @@ module Devise module Models module Async extend ActiveSupport::Concern + + STRINGIFYABLE_TYPES = [Symbol] included do # Register hook to send all devise pending notifications. @@ -40,7 +42,7 @@ def send_devise_notification(notification, *args) # If the record isn't dirty (aka has already been saved) enqueue right away # because the callback has already been triggered. else - args = stringify_locale_args(args) + args = stringify_args_values(args) Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end end @@ -50,7 +52,7 @@ def send_devise_pending_notifications devise_pending_notifications.each do |notification, args| # Use `id.to_s` to avoid problems with mongoid 2.4.X ids being serialized # wrong with YAJL. - args = stringify_locale_args(args) + args = stringify_args_values(args) Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end @devise_pending_notifications = [] @@ -76,15 +78,20 @@ def add_current_locale_to_args(args) args.push(opts) end - def stringify_locale_args(args) + def stringify_args_values(args) args.each_with_object([]) do |a, acc| - arg = is_arg_locale?(a) ? a.deep_merge(a) {|_,_,v| v.to_s} : a + arg = is_arg_hash?(a) ? a.deep_merge(a) {|_,_,v| is_val_stringifyable?(v) ? v.to_s : v} : a acc << arg end end - def is_arg_locale?(arg) - arg.present? && arg.is_a?(Hash) && arg['locale'].present? + def is_arg_hash?(arg) + arg.present? && arg.is_a?(Hash) + end + + def is_val_stringifyable?(val) + # val is stringifyable if is one of the STRINGIFYABLE_TYPES + val.present? && STRINGIFYABLE_TYPES.include?(val.class) end end end