LibrePortal/scripts/source/load_sources.sh
librelad dba63873f5 feat(lazy-load): flip CLI to LP_LAZY=1 by default (Phase 4)
scripts/source/load_sources.sh — when init_run_flag=false (one-shot CLI
invocation), default LP_LAZY=1 before sourceCheckFiles runs. Honours an
explicit `LP_LAZY=0 libreportal …` override for debugging or for working
around a stale manifest.

Long-running processes (init_run_flag=true → task processor, WebUI
service) stay unchanged — they want eager loading because they keep
running and benefit from every function being already-hot.

Measured on this box, 3 runs each:
  EAGER (default before this commit):
    real 0.91s / 1.12s / 0.94s   avg ~0.99s wall
    user 0.40s / 0.41s / 0.42s   avg ~0.41s CPU
  LAZY (new default for CLI):
    real 0.63s / 0.65s / 0.66s   avg ~0.65s wall
    user 0.26s / 0.27s / 0.26s   avg ~0.26s CPU

Wall: ~340ms saved per invocation (34%).
User: ~150ms saved per invocation (37%).

Files actually sourced at startup: 455 → 8 (the manifest itself + 7
side-effect files: setup_lock, the two crontab processors, backup_db,
backup_files, swap_docker_type, migrate_url_rewrite).

Safety nets:
- Missing manifest auto-falls-back to eager loading (init.sh check
  in the lazy branch sets LP_LAZY=0 if function_manifest.sh is absent).
- Stub for a function not in the manifest still produces a clean
  'command not found' rather than weird behaviour, so a stale manifest
  surfaces immediately. `libreportal regen arrays` regenerates both
  files_*.sh and function_manifest.sh.

Smoke-tested (lazy mode active): `libreportal help`, `peer list`,
`peer` (shows full help), `restore` (shows full help), `debug load-
trace peer list` (traces a lazy run and shows the 8 files loaded). All
output identical to eager mode.

Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-26 20:56:25 +01:00

59 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# This is used for initial loading
# The starting point and is the only code that doesnt contain logic requirements
# Source "init.sh" and "variables.sh" if they exist, otherwise return an error
if [ -f "init.sh" ] && [ -f "variables.sh" ]; then
source "init.sh"
source "variables.sh"
else
# Print an error message for any missing files
[ ! -f "init.sh" ] && isError " File 'init.sh' does not exist. Unable to source."
[ ! -f "variables.sh" ] && isError " File 'variables.sh' does not exist. Unable to source."
echo "Files are missing, please run 'libreportal reset'"
fi
# Source config check
if [ -f "${install_scripts_dir}config/core/config_check_missing.sh" ]; then
source "${install_scripts_dir}config/core/config_check_missing.sh"
checkConfigFilesMissingFiles;
else
# Print an error message for any missing files
[ ! -f "${install_scripts_dir}config/core/config_check_missing.sh" ] && isError " File '${install_scripts_dir}config/core/config_check_missing.sh' does not exist. Unable to source."
echo "Files are missing, please run 'libreportal reset'"
fi
# Source the remaining files if they all exist
if [ -f "${install_scripts_dir}source/loading/check_files.sh" ] && \
[ -f "${install_scripts_dir}source/loading/initilize_files.sh" ] && \
[ -f "${install_scripts_dir}source/loading/scan_files.sh" ]; then
source "${install_scripts_dir}source/loading/check_files.sh"
source "${install_scripts_dir}source/loading/initilize_files.sh"
source "${install_scripts_dir}source/loading/scan_files.sh"
else
# Print an error message for any missing files
[ ! -f "${install_scripts_dir}source/loading/check_files.sh" ] && isError " File '${install_scripts_dir}source/loading/check_files.sh' does not exist. Unable to source."
[ ! -f "${install_scripts_dir}source/loading/initilize_files.sh" ] && isError " File '${install_scripts_dir}source/loading/initilize_files.sh' does not exist. Unable to source."
[ ! -f "${install_scripts_dir}source/loading/scan_files.sh" ] && isError " File '${install_scripts_dir}source/loading/scan_files.sh' does not exist. Unable to source."
echo "Files are missing, please run 'libreportal reset'"
fi
# For starting the script
if [[ $init_run_flag == "true" ]]; then
# For loading LibrePortal as a long-running process (task processor,
# WebUI service). Eager loading wins here — the process keeps running,
# so first call to anything should be hot. LP_LAZY stays unset.
sourceCheckFiles "run";
elif [[ $init_run_flag == "false" ]]; then
# For one-shot CLI invocations. Default LP_LAZY=1 so short commands
# (`libreportal help`, `peer list`, etc.) only source the ~8 files with
# top-level side effects + the function manifest stubs, deferring the
# ~420 pure function libs until something actually calls them. ~34%
# wall-time savings on a no-op command in measurement (Phase 4 of the
# lazy-load work). Explicit `LP_LAZY=0 libreportal …` still works for
# debugging or for catching a stale manifest.
: "${LP_LAZY:=1}"
export LP_LAZY
sourceCheckFiles "cli";
fi