lib/pushpop/cli.rb in pushpop-0.2 vs lib/pushpop/cli.rb in pushpop-0.3.1

- old
+ new

@@ -1,8 +1,9 @@ require 'thor' require 'dotenv' require 'pushpop' +require 'thwait' module Pushpop class CLI < Thor def self.file_options @@ -22,11 +23,11 @@ map 'jobs:describe' => 'describe_jobs' file_options def describe_jobs Dotenv.load - require_file(options[:file]) + Pushpop.require_file(options[:file]) Pushpop.jobs.tap do |jobs| jobs.each do |job| puts job.name end end @@ -36,42 +37,45 @@ map 'jobs:run_once' => 'run_jobs_once' file_options def run_jobs_once Dotenv.load - require_file(options[:file]) + Pushpop.require_file(options[:file]) Pushpop.run end desc 'jobs:run', 'Run jobs ongoing' map 'jobs:run' => 'run_jobs' file_options def run_jobs Dotenv.load - require_file(options[:file]) + Pushpop.require_file(options[:file]) Pushpop.schedule - Clockwork.manager.run - end - private + threads = [] + threads << Pushpop.start_clock - def require_file(file) - if file - if File.directory?(file) - Dir.glob("#{file}/**/*.rb").each { |file| - load "#{Dir.pwd}/#{file}" - } - else - load file + Pushpop.web.app.traps = false + web_thread = Pushpop.start_webserver + threads << web_thread if web_thread + + # Listen to exit signals, so the CLI doesn't hang infinitely on clock + [:INT, :TERM].each do |signal| + trap(signal) do + threads.each do |thread| + thread.exit + end end - else - Dir.glob("#{Dir.pwd}/jobs/**/*.rb").each { |file| - load file - } end - end + # Wait for both the clock thread and the sinatra thread to close before exiting + ThreadsWait.all_waits(threads) do + threads.each do |thread| + thread.exit + end + end + end end end