Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Last active June 10, 2024 13:07
Show Gist options
  • Save rmosolgo/e8d196b77e4a212e28bae5442f7357d3 to your computer and use it in GitHub Desktop.
Save rmosolgo/e8d196b77e4a212e28bae5442f7357d3 to your computer and use it in GitHub Desktop.
# This module will add a method
# to clean up empty topics:
module RemoveEmptyTopicsStorageExtension
def remove_empty_topics
all_topics, _topics_count = topics(limit: nil, offset: nil)
topics_to_remove = all_topics.select { |t| t.subscriptions_count == 0 }
if topics_to_remove.any?
topic_names = topics_to_remove.map(&:name)
fingerprint_key = topic_names.map { |t| fingerprints_key(t) }
with_redis do |r|
r.del(fingerprint_key)
r.srem(all_topics_key, topic_names)
end
end
end
end
# Add the method to GraphQL-Pro's storage class:
GraphQL::Pro::Subscriptions::RedisStorage.include(RemoveEmptyTopicsStorageExtension)
# Call the method on your schema's subscriptions system:
MySchema.subscriptions.instance_variable_get(:@storage).remove_empty_topics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment