13
loading...
This website collects cookies to deliver better user experience
redis-cli set leastbad rules
on your terminal and pick it up in your app.worker
in Procfile
... sort of like Sidekiq.What follows is the MVP of my new gem. In fact, it's not a gem, yet: it's an initializer! It has no tests and is hours old. My janky code would make poor Mike bleed out. The goal is to see if folks actually need/want this to exist. I'm looking for feedback on what the ideal Rails-side API would actually look like.
config/initializers/sidekiq.rb
:module Sidekiq
class Subscriber
include ::Sidekiq::Util
def initialize
@done = false
@thread = nil
end
def start
@thread ||= safe_thread("subscriber") {
until @done
Sidekiq.redis do |conn|
# https://redis.io/topics/notifications#configuration
conn.config(:set, "notify-keyspace-events", "E$lshz")
# https://redis.io/topics/notifications#events-generated-by-different-commands
conn.psubscribe("__key*__:*") do |on|
on.psubscribe do
@firehose = Firehose.new
end
on.pmessage do |pattern, command, key|
@firehose.process(command.split(":").last.to_sym, key)
end
on.punsubscribe do
@firehose = nil
end
end
end
end
Sidekiq.logger.info("Subscriber exiting...")
}
end
def terminate
@done = true
if @thread
t = @thread
Thread.kill(@thread)
@thread = nil
t.value
end
end
end
end
module CoreExtensions
module Sidekiq
module Launcher
attr_accessor :subscriber
def initialize(options)
@subscriber = ::Sidekiq::Subscriber.new
super(options)
end
def run
super
subscriber.start
end
def quiet
subscriber.terminate
super
end
def stop
subscriber.terminate
super
end
end
end
end
Sidekiq.configure_server do
require "sidekiq/launcher"
::Sidekiq::Launcher.prepend(CoreExtensions::Sidekiq::Launcher)
end
SET
command. I have a simple AllUsers
ActionCable Channel in play for testing. This lives in app/lib/firehose.rb
:class Firehose
include CableReady::Broadcaster
attr_reader :redis
def initialize
@redis = ::ActionCable.server.pubsub.redis_connection_for_subscriptions
end
def process(command, key)
case command # https://github.com/rails/kredis#examples
when :set # string, integer, json
cable_ready["all_users"].console_log(message: "#{key} was just updated to #{redis.get(key)}").broadcast
when :rpush # list
when :lrem # unique_list
when :sadd # set
when :incr
when :decr
when :incrby
when :decrby
when :exists
when :del
cable_ready["all_users"].console_log(message: "#{key} was deleted").broadcast
else
end
end
end