Module: RactorRailsShim::InstallStrategy::Ractor

Defined in:
lib/ractor_rails_shim/roles/install_strategy.rb

Overview

Full install for Ractor (Puma/Falcon in Ractor mode, Falcon): all patches that route framework globals through per-Ractor IES.

Class Method Summary collapse

Class Method Details

.installObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ractor_rails_shim/roles/install_strategy.rb', line 21

def self.install
  RactorRailsShim.install_mattr_accessor
  RactorRailsShim.install_class_attribute
  RactorRailsShim.install_zeitwerk_registry
  RactorRailsShim.install_rubygems
  RactorRailsShim.install_rails_module
  RactorRailsShim::ConstantShareabilizer.install
  RactorRailsShim.install_execution_wrapper
  require "active_support/callbacks" rescue nil
  RactorRailsShim::CallbackCapture.install_callback_declaration_capture!
  # Patch the `scope` macro BEFORE the app's models are eager-loaded, so
  # `scope :recent, -> { ... }` compiles a worker-safe (string-eval'd)
  # method instead of Rails' un-shareable define_method. The patch
  # requires active_record itself, so it is safe to call this early —
  # it applies immediately and is idempotent (no-op at prepare time).
  RactorRailsShim.__send__(:_install_activerecord_scope_patch)
  # Patch ActiveRecord reflections BEFORE eager-load: AR memoizes ivars
  # (@foreign_key, @klass, @inverse_of, ...) on reflection objects, but
  # those objects are frozen in the shared app graph, so the write raises
  # FrozenError in workers on the first association read. Route the
  # memoization through a per-worker cache instead.
  RactorRailsShim.__send__(:_install_activerecord_reflection_patch)
  # Patch ActiveStorage's `has_one_attached` / `has_many_attached` BEFORE
  # the app's models eager-load, so the generated `has_one`/`has_many`
  # association scope lambda gets a shareable `self` (ActiveStorage's
  # default scope uses the un-shareable association builder as `self`,
  # which makes `User.reflections` un-shareable and breaks attachment
  # reads in worker Ractors). The patch force-loads the macro module if
  # needed and is idempotent (no-op at prepare time).
  RactorRailsShim.__send__(:_install_active_storage_patch)
  # Patch ActiveRecord::Store#store_accessor BEFORE any model that uses
  # `store` is loaded, so the generated accessors (e.g. ActiveStorage's
  # `identified=`/`analyzed=`/`composed=`) are emitted as string-eval
  # `def`s (shareable) instead of `define_method` blocks. Without this,
  # a worker Ractor raises "defined with an un-shareable Proc".
  RactorRailsShim.__send__(:_install_active_record_store_patch)
  # Patch Marcel's `EXTENSIONS` table BEFORE eager-load so the frozen,
  # shareable Hash is in place when worker Ractors extract an attachment's
  # content type (ActiveStorage::Blob#extract_content_type -> Marcel).
  RactorRailsShim.__send__(:_install_marcel_patch)
  # Patch ActionView::Base.with_empty_template_cache EARLY (before
  # eager load) so production's DetailsKey.view_context_class uses the
  # block-free version. The framework's original defines
  # compiled_method_container via define_method(&block) — an
  # un-shareable Proc that breaks worker Ractors. on_load fires as
  # soon as ActionView is required, well before the app's eager_load.
  ActiveSupport.on_load(:action_view) do
    RactorRailsShim._install_with_empty_template_cache_patch
    RactorRailsShim._install_action_view_sanitize_patch
    RactorRailsShim._install_loofah_patch
  end
end