activitypub-academy/app/lib/activity_logger.rb

44 lines
806 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class ActivityLogger
2023-01-13 03:48:04 +09:00
@@loggers = Hash.new { |hash, key| hash[key] = [] }
def self.register(id, sse)
2023-01-13 03:48:04 +09:00
@@loggers[id] << sse
end
2023-01-13 03:48:04 +09:00
def self.unregister(id, sse)
@@loggers[id].delete(sse)
end
def self.log(id, event)
2023-01-15 01:12:20 +09:00
@@loggers[id].each do |logger|
logger.write event
rescue
puts 'rescued'
2023-01-30 04:12:00 +09:00
logger.close
ActivityLogger.unregister(id, logger)
2023-01-30 04:12:00 +09:00
puts 'closed logger'
2023-01-15 01:12:20 +09:00
end
2023-01-13 03:48:04 +09:00
end
def self.reset
@@loggers.clear
end
def self.start_keep_alive_thread
Thread.new {
while true
event = ActivityLogEvent.new('keep-alive', nil, nil, nil)
puts "writing keep-alive"
@@loggers.each_key do |key|
ActivityLogger.log(key, event)
end
sleep 10
end
}
end
end