Add class Diversipub::PumaTask

This commit is contained in:
Alex Kotov 2023-05-03 16:51:31 +04:00
parent 6d26cc6e39
commit 89759226d7
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 81 additions and 47 deletions

View file

@ -21,6 +21,7 @@ require 'sinatra/base'
################
require_relative 'diversipub/main'
require_relative 'diversipub/puma_task'
require_relative 'diversipub/rack_app'
require_relative 'diversipub/sinatra_app'
require_relative 'diversipub/version'

View file

@ -33,16 +33,19 @@ module Diversipub
p stdout
p stderr
puma_thread.join
puma_task.join_thread
true
end
def stop_gracefully
puma.stop
puma_task.stop_gracefully
nil
end
alias stop_urgently stop_gracefully
def stop_urgently
puma_task.stop_urgently
nil
end
private
@ -66,50 +69,8 @@ module Diversipub
end.freeze
end
def puma_thread
@puma_thread ||= Thread.start { puma.run }
end
def puma
@puma ||= Puma::Launcher.new(
puma_config,
log_writer: puma_log_writer,
)
end
def puma_config
@puma_config ||= Puma::Configuration.new(
{
app: rack_app,
binds: ['tcp://127.0.0.1:9292'].freeze,
environment: 'production',
log_requests: true,
logger: puma_logger,
rackup: nil,
tag: 'diversipub',
tcp_host: '127.0.0.1',
tcp_port: 9292,
workers: 0,
},
)
end
def puma_log_writer
@puma_log_writer ||= Puma::LogWriter.new stderr, stderr
end
def puma_logger
@puma_logger ||= Logger.new(stderr).tap do |puma_logger|
puma_logger.level = Logger::DEBUG
end
end
def rack_app
@rack_app ||= RackApp.new sinatra_app
end
def sinatra_app
@sinatra_app ||= SinatraApp.new self
def puma_task
@puma_task ||= PumaTask.new self
end
end
end

View file

@ -0,0 +1,72 @@
# frozen_string_literal: true
module Diversipub
##
# Puma web server task runner.
#
class PumaTask
def initialize(main)
@main = main
end
def join_thread
thread.join
nil
end
def stop_gracefully
puma.stop
nil
end
alias stop_urgently stop_gracefully
private
def thread
@thread ||= Thread.start { puma.run }
end
def puma
@puma ||= Puma::Launcher.new(
puma_config,
log_writer: puma_log_writer,
)
end
def puma_config
@puma_config ||= Puma::Configuration.new(
{
app: rack_app,
binds: ['tcp://127.0.0.1:9292'].freeze,
environment: 'production',
log_requests: true,
logger: puma_logger,
rackup: nil,
tag: 'diversipub',
tcp_host: '127.0.0.1',
tcp_port: 9292,
workers: 0,
},
)
end
def puma_log_writer
@puma_log_writer ||= Puma::LogWriter.new @main.stderr, @main.stderr
end
def puma_logger
@puma_logger ||= Logger.new(@main.stderr).tap do |puma_logger|
puma_logger.level = Logger::DEBUG
end
end
def rack_app
@rack_app ||= RackApp.new sinatra_app
end
def sinatra_app
@sinatra_app ||= SinatraApp.new @main
end
end
end