switchman 3.5.0 → 3.5.2

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: 1f6f878b1a2a9ba1dc0c75ce4fcd5d299178ea7940a048190bd4b181ae306339
4
- data.tar.gz: 19b31ce1e456fbecf2a9ba9b15644bc6073cd521b6a604da81a0c5f8977f579a
3
+ metadata.gz: 9a91e85cce6bc5ae253213341573bb4937692ce0763bb4359ffa5ee4b12d8a2e
4
+ data.tar.gz: deae9f167202394f6a8d02910a911f4d64a2d944ed9406f0263861b8f609ca82
5
5
  SHA512:
6
- metadata.gz: bad542adf6c09d1e1e50ef3054a8ca8d106bd3eedb7cb9dbbe544e44dd88c7f19226e7dd0d805ad5a5685009be7b9c9fe20623182c8c624c88724d00d7bc5656
7
- data.tar.gz: 43b76369798a3683f838e392466cec0628d0c4a309354db7ccadcafca604f6eeffdcc460e036ca8d0f105c7abed2987baa23d51b830e628951c057adb554f305
6
+ metadata.gz: 85626eedd9e74291cbad627523936750dc5a3551bc9cd77e6dd5c49790edeb2e392d974f394bc1f94205b82447be2d4b71c8a108ab63f77947021b6402a64327
7
+ data.tar.gz: e288052614bb7e4a872a5aba886829b0b86f1f9105914cfc947badd6ae3323da7e2cbac835521321a71200cd612d2e945ce474546cb87b71205a240e910d1af6
@@ -27,12 +27,17 @@ module Switchman
27
27
  return configs if configs.is_a?(Array)
28
28
 
29
29
  db_configs = configs.flat_map do |env_name, config|
30
- # It would be nice to do the auto-fallback that we want here, but we haven't
31
- # actually done that for years (or maybe ever) and it will be a big lift to get working
32
- roles = config.keys.select do |k|
33
- config[k].is_a?(Hash) || (config[k].is_a?(Array) && config[k].all?(Hash))
30
+ if config.is_a?(Hash)
31
+ # It would be nice to do the auto-fallback that we want here, but we haven't
32
+ # actually done that for years (or maybe ever) and it will be a big lift to get working
33
+ roles = config.keys.select do |k|
34
+ config[k].is_a?(Hash) || (config[k].is_a?(Array) && config[k].all?(Hash))
35
+ end
36
+ base_config = config.except(*roles)
37
+ else
38
+ base_config = config
39
+ roles = []
34
40
  end
35
- base_config = config.except(*roles)
36
41
 
37
42
  name = "#{env_name}/primary"
38
43
  name = "primary" if env_name == default_env
@@ -5,10 +5,12 @@ module Switchman
5
5
  module Persistence
6
6
  # touch reads the id attribute directly, so it's not relative to the current shard
7
7
  def touch(*, **)
8
+ writable_shadow_record_warning
8
9
  shard.activate(self.class.connection_class_for_self) { super }
9
10
  end
10
11
 
11
12
  def update_columns(*)
13
+ writable_shadow_record_warning
12
14
  shard.activate(self.class.connection_class_for_self) { super }
13
15
  end
14
16
 
@@ -17,6 +19,16 @@ module Switchman
17
19
  db.unguard { super }
18
20
  end
19
21
 
22
+ def destroy
23
+ writable_shadow_record_warning
24
+ super
25
+ end
26
+
27
+ def create_or_update(**, &block)
28
+ writable_shadow_record_warning
29
+ super
30
+ end
31
+
20
32
  def reload(*)
21
33
  res = super
22
34
  # When a shadow record is reloaded the real record is returned. So
@@ -24,6 +36,12 @@ module Switchman
24
36
  @loaded_from_shard = @shard
25
37
  res
26
38
  end
39
+
40
+ def writable_shadow_record_warning
41
+ return unless shadow_record? && Switchman.config[:writable_shadow_records]
42
+
43
+ Switchman::Deprecation.warn("writing to shadow records is not supported")
44
+ end
27
45
  end
28
46
  end
29
47
  end
@@ -81,6 +81,8 @@ module Switchman
81
81
  # Do this after so that all database servers for all roles are established and we won't prematurely
82
82
  # configure a connection for the wrong role
83
83
  @all_roles = roles.uniq
84
+ return @database_servers if @database_servers.empty?
85
+
84
86
  Shard.send(:configure_connects_to)
85
87
  end
86
88
  @database_servers
@@ -168,7 +168,7 @@ module Switchman
168
168
  # silly like dealloc'ing prepared statements)
169
169
  ::ActiveRecord::Base.clear_all_connections!
170
170
 
171
- parent_process_name = `ps -ocommand= -p#{Process.pid}`.slice(/#{$0}.*/)
171
+ parent_process_name = sanitized_process_title
172
172
  ret = ::Parallel.map(scopes, in_processes: (scopes.length > 1) ? parallel : 0) do |server, subscope|
173
173
  name = server.id
174
174
  last_description = name
@@ -444,6 +444,39 @@ module Switchman
444
444
  shard = nil unless shard.database_server
445
445
  shard
446
446
  end
447
+
448
+ # Determines the name of the current process, including arguments, but stripping
449
+ # any shebang from the invoked script, and any additional path info from the
450
+ # executable.
451
+ #
452
+ # @return [String]
453
+ def sanitized_process_title
454
+ # get the effective process name from `ps`; this will include any changes
455
+ # from Process.setproctitle _or_ assigning to $0.
456
+ parent_process_name = `ps -ocommand= -p#{Process.pid}`.strip
457
+ # Effective process titles may be shorter than the actual
458
+ # command; truncate our ARGV[0] so that they are comparable
459
+ # for the next step
460
+ argv0 = if parent_process_name.length < Process.argv0.length
461
+ Process.argv0[0..parent_process_name.length]
462
+ else
463
+ Process.argv0
464
+ end
465
+
466
+ # when running via a shebang, the `ps` output will include the shebang
467
+ # (i.e. it will be "ruby bin/rails c"); attempt to strip it off.
468
+ # Note that argv0 in this case will _only_ be `bin/rails` (no shebang,
469
+ # no arguments). We want to preserve the arguments we got from `ps`
470
+ if (index = parent_process_name.index(argv0))
471
+ parent_process_name.slice!(0...index)
472
+ end
473
+
474
+ # remove directories from the main executable to make more room
475
+ # for additional info
476
+ argv = parent_process_name.shellsplit
477
+ argv[0] = File.basename(argv[0])
478
+ argv.shelljoin
479
+ end
447
480
  end
448
481
 
449
482
  def name
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = "3.5.0"
4
+ VERSION = "3.5.2"
5
5
  end
data/lib/switchman.rb CHANGED
@@ -18,6 +18,8 @@ loader.inflector = SwitchmanInflector.new(__FILE__)
18
18
  loader.setup
19
19
 
20
20
  module Switchman
21
+ Deprecation = ::ActiveSupport::Deprecation.new("4.0", "Switchman")
22
+
21
23
  def self.config
22
24
  # TODO: load from yaml
23
25
  @config ||= {}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.0
4
+ version: 3.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-05-02 00:00:00.000000000 Z
13
+ date: 2023-05-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord