Class: Cosmo::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmo/cli.rb,
sig/cosmo/cli.rbs

Overview

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/BlockLength

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

rubocop:disable Layout/TrailingWhitespace,Lint/IneffectiveAccessModifier

Returns:

  • (::String)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cosmo/cli.rb', line 212

def self.banner
  <<-TEXT
                .#%+:                                                  
                 ==-.                                       +.         
                   +:  .::::.                              :*-         
                 .=%%%%%%%%%%%%%#-                                     
              .#%%%%%%%%##*+===+*#%%:                                  
            :##%%%%#:  :-::...::::. -%.                                
           +%%%**  :.             :+. -=.%:                         -  
          *%%%%: .-%%%#             ++ ---%.    -=                :==- 
          :%%%-  *%%%%               *- %:#+   =%%%.             .====:
      .%@%+.##.  #%%:                -+ =-=-    *%%:  .            :=  
      =*%%%=-#.                      :: =-:     *%-%%%%%#              
      .%=##* #-                         %.     :%%-+++%%%:             
       +=*+= +%.                      .*.   .=+.%%-#%%#*+:             
        ===: =*%=                   .*+  *%+%%% -%*:%%%%%:       .     
             =***%*.            .:#*:  .%%*+%%%#.+%+. .          =     
       -%#-:        -*########+   .:   +%%%=%%%*++:                    
    .##:---.  :%%-  *: +%%%%%%%%%%%##. -#%%*= =-                       
     *#:-: :%%%%+%%= --%%#. -#%%%%+=#-   =:            .:::::::::::::: 
     :#- =%%%%%%%+++. +*%=-%%%%#-..       ..:::::::::::::..            
        +***+:=***::  ==:.      ....::..                               
     -%%%%%+%%-         ....               .     +##%%=  .#%%%%%%%#.   
    .%%%%##=%%-               :+#%%%#. *%%%%+   -%%%%%= .%%%%%%%%%%%   
     -+--         -=#%#+:   +%%%%%%%#. *%%%%%+  #%%%%%= =%%%*   %%%%.  
       .-+###.  *%%%%%%%%+ -%%%%+.     *%%%%%%:*%%%%%%+ -%%%*   %%%%.  
    .*%%%%%%%  %%%%+.=%%%# =%%%%-      *%%%%%%%%%%*%%%+ -%%%*   %%%%.  
   =%%%%%#=:. :%%%#  .%%%#  #%%%%%%%:  +%%%-*%%%%:=%%%+ -%%%*   %%%%.  
  -%%%%-      .%%%#   %%%%    .#%%%%%= +%%%- #%%= -%%%+ -%%%*  .%%%%.  
  *%%%#       .%%%#.  %%%%       +%%%* +%%%-      -%%%+  #%%%###%%%*   
  %%%%*        %%%#.  #%%%..****#####- =###-      -###+   =######*.    
  #%%%#        #%%%*+*#%%* .########:  =#*=:                           
  +%%%%-   .=+ .########=   :----.           .:::--====++++********### 
  .#%#########:  :==-:          ..:--=====---::::..                    
   .########+.         .:--=--::.                                      
      :--.       .---:.                                                
             :.                                                        
  TEXT
end

.instanceCLI

Returns:



13
14
15
# File 'lib/cosmo/cli.rb', line 13

def self.instance
  @instance ||= new
end

.runvoid

This method returns an undefined value.



9
10
11
# File 'lib/cosmo/cli.rb', line 9

def self.run
  instance.run
end

Instance Method Details

#boot_applicationvoid

This method returns an undefined value.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cosmo/cli.rb', line 56

def boot_application
  boot_path = File.expand_path("config/boot.rb")
  require boot_path if File.exist?(boot_path)

  environment_path = File.expand_path("config/environment.rb")
  require environment_path if File.exist?(environment_path)

  # Ensure the ActiveJob integration is loaded when ActiveJob is present
  # but was not already pulled in by the app (e.g. non-Rails setup or the Railtie hasn't fired yet at this point).
  require "cosmo/active_job" if defined?(::ActiveJob) && !defined?(Cosmo::ActiveJobAdapter::Executor)
end

#flags_parser(flags) ⇒ Object

Parameters:

  • flags (Hash[Symbol, untyped])

Returns:

  • (Object)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cosmo/cli.rb', line 79

def flags_parser(flags)
  OptionParser.new do |o|
    o.banner = "Usage: cosmo [flags] [command] [options]"
    o.separator ""
    o.separator "Command:"
    o.separator "  jobs      Run jobs"
    o.separator "  streams   Run streams"
    o.separator "  actions   Run actions"
    o.separator ""
    o.separator "Flags:"

    o.on "-c", "--concurrency INT", Integer, "Threads to use" do |arg|
      flags[:concurrency] = arg
    end

    o.on "-r", "--require PATH|DIR", "Location of files to require" do |arg|
      flags[:require] = arg
    end

    o.on "-t", "--timeout NUM", Integer, "Shutdown timeout" do |arg|
      flags[:timeout] = arg
    end

    o.on "-C", "--config PATH", "Path to config file" do |arg|
      flags[:config_file] = arg
    end

    o.on "-S", "--setup", "Create/update streams and sync cron schedules, then exit" do
      load_config(flags)
      boot_application

      Config[:setup]&.each do |type, configs|
        next if type == :cron

        first_line = true
        configs.each do |name, config|
          meta = { metadata: { "_cosmo.type" => "jobs" } } if type == :jobs
          Client.instance.setup_stream(name.to_s, config.merge(Hash(meta)))
          first_line ? print("Stream is ready: #{name}") : print(", #{name}")
          first_line = false
        end
      end

      puts
      schedules = Config.dig(:setup, :cron)&.reduce(0) do |sum, (name, entry)|
        class_name = entry.delete(:class)
        API::Cron.instance.upsert!(**entry, name: name, class_name: class_name)
        sum + 1
      end.to_i

      puts "Cron sync complete: #{schedules} schedule(s) registered" unless schedules.zero?
      puts "Cosmo streams#{" and cron schedules" unless schedules.zero?} set up successfully."
      exit(0)
    end

    o.on_tail "-v", "--version", "Print version" do
      puts "Cosmo #{VERSION}"
      exit(0)
    end

    o.on_tail "-h", "--help", "Show help" do
      puts o
      exit(0)
    end
  end
end

#load_config(flags) ⇒ void

This method returns an undefined value.

Parameters:

  • flags (Hash[Symbol, untyped])

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cosmo/cli.rb', line 41

def load_config(flags)
  path = flags[:config_file]
  raise ConfigNotFoundError, path if path && !File.exist?(path)

  unless path
    # Try default path
    default_path = File.expand_path(Config::DEFAULT_PATH)
    path = default_path if File.exist?(default_path)
  end

  Config.load(path)
  Config.set(:concurrency, flags[:concurrency]) if flags[:concurrency]
  Config.set(:timeout, flags[:timeout]) if flags[:timeout]
end

#options_parser(command, options) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Parameters:

  • command (::String, nil)
  • options (Hash[Symbol, untyped])

Returns:

  • (Object)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cosmo/cli.rb', line 146

def options_parser(command, options) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  case command
  when "jobs"
    OptionParser.new do |o|
      o.banner = "Usage: cosmo jobs [options]"

      o.on "--stream NAME", "Job's stream" do |arg|
        options[:stream] = arg
      end

      o.on "--subject NAME", "Job's subject" do |arg|
        options[:subject] = arg
      end
    end
  when "streams"
    OptionParser.new do |o|
      o.banner = "Usage: cosmo streams [options]"
      o.separator ""
      o.separator "  [m] many processors can be specified, in that case single options [1] are ignored"
      o.separator "  [1] options work only for a single processor"
      o.separator ""

      o.on "--processors NAMES", "[m] Specify processors names with comma" do |arg|
        options[:processors] = arg.split(",")
      end

      o.on "--stream NAME", "[1] Specify stream name" do |arg|
        options[:stream] = arg
      end

      o.on "--subject NAME", "[1] Specify subject name" do |arg|
        options[:subject] = arg
      end

      o.on "--consumer_name NAME", "[1] Specify consumer name" do |arg|
        options[:consumer_name] = arg
      end

      o.on "--batch_size NUM", Integer, "[1] Number of messages in the batch" do |arg|
        options[:batch_size] = arg
      end
    end
  when "actions"
    OptionParser.new do |o|
      o.banner = "Usage: cosmo actions [options]"

      o.on "-n", "--nop", "Do nothing and exit" do
        exit(0)
      end
    end
  end
end

#parse[Hash[Symbol, untyped], ::String?, Hash[Symbol, untyped]]

Returns:

  • ([Hash[Symbol, untyped], ::String?, Hash[Symbol, untyped]])


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cosmo/cli.rb', line 28

def parse
  flags = {}
  parser = flags_parser(flags)
  parser.order!

  options = {}
  command = ARGV.shift
  parser = options_parser(command, options)
  parser&.order!

  [flags, command, options]
end

#require_files(path) ⇒ void

This method returns an undefined value.

Parameters:

  • path (::String)


199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cosmo/cli.rb', line 199

def require_files(path)
  path = File.expand_path(path)

  if File.directory?(path)
    files = Dir["#{path}/**/*.rb"]
    files.each { |f| require f }
    return
  end

  require path
end

#require_path(path) ⇒ void

This method returns an undefined value.

Parameters:

  • path (::String, nil)


68
69
70
71
72
73
74
75
76
77
# File 'lib/cosmo/cli.rb', line 68

def require_path(path)
  if path
    require_files(path)
    return # If a path is provided don't load default dirs.
  end

  # Load files from app/streams if they exist.
  # Streams are always eagerly loaded since they register classes to process events.
  require_files("app/streams") if File.directory?("app/streams")
end

#runvoid

This method returns an undefined value.



17
18
19
20
21
22
23
24
# File 'lib/cosmo/cli.rb', line 17

def run
  flags, command, options = parse
  load_config(flags)
  puts self.class.banner
  boot_application
  require_path(flags[:require])
  Engine.run(command, options)
end