aboutsummaryrefslogtreecommitdiff
path: root/lib/argument_deduplication/server.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-10-25 01:07:00 +0200
committerEugen Rochko <eugen@zeonfederated.com>2022-10-25 15:19:33 +0200
commit1bfbfb0317263be46869150f6673f014e8ef0ae8 (patch)
treea91cd724c7492ebee3b87860cda0b5a9f7a1de98 /lib/argument_deduplication/server.rb
parent30453fab80d55fc10766f0e067c31d96753ccfda (diff)
downloadmastodon-feature-argument-deduplication.tar
mastodon-feature-argument-deduplication.tar.gz
mastodon-feature-argument-deduplication.tar.bz2
mastodon-feature-argument-deduplication.zip
Add deduplication for JSON payloads in job queuefeature-argument-deduplication
Diffstat (limited to 'lib/argument_deduplication/server.rb')
-rw-r--r--lib/argument_deduplication/server.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/argument_deduplication/server.rb b/lib/argument_deduplication/server.rb
new file mode 100644
index 000000000..0da4d1c62
--- /dev/null
+++ b/lib/argument_deduplication/server.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+module ArgumentDeduplication
+ class Server
+ include Sidekiq::ServerMiddleware
+
+ def call(_worker, job, _queue)
+ argument = process_argument!(job)
+
+ yield
+
+ # If the job completes successfully, we can remove
+ # the argument from the store. If there is an exception,
+ # the job will be retried, so we can't remove the argument
+ # from the store yet. When retries are exhausted, or when
+ # retries are disabled for the worker, the configured death
+ # handler will remove it.
+
+ argument&.pop!
+ end
+
+ private
+
+ def process_argument!(job)
+ return unless job['deduplicate_arguments']
+
+ argument_index = job['deduplicate_arguments']
+ content_hash = job['args'][argument_index]
+ value = Sidekiq.redis { |redis| redis.get("#{PREFIX}:value:#{content_hash}") }
+
+ raise CorruptedArgumentError, "The argument for hash #{content_hash} could not be found" if value.nil?
+
+ job['args'][argument_index] = value
+
+ Argument.new(content_hash, value)
+ end
+ end
+end