Module: Cloudtasker::CLI
- Defined in:
- lib/cloudtasker/cli.rb
Overview
Cloudtasker executable logic
Class Method Summary collapse
-
.banner ⇒ String
Return the server banner.
-
.boot_system ⇒ Object
Load Rails if defined.
-
.environment ⇒ String
Return the current environment.
-
.handle_signal(sig) ⇒ Object
Handle process signals.
-
.jruby? ⇒ Boolean
Return true if we are running in JRuby.
-
.local_server ⇒ Cloudtasker::LocalServer
Return the local Cloudtasker server.
-
.logger ⇒ Logger
Return the Cloudtasker logger.
-
.non_dev_warning_message ⇒ String
Return the message to display when users attempt to run the local development server in non-dev environments.
-
.print_banner ⇒ Object
Print the server banner.
-
.print_non_dev_warning ⇒ <Type>
Display a warning message when run in non-dev env.
-
.rails_app? ⇒ Boolean
Return true if we are running in Rails.
-
.run(opts = {}) ⇒ Object
Run the cloudtasker development server.
-
.run_server(read_pipe, opts = {}) ⇒ Object
Run server and wait for signals.
-
.setup_signals(write_pipe) ⇒ Object
Define which signals to trap.
Class Method Details
.banner ⇒ String
Return the server banner
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/cloudtasker/cli.rb', line 147 def <<~'TEXT' ___ _ _ _ _ / __\ | ___ _ _ __| | |_ __ _ ___| | _____ _ __ / / | |/ _ \| | | |/ _` | __/ _` / __| |/ / _ \ '__| / /___| | (_) | |_| | (_| | || (_| \__ \ < __/ | \____/|_|\___/ \__,_|\__,_|\__\__,_|___/_|\_\___|_| TEXT end |
.boot_system ⇒ Object
Load Rails if defined
59 60 61 62 63 64 65 66 67 68 69 |
# 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.('./config/environment.rb') end |
.environment ⇒ String
Return the current environment.
16 17 18 |
# File 'lib/cloudtasker/cli.rb', line 16 def environment Cloudtasker.config.environment end |
.handle_signal(sig) ⇒ Object
Handle process signals
138 139 140 |
# File 'lib/cloudtasker/cli.rb', line 138 def handle_signal(sig) raise(Interrupt) if %w[INT TERM].include?(sig) end |
.jruby? ⇒ Boolean
Return true if we are running in JRuby.
34 35 36 |
# File 'lib/cloudtasker/cli.rb', line 34 def jruby? defined?(::JRUBY_VERSION) end |
.local_server ⇒ Cloudtasker::LocalServer
Return the local Cloudtasker server.
52 53 54 |
# File 'lib/cloudtasker/cli.rb', line 52 def local_server @local_server ||= LocalServer.new end |
.logger ⇒ Logger
Return the Cloudtasker logger
43 44 45 |
# File 'lib/cloudtasker/cli.rb', line 43 def logger Cloudtasker.logger end |
.non_dev_warning_message ⇒ String
Return the message to display when users attempt to run the local development server in non-dev environments.
175 176 177 178 179 180 181 182 183 |
# File 'lib/cloudtasker/cli.rb', line 175 def <<~'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 |
.print_banner ⇒ Object
Print the server banner
188 189 190 191 192 |
# File 'lib/cloudtasker/cli.rb', line 188 def puts "\e[96m" puts puts "\e[0m" end |
.print_non_dev_warning ⇒ <Type>
Display a warning message when run in non-dev env.
163 164 165 166 167 |
# File 'lib/cloudtasker/cli.rb', line 163 def print_non_dev_warning puts "\e[31m" puts puts "\e[0m" end |
.rails_app? ⇒ Boolean
Return true if we are running in Rails.
25 26 27 |
# File 'lib/cloudtasker/cli.rb', line 25 def rails_app? defined?(::Rails) end |
.run(opts = {}) ⇒ Object
Run the cloudtasker development server.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cloudtasker/cli.rb', line 74 def run(opts = {}) boot_system # Print banner environment == 'development' ? : 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(read_pipe, opts = {}) ⇒ Object
Run server and wait for signals.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cloudtasker/cli.rb', line 103 def run_server(read_pipe, opts = {}) local_server.start(opts) while (readable_io = read_pipe.wait_readable) signal = readable_io.first[0].gets.strip handle_signal(signal) end rescue Interrupt logger.info 'Shutting down' local_server.stop logger.info 'Stopped' end |
.setup_signals(write_pipe) ⇒ Object
Define which signals to trap
121 122 123 124 125 126 127 128 129 130 131 |
# 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| trap(sig) { write_pipe.puts(sig) } rescue ArgumentError puts "Signal #{sig} not supported" end end |