Module: Cloudtasker::CLI

Defined in:
lib/cloudtasker/cli.rb

Overview

Cloudtasker executable logic

Class Method Summary collapse

Class Method Details

Return the server banner

Returns:

  • (String)

    The server banner



147
148
149
150
151
152
153
154
155
156
# File 'lib/cloudtasker/cli.rb', line 147

def banner
  <<~'TEXT'
       ___ _                 _ _            _
      / __\ | ___  _   _  __| | |_ __ _ ___| | _____ _ __
     / /  | |/ _ \| | | |/ _` | __/ _` / __| |/ / _ \ '__|
    / /___| | (_) | |_| | (_| | || (_| \__ \   <  __/ |
    \____/|_|\___/ \__,_|\__,_|\__\__,_|___/_|\_\___|_|

  TEXT
end

.boot_systemObject

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.expand_path('./config/environment.rb')
end

.environmentString

Return the current environment.

Returns:

  • (String)

    The environment name.



16
17
18
# File 'lib/cloudtasker/cli.rb', line 16

def environment
  Cloudtasker.config.environment
end

.handle_signal(sig) ⇒ Object

Handle process signals

Parameters:

  • sig (String)

    The signal.

Raises:

  • (Interrupt)


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.

Returns:

  • (Boolean)

    True if JRuby is loaded.



34
35
36
# File 'lib/cloudtasker/cli.rb', line 34

def jruby?
  defined?(::JRUBY_VERSION)
end

.local_serverCloudtasker::LocalServer

Return the local Cloudtasker server.

Returns:



52
53
54
# File 'lib/cloudtasker/cli.rb', line 52

def local_server
  @local_server ||= LocalServer.new
end

.loggerLogger

Return the Cloudtasker logger

Returns:

  • (Logger)

    The Cloudtasker logger.



43
44
45
# File 'lib/cloudtasker/cli.rb', line 43

def logger
  Cloudtasker.logger
end

.non_dev_warning_messageString

Return the message to display when users attempt to run the local development server in non-dev environments.

Returns:

  • (String)

    The warning message.



175
176
177
178
179
180
181
182
183
# File 'lib/cloudtasker/cli.rb', line 175

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

Print the server banner



188
189
190
191
192
# File 'lib/cloudtasker/cli.rb', line 188

def print_banner
  puts "\e[96m"
  puts banner
  puts "\e[0m"
end

Display a warning message when run in non-dev env.

Returns:

  • (<Type>)

    <description>



163
164
165
166
167
# File 'lib/cloudtasker/cli.rb', line 163

def print_non_dev_warning
  puts "\e[31m"
  puts non_dev_warning_message
  puts "\e[0m"
end

.rails_app?Boolean

Return true if we are running in Rails.

Returns:

  • (Boolean)

    True if rails is loaded.



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_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(read_pipe, opts = {}) ⇒ Object

Run server and wait for signals.

Parameters:

  • read_pipe (IO)

    Where to read signals.

  • opts (Hash) (defaults to: {})

    Server options.



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

Parameters:

  • write_pipe (IO)

    Where to write signals.



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