Fix CLI tool not exiting

This commit is contained in:
Alex Ling 2020-08-05 09:48:31 +00:00
parent 99a77966ad
commit c07f421322
2 changed files with 19 additions and 13 deletions

View File

@ -6,6 +6,7 @@
class MainFiber class MainFiber
@@channel = Channel(-> Nil).new @@channel = Channel(-> Nil).new
@@done = Channel(Bool).new @@done = Channel(Bool).new
@@main_fiber = Fiber.current
def self.start_and_block def self.start_and_block
loop do loop do
@ -21,9 +22,13 @@ class MainFiber
end end
def self.run(&block : -> Nil) def self.run(&block : -> Nil)
@@channel.send block if @@main_fiber == Fiber.current
until @@done.receive block.call
Fiber.yield else
@@channel.send block
until @@done.receive
Fiber.yield
end
end end
end end
end end

View File

@ -45,17 +45,22 @@ class CLI < Clim
version "Version #{MANGO_VERSION}", short: "-v" version "Version #{MANGO_VERSION}", short: "-v"
common_option common_option
run do |opts| run do |opts|
Config.load(opts.config).set_current
MangaDex::Downloader.default
Plugin::Downloader.default
puts BANNER puts BANNER
puts DESCRIPTION puts DESCRIPTION
puts puts
# empty ARGV so it won't be passed to Kemal # empty ARGV so it won't be passed to Kemal
ARGV.clear ARGV.clear
Server.new.start
Config.load(opts.config).set_current
MangaDex::Downloader.default
Plugin::Downloader.default
spawn do
Server.new.start
end
MainFiber.start_and_block
end end
sub "admin" do sub "admin" do
@ -123,8 +128,4 @@ class CLI < Clim
end end
end end
spawn do CLI.start(ARGV)
CLI.start(ARGV)
end
MainFiber.start_and_block