cosmonats 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: faaf7e9f0c3b86375099ee43d1125ec1545251d121fedbf8d7596bb0ca8f2461
4
- data.tar.gz: 97fd56077b64673e02af110dfab8edbeea424114b1a54f89b240a54d308bbcdc
3
+ metadata.gz: c0995753e33b278f5a0d2d6455faf07f5c3c604595fa93c6a40260c68b69c160
4
+ data.tar.gz: c78c109d014f863079c3ee53171a852241b5a0b3082f9a29b1438b5708782436
5
5
  SHA512:
6
- metadata.gz: db91195e38a240f5091ca616e1cb916707b6943e12fe61b173282a438381b6d258946a59e99445c9f5c26519515c7ea8ba57b95cdfd004ff396ec36c2067002b
7
- data.tar.gz: 76eb8aa63116623cdfe1bf1d0ce07a57148f473b9818cc2ab6097501740668d3f3b1f5202f6f83eb91340d13cb713a0836d42db01e70f0f921a9f617fed8b19d
6
+ metadata.gz: 7947f18a6ab2830fa726d9903eb8ee926bf4bbf897ee71e28c262adc4aa664828c1ad758776aa61caac2964e52c83370cd4d4090c3ce05f399988c79917676e6
7
+ data.tar.gz: 12a7f6f7e3a715bdda711fa29acbba7db822d17cd4441b008942d7cac0eb66581f2412a4df16811580792af0913c0ffbe1370b0465478e577a6a3a45b714364b
data/bin/cosmo CHANGED
@@ -4,4 +4,6 @@
4
4
  require "bundler/setup"
5
5
  require "cosmonats"
6
6
 
7
+ Process.setproctitle("cosmo")
8
+
7
9
  Cosmo::CLI.run
data/lib/cosmo/cli.rb CHANGED
@@ -14,11 +14,12 @@ module Cosmo
14
14
  end
15
15
 
16
16
  def run
17
- flags, command, _options = parse
17
+ flags, command, options = parse
18
18
  load_config(flags[:config_file])
19
19
  puts self.class.banner
20
- require_files(flags[:require])
21
- Engine.run(command)
20
+ boot_application
21
+ require_path(flags[:require])
22
+ Engine.run(command, options)
22
23
  end
23
24
 
24
25
  private
@@ -48,15 +49,23 @@ module Cosmo
48
49
  Config.load(path)
49
50
  end
50
51
 
51
- def require_files(path)
52
- return unless path
52
+ def boot_application
53
+ boot_path = File.expand_path("config/boot.rb")
54
+ require boot_path if File.exist?(boot_path)
53
55
 
54
- if File.directory?(path)
55
- files = Dir[File.expand_path("#{path}/*.rb")]
56
- files.each { |f| require f }
57
- else
58
- require File.expand_path(path)
56
+ environment_path = File.expand_path("config/environment.rb")
57
+ require environment_path if File.exist?(environment_path)
58
+ end
59
+
60
+ def require_path(path)
61
+ if path
62
+ require_files(path)
63
+ return # If a path is provided don't load default dirs.
59
64
  end
65
+
66
+ # Load files from app/streams if they exist.
67
+ # Streams are always eagerly loaded since they register classes to process events.
68
+ require_files("app/streams") if File.directory?("app/streams")
60
69
  end
61
70
 
62
71
  def flags_parser(flags) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@@ -128,20 +137,28 @@ module Cosmo
128
137
  when "streams"
129
138
  OptionParser.new do |o|
130
139
  o.banner = "Usage: cosmo streams [options]"
140
+ o.separator ""
141
+ o.separator " [m] many processors can be specified, in that case single options [1] are ignored"
142
+ o.separator " [1] options work only for a single processor"
143
+ o.separator ""
144
+
145
+ o.on "--processors NAMES", "[m] Specify processors names with comma" do |arg|
146
+ options[:processors] = arg.split(",")
147
+ end
131
148
 
132
- o.on "--stream NAME", "Specify stream name" do |arg|
149
+ o.on "--stream NAME", "[1] Specify stream name" do |arg|
133
150
  options[:stream] = arg
134
151
  end
135
152
 
136
- o.on "--subject NAME", "Specify subject name" do |arg|
153
+ o.on "--subject NAME", "[1] Specify subject name" do |arg|
137
154
  options[:subject] = arg
138
155
  end
139
156
 
140
- o.on "--consumer_name NAME", "Specify consumer name" do |arg|
157
+ o.on "--consumer_name NAME", "[1] Specify consumer name" do |arg|
141
158
  options[:consumer_name] = arg
142
159
  end
143
160
 
144
- o.on "--batch_size NUM", Integer, "Number of messages in the batch" do |arg|
161
+ o.on "--batch_size NUM", Integer, "[1] Number of messages in the batch" do |arg|
145
162
  options[:batch_size] = arg
146
163
  end
147
164
  end
@@ -156,6 +173,18 @@ module Cosmo
156
173
  end
157
174
  end
158
175
 
176
+ def require_files(path)
177
+ path = File.expand_path(path)
178
+
179
+ if File.directory?(path)
180
+ files = Dir["#{path}/**/*.rb"]
181
+ files.each { |f| require f }
182
+ return
183
+ end
184
+
185
+ require path
186
+ end
187
+
159
188
  # rubocop:disable Layout/TrailingWhitespace,Lint/IneffectiveAccessModifier
160
189
  def self.banner
161
190
  <<-TEXT
data/lib/cosmo/engine.rb CHANGED
@@ -23,12 +23,16 @@ module Cosmo
23
23
  @running = Concurrent::AtomicBoolean.new
