module Cloudtasker::CLI
Cloudtasker
executable logic
Public Instance Methods
Load Rails if defined
# File lib/cloudtasker/cli.rb, line 59 def boot_system # Sync logs STDOUT.sync = true # Check for Rails return false unless File.exist?('./config/environment.rb') require 'rails' require 'cloudtasker/engine' require File.expand_path('./config/environment.rb') end
Return the current environment.
@return [String] The environment name.
# File lib/cloudtasker/cli.rb, line 16 def environment Cloudtasker.config.environment end
Handle process signals
@param [String] sig The signal.
# File lib/cloudtasker/cli.rb, line 140 def handle_signal(sig) raise(Interrupt) if %w[INT TERM].include?(sig) end
Return true if we are running in JRuby.
@return [Boolean] True if JRuby is loaded.
# File lib/cloudtasker/cli.rb, line 34 def jruby? defined?(::JRUBY_VERSION) end
Return the local Cloudtasker
server.
@return [Cloudtasker::LocalServer] The local Cloudtasker
server.
# File lib/cloudtasker/cli.rb, line 52 def local_server @local_server ||= LocalServer.new end
Return the Cloudtasker
logger
@return [Logger] The Cloudtasker
logger.
# File lib/cloudtasker/cli.rb, line 43 def logger Cloudtasker.logger end
Return the message to display when users attempt to run the local development server in non-dev environments.
@return [String] The warning message.
# File lib/cloudtasker/cli.rb, line 177 def non_dev_warning_message <<~'TEXT' ============================================ /!\ ==================================================== Your are running the Cloudtasker local development server in a NON-DEVELOPMENT environment. This is not recommended as the the development server is not designed for production-like load. If you need a job processing server to run yourself please use Sidekiq instead (https://sidekiq.org) ============================================ /!\ ==================================================== TEXT end
Display a warning message when run in non-dev env.
@return [<Type>] <description>
# File lib/cloudtasker/cli.rb, line 165 def print_non_dev_warning puts "\e[31m" puts non_dev_warning_message puts "\e[0m" end
Return true if we are running in Rails.
@return [Boolean] True if rails is loaded.
# File lib/cloudtasker/cli.rb, line 25 def rails_app? defined?(::Rails) end
Run the cloudtasker development server.
# File lib/cloudtasker/cli.rb, line 74 def run(opts = {}) boot_system # Print banner environment == 'development' ? print_banner : print_non_dev_warning # Print rails info if rails_app? logger.info "[Cloudtasker/Server] Booted Rails #{::Rails.version} application in #{environment} environment" end # Get internal read/write pip self_read, self_write = IO.pipe # Setup signals to trap setup_signals(self_write) logger.info "[Cloudtasker/Server] Running in #{RUBY_DESCRIPTION}" # Wait for signals run_server(self_read, opts) end
Run server and wait for signals.
@param [IO] read_pipe Where to read signals. @param [Hash] opts Server options.
# File lib/cloudtasker/cli.rb, line 103 def run_server(read_pipe, opts = {}) local_server.start(opts) while (readable_io = IO.select([read_pipe])) signal = readable_io.first[0].gets.strip handle_signal(signal) end rescue Interrupt logger.info 'Shutting down' local_server.stop logger.info 'Stopped' end
Define which signals to trap
@param [IO] write_pipe Where to write signals.
# File lib/cloudtasker/cli.rb, line 121 def setup_signals(write_pipe) # Display signals on log output sigs = %w[INT TERM TTIN TSTP] # USR1 and USR2 don't work on the JVM sigs << 'USR2' unless jruby? sigs.each do |sig| begin trap(sig) { write_pipe.puts(sig) } rescue ArgumentError puts "Signal #{sig} not supported" end end end