lib/zold/node/async_entrance.rb in zold-0.14.12 vs lib/zold/node/async_entrance.rb in zold-0.14.13
- old
+ new
@@ -43,20 +43,31 @@
raise 'Log can\'t be nil' if log.nil?
@log = log
@mutex = Mutex.new
end
+ def to_json
+ json = {
+ 'queue': queue.count,
+ 'pool.length': @pool.length,
+ 'pool.running': @pool.running?
+ }
+ opts = queue
+ json['queue_age'] = opts.empty? ? 0 : Time.now - File.mtime(File.join(@dir, opts[0]))
+ @entrance.to_json.merge(json)
+ end
+
def start
@entrance.start do
FileUtils.mkdir_p(@dir)
@pool = Concurrent::FixedThreadPool.new(
AsyncEntrance::THREADS, max_queue: AsyncEntrance::THREADS, fallback_policy: :abort
)
AsyncEntrance::THREADS.times do
@pool.post do
- VerboseThread.new(@log).run(true) do
- loop do
+ loop do
+ VerboseThread.new(@log).run(true) do
take
break if @pool.shuttingdown?
sleep Random.rand(100) / 100
end
end
@@ -73,17 +84,9 @@
@pool.kill
@log.info('Async entrance was killed')
end
end
end
- end
-
- def to_json
- @entrance.to_json.merge(
- 'queue': queue.count,
- 'pool.length': @pool.length,
- 'pool.running': @pool.running?
- )
end
def push(id, body)
@mutex.synchronize do
AtomicFile.new(File.join(@dir, id.to_s)).write(body)