24
24
  end
25
25
 
26
- def run(type)
26
+ def run(type, options)
27
27
  handler = Utils::Signal.trap(:INT, :TERM)
28
28
  Logger.info "Starting processing, hit Ctrl-C to stop"
29
29
 
30
- @processors = type && PROCESSORS.key?(type.to_sym) ? [PROCESSORS[type.to_sym]] : PROCESSORS.values
31
- @processors = @processors.map { _1.run(@pool, @running) }
30
+ processor_classes = type && PROCESSORS.key?(type.to_sym) ? [PROCESSORS[type.to_sym]] : PROCESSORS.values
31
+ @processors = processor_classes.map { _1.run(@pool, @running, options) }
32
+ if @running.false?
33
+ Logger.warn "Shutting down... (No processors are running)"
34
+ return
35
+ end
32
36
 
33
37
  signal = handler.wait
34
38
  Logger.info "Shutting down... (#{signal} received)"
@@ -3,7 +3,7 @@
3
3
  module Cosmo
4
4
  module Job
5
5
  class Processor < ::Cosmo::Processor
6
- def initialize(pool, running)
6
+ def initialize(pool, running, options)
7
7
  super
8
8
  @weights = []
9
9
  end
@@ -6,9 +6,10 @@ module Cosmo
6
6
  new(...).tap(&:run)
7
7
  end
8
8
 
9
- def initialize(pool, running)
9
+ def initialize(pool, running, options)
10
10
  @pool = pool
11
11
  @running = running
12
+ @options = options
12
13
  @consumers = {}
13
14
  end
14
15
 
@@ -3,7 +3,7 @@
3
3
  module Cosmo
4
4
  module Stream
5
5
  class Processor < ::Cosmo::Processor
6
- def initialize(pool, running)
6
+ def initialize(pool, running, options)
7
7
  super
8
8
  @configs = {}
9
9
  @processors = {}
@@ -63,18 +63,10 @@ module Cosmo
63
63
  raise
64
64
  end
65
65
 
66
- def setup_configs # rubocop:disable Metrics/AbcSize
67
- @configs.merge!(
68
- Config.dig(:consumers, :streams).to_h do |config|
69
- klass = Utils::String.safe_constantize(config[:class])
70
- [config[:stream].to_sym, klass ? config.merge(class: klass) : nil]
71
- end.compact
72
- )
73
- @configs.merge!(
74
- Config.system[:streams].to_h do |klass|
75
- [klass.default_options[:stream].to_sym, klass.default_options.merge(class: klass)]
76
- end
77
- )
66
+ def setup_configs
67
+ configs = static_config.merge(dynamic_config)
68
+ configs = configs.select { |_, c| @options[:processors].include?(c[:class].name) } if @options[:processors]
69
+ @configs.merge!(configs)
78
70
  end
79
71
 
80
72
  def setup_processors
@@ -89,6 +81,20 @@ module Cosmo
89
81
  @consumers[stream_name] = client.subscribe(subjects, consumer_name, config.merge(deliver_policy))
90
82
  end
91
83
  end
84
+
85
+ def static_config
86
+ Config.dig(:consumers, :streams)&.filter_map do |config|
87
+ next unless (klass = Utils::String.safe_constantize(config[:class]))
88
+
89
+ [config[:stream].to_sym, config.merge(class: klass)]
90
+ end.to_h
91
+ end
92
+
93
+ def dynamic_config
94
+ Config.system[:streams].to_h do |klass|
95
+ [klass.default_options[:stream].to_sym, klass.default_options.merge(class: klass)]
96
+ end
97
+ end
92
98
  end
93
99
  end
94
100
  end
data/lib/cosmo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cosmo
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/sig/cosmo/cli.rbs CHANGED
@@ -16,7 +16,11 @@ module Cosmo
16
16
 
17
17
  def load_config: (::String? path) -> void
18
18
 
19
- def require_files: (::String? path) -> void
19
+ def boot_application: () -> void
20
+
21
+ def require_path: (::String? path) -> void
22
+
23
+ def require_files: (::String path) -> void
20
24
 
21
25
  def flags_parser: (Hash[Symbol, untyped] flags) -> untyped
22
26
 
data/sig/cosmo/engine.rbs CHANGED
@@ -14,7 +14,7 @@ module Cosmo
14
14
 
15
15
  def initialize: () -> void
16
16
 
17
- def run: (::String? type) -> void
17
+ def run: (::String? type, Hash[Symbol, untyped] options) -> void
18
18
 
19
19
  def shutdown: () -> void
20
20
  end
@@ -3,10 +3,11 @@ module Cosmo
3
3
  @pool: Utils::ThreadPool
4
4
  @running: untyped
5
5
  @consumers: Hash[Symbol, untyped]
6
+ @options: Hash[Symbol, untyped]
6
7
 
7
8
  def self.run: (*untyped) -> Processor
8
9
 
9
- def initialize: (Utils::ThreadPool pool, untyped running) -> void
10
+ def initialize: (Utils::ThreadPool pool, untyped running, Hash[Symbol, untyped] options) -> void
10
11
 
11
12
  def run: () -> void
12
13
 
@@ -21,6 +21,10 @@ module Cosmo
21
21
  def setup_processors: () -> void
22
22
 
23
23
  def setup_consumers: () -> void
24
+
25
+ def static_config: -> Hash[Symbol, untyped]
26
+
27
+ def dynamic_config: -> Hash[Symbol, untyped]
24
28
  end
25
29
  end
26
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cosmonats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Vorotilin