Module: Lich::Main::UserExitDispatch

Defined in:
documented/main/user_exit_dispatch.rb

Overview

User-initiated ("...exit"/"...quit") shutdown entry point shared by the primary and detachable frontend client loops in lib/main/main.rb.

Extracted from main.rb so the ordering guarantee it enforces is directly testable: the shutdown watchdog must be armed before Common::OrderlyShutdown.request_user_exit runs, because that call drains scripts and runs their +before_dying+/+at_exit+ hooks inline, any of which can hang. A bare request_user_exit would run that hang-prone drain unprotected.

Since:

  • 5.19.2

Class Method Summary collapse

Class Method Details

.active_sessions_lifecycleObject?

Resolves the ActiveSessions lifecycle when the feature is loaded, so a normal exit updates the session registry; nil otherwise.

Returns:

  • (Object, nil)

Since:

  • 5.19.2



70
71
72
# File 'documented/main/user_exit_dispatch.rb', line 70

def active_sessions_lifecycle
  Lich::InternalAPI::ActiveSessions::Lifecycle if defined?(Lich::InternalAPI::ActiveSessions::Lifecycle)
end

.dispatch_detachable_client(client_string, cmd_prefix: $cmd_prefix, **request_options) ⇒ Boolean

Detachable-frontend dispatch for one raw client line.

Applies the same $cmd_prefix prefixing the client loops use before matching, and when the (prefixed) line is a user-exit command, arms the watchdog and runs the orderly shutdown under the :detachable_frontend source. The caller breaks its read loop when this returns true.

Parameters:

  • client_string (String)

    raw line received from the detachable client

  • cmd_prefix (String) (defaults to: $cmd_prefix)

    frontend command prefix (default $cmd_prefix)

  • request_options (Hash)

Returns:

  • (Boolean)

    true when the line was an exit command and was handled

Since:

  • 5.19.2



54
55
56
57
58
59
60
61
62
63
64
# File 'documented/main/user_exit_dispatch.rb', line 54

def dispatch_detachable_client(client_string, cmd_prefix: $cmd_prefix, **request_options)
  prefixed = "#{cmd_prefix}#{client_string}"
  return false unless Lich::Common::ShutdownIntent.user_exit_command?(prefixed)

  run_orderly_user_shutdown(
    source: :detachable_frontend,
    **request_options,
    server_exit_command: prefixed
  )
  true
end

.run_orderly_user_shutdown(source: :primary_frontend, **request_options) ⇒ Object

Arms the shutdown watchdog (idempotent) and then runs the orderly user-exit sequence. Extra keyword options are forwarded to Common::OrderlyShutdown.request_user_exit (used by tests to inject the script drain, Vars, and Game collaborators).

Parameters:

  • source (Symbol) (defaults to: :primary_frontend)

    shutdown source recorded in the coordinator/logs

  • request_options (Hash)

    forwarded to request_user_exit

Returns:

  • (Object)

    the orderly-shutdown result

Since:

  • 5.19.2



34
35
36
37
38
39
40
41
# File 'documented/main/user_exit_dispatch.rb', line 34

def run_orderly_user_shutdown(source: :primary_frontend, **request_options)
  Lich::Common::ShutdownWatchdog.arm if defined?(Lich::Common::ShutdownWatchdog)
  Lich::Common::OrderlyShutdown.request_user_exit(
    source: source,
    active_sessions_lifecycle: active_sessions_lifecycle,
    **request_options
  )
end