2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2019-04-01 18:20:41 +02:00
|
|
|
|
|
|
|
require "yaml"
|
2013-05-04 20:50:24 -07:00
|
|
|
|
2012-02-17 15:54:11 -08:00
|
|
|
module Sidekiq
|
|
|
|
module Extensions
|
2017-01-04 10:30:42 -08:00
|
|
|
SIZE_LIMIT = 8_192
|
|
|
|
|
2012-07-20 20:11:33 -07:00
|
|
|
class Proxy < BasicObject
|
2019-04-01 18:20:41 +02:00
|
|
|
def initialize(performable, target, options = {})
|
2012-02-17 15:54:11 -08:00
|
|
|
@performable = performable
|
|
|
|
@target = target
|
2012-10-17 10:17:19 -07:00
|
|
|
@opts = options
|
2012-02-17 15:54:11 -08:00
|
|
|
end
|
|
|
|
|
2022-01-18 16:55:31 +01:00
|
|
|
def method_missing(name, *args, **kwargs)
|
2012-02-17 15:54:11 -08:00
|
|
|
# Sidekiq has a limitation in that its message must be JSON.
|
|
|
|
# JSON can't round trip real Ruby objects so we use YAML to
|
|
|
|
# serialize the objects to a String. The YAML will be converted
|
|
|
|
# to JSON and then deserialized on the other side back into a
|
|
|
|
# Ruby object.
|
2022-01-18 16:55:31 +01:00
|
|
|
obj = [@target, name, args, kwargs]
|
2017-01-04 10:30:42 -08:00
|
|
|
marshalled = ::YAML.dump(obj)
|
|
|
|
if marshalled.size > SIZE_LIMIT
|
|
|
|
::Sidekiq.logger.warn { "#{@target}.#{name} job argument is #{marshalled.bytesize} bytes, you should refactor it to reduce the size" }
|
|
|
|
end
|
2021-05-24 12:29:45 -07:00
|
|
|
@performable.client_push({"class" => @performable,
|
|
|
|
"args" => [marshalled],
|
|
|
|
"display_class" => "#{@target}.#{name}"}.merge(@opts))
|
2012-02-17 15:54:11 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|