Last active
July 7, 2022 22:46
-
-
Save st0012/02368276bd35519a5a2acd619031a454 to your computer and use it in GitHub Desktop.
Monitor a queue's message inflow/outflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'io/console/size' | |
# Usage: Replace a Queue.new with DebugQueue.new(Queue.new) | |
class DebugQueue < SimpleDelegator | |
def <<(*msg, trace: caller_locations(3, 3)) | |
debug_print("<<", msg, trace) | |
super(*msg) | |
end | |
def pop(trace: caller_locations(3, 3)) | |
super().tap do |msg| | |
debug_print(">>", msg, trace) | |
end | |
end | |
private | |
def debug_print(direction, msg, trace) | |
msg = msg.to_s | |
msg = | |
if msg.size > msg_width | |
msg[0..msg_width] + "..." | |
else | |
msg | |
end | |
$stdout.puts( | |
<<~MSG | |
#{direction} | #{msg} | |
trace: #{trace.join(",\n" + " " * 9)} | |
MSG | |
) | |
end | |
def msg_width | |
# prevent msg from overflow to another line | |
@msg_width ||= (IO.console_size[1] - 10) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps it could also use a
name
attribute when there're multiple queues?