-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!/usr/bin/env -S ruby --disable-gems | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require 'logger' | ||
require 'redis' | ||
|
||
module CI | ||
module Queue | ||
module Redis | ||
class Monitor | ||
|
||
def initialize(pipe, logger, redis_url, zset_key, processed_key) | ||
@zset_key = zset_key | ||
@processed_key = processed_key | ||
@logger = logger | ||
@redis = ::Redis.new(url: redis_url) | ||
@shutdown = false | ||
@pipe = pipe | ||
@self_pipe_reader, @self_pipe_writer = IO.pipe | ||
@self_pipe_writer.sync = true | ||
@queue = [] | ||
@deadlines = {} | ||
%i[TERM INT USR1].each do |sig| | ||
Signal.trap(sig) { soft_signal(sig) } | ||
end | ||
end | ||
|
||
def soft_signal(sig) | ||
@queue << sig | ||
@self_pipe_writer << '.' | ||
end | ||
|
||
HEARTBEAT_SCRIPT = <<~LUA | ||
local zset_key = KEYS[1] | ||
local processed_key = KEYS[2] | ||
local current_time = ARGV[1] | ||
local test = ARGV[2] | ||
if redis.call('sismember', processed_key, test) ~= 1 then | ||
redis.call('zadd', zset_key, current_time, test) | ||
end | ||
LUA | ||
def process_tick!(id:) | ||
@script ||= @redis.script(:load, HEARTBEAT_SCRIPT) | ||
@redis.evalsha(@script, keys: [@zset_key, @processed_key], argv: [Time.now.to_f, id]) | ||
end | ||
|
||
HEADER = 'L' | ||
HEADER_SIZE = [0].pack(HEADER).bytesize | ||
def read_message(io) | ||
case header = io.read_nonblock(HEADER_SIZE, exception: false) | ||
when :wait_readable | ||
nil | ||
when nil | ||
@logger.debug('Broken pipe, exiting') | ||
@shutdown = 0 | ||
false | ||
else | ||
Marshal.load(io.read(header.unpack1(HEADER))) | ||
end | ||
end | ||
|
||
def process_messages(io) | ||
while (message = read_message(io)) | ||
type, kwargs = message | ||
public_send("process_#{type}", **kwargs) | ||
end | ||
end | ||
|
||
def wait_for_events(ios) | ||
return if @shutdown | ||
|
||
return unless (ready = IO.select(ios, nil, nil, 10)) | ||
|
||
ready[0].each do |io| | ||
case io | ||
when @self_pipe_reader | ||
io.read_nonblock(512, exception: false) # Just flush the pipe, the information is in the @queue | ||
when @pipe | ||
process_messages(@pipe) | ||
else | ||
raise "Unknown reader: #{io.inspect}" | ||
end | ||
end | ||
end | ||
|
||
def monitor | ||
ios = [@self_pipe_reader, @pipe] | ||
|
||
until @shutdown | ||
while (sig = @queue.shift) | ||
case sig | ||
when :INT, :TERM | ||
@logger.debug("Received #{sig}, exiting") | ||
@shutdown = 0 | ||
break | ||
else | ||
raise "Unknown signal: #{sig.inspect}" | ||
end | ||
end | ||
|
||
wait_for_events(ios) | ||
end | ||
|
||
@logger.debug('Done') | ||
@shutdown | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
logger = Logger.new($stderr) | ||
if ARGV.include?('-v') | ||
logger.level = Logger::DEBUG | ||
else | ||
logger.level = Logger::INFO | ||
logger.formatter = ->(_severity, _timestamp, _progname, msg) { "#{msg}\n" } | ||
end | ||
|
||
redis_url = ARGV[0] | ||
zset_key = ARGV[1] | ||
processed_key = ARGV[2] | ||
manager = CI::Queue::Redis::Monitor.new($stdin, logger, redis_url, zset_key, processed_key) | ||
$stdout << "." | ||
$stdout.close | ||
exit(manager.monitor) |
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
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
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
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