From 1c1790760916c1b8aaffd16e77bf4c135729e37d Mon Sep 17 00:00:00 2001 From: Thomas Klausner Date: Fri, 18 Aug 2023 10:40:37 +0300 Subject: [PATCH] Bug 31729: enable plackup hot restart in ktd This patch implements a more idiomatoic way for hot-reloading. Instead of adding a new daemon/watcher+inotify or use a Middleware, we can use `plackup -R /path/to/dir`, which is the standard Perl way to do hot reloading for plack apps. Instead of calling `/usr/bin/perl starman` directly, we use `plackup -s Starman`. And can then use `-R /kohadevbox/koha/Koha` to watch that directory for changes and hot-reload the plack app. I've added a new option `koha-plack --hot-reload`. Only if this option is set AND we're running inside koha-testing-docker (by checking KOHA_INSTANCE = "kohadev") hot reloading is enabled. Test plan: Before applying the patch: * Start koha-testing-docker and enter the container * Stop plack: koha-plack --stop kohadev * Check the log: tail -n 4 /var/log/koha/kohadev/plack-error.log You should see lines like Worker processes cleaned up Server closing! * Start it again: koha-plack --start kohadev Output should be "Starting Plack daemon for kohadev:" And you end back at the interactive shell * Check the log: tail -n 4 /var/log/koha/kohadev/plack-error.log it should look like: Starman::Server (type Net::Server::PreFork) starting! Binding to UNIX socket file "/var/run/koha/kohadev/plack.sock" Starman: Accepting connections at unix://localhost:/var/run/koha/kohadev/plack.sock/ * Stop it again: koha-plack --stop kohadev Now apply the patch. As in koha-testing-docker the koha-plack script (after applying the patch) is NOT installed to /sbin you have to call the dev version. * Start the server with hot-reloading: debian/scripts/koha-plack --start --hot-reload kohadev * Output should be: Starting Plack daemon for kohadev:Watching /kohadevbox/koha/Koha /etc/koha/sites/kohadev/plack.psgi for file updates. * You are now NOT back in the shell, as the plack app is NOT running daemonized * So you need to open another ktd --shell * Check the log: tail -n 4 /var/log/koha/kohadev/plack-error.log Starman::Server (type Net::Server::PreFork) starting! pid(27638) Binding to UNIX socket file "/var/run/koha/kohadev/plack.sock" Starman: Accepting connections at unix://localhost:/var/run/koha/kohadev/plack.sock/ * Update a code file touch Koha/Biblios.pm * in the other shell (where plack is running) you should see: -- /kohadevbox/koha/Koha/Biblios.pm updated. Killing the existing server Successfully killed! Restarting the new server process. * Yay! * If you want to still stop/restart plack manually, you can hit CTRL-C in the shell were you started plack to stop the process. Signed-off-by: David Cook --- debian/scripts/koha-plack | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/debian/scripts/koha-plack b/debian/scripts/koha-plack index 6fe1860268..86ce5c2f68 100755 --- a/debian/scripts/koha-plack +++ b/debian/scripts/koha-plack @@ -60,6 +60,7 @@ $scriptname -h|--help --debugger-location Specify the host:port for your debugger tool (defaults to localhost:9000) --debugger-path Specify the path for the debugger library + --hot-reload Enable hot-reload (only in koha-testing-docker) --quiet|-q Make the script quiet about non existent instance names (useful for calling from another scripts). --help|-h Display this help message @@ -95,6 +96,7 @@ start_plack() logging="--access-log /var/log/koha/${instancename}/plack.log \ --error-log /var/log/koha/${instancename}/plack-error.log" max_requests_and_workers="--max-requests ${PLACK_MAX_REQUESTS} --workers ${PLACK_WORKERS}" + plack_hot_reload="" if [ "$DEV_INSTALL" = "1" ]; then # Maybe we should switch off debug_mode if DEV_INSTALL is not set? @@ -106,12 +108,18 @@ start_plack() daemonize="" logging="" # remote debugger takes care max_requests_and_workers="--workers 1" - STARMAN="/usr/bin/perl -d ${STARMAN}" + PLACKUP="/usr/bin/perl -d ${PLACKUP}" fi - STARMANOPTS="-M FindBin ${max_requests_and_workers} \ + if [ "$hot_reload" = "yes" ] && [ "$KOHA_INSTANCE" = "kohadev" ] && [ -d "/kohadevbox/koha/Koha" ]; then + plack_hot_reload="-R /kohadevbox/koha/Koha"; + daemonize="" + fi + + PLACKUPOPTS="-M FindBin ${max_requests_and_workers} \ + -s Starman --user=${instance_user} --group ${instancename}-koha \ - --pid ${PIDFILE} ${daemonize} ${logging} \ + --pid ${PIDFILE} ${daemonize} ${logging} ${plack_hot_reload}\ -E ${environment} --socket ${PLACKSOCKET} ${PSGIFILE}" if ! is_plack_running ${instancename}; then @@ -123,7 +131,7 @@ start_plack() current_dir=$(pwd) eval cd ~$instance_user - if ${STARMAN} ${STARMANOPTS}; then + if ${PLACKUP} ${PLACKUPOPTS}; then log_end_msg 0 else log_end_msg 1 @@ -398,13 +406,14 @@ _do_instance() { esac } -STARMAN=$(which starman) +PLACKUP=$(which plackup) op="" quiet="no" debug_mode="no" debugger_key="" debugger_location="localhost:9000" debugger_path="" +hot_reload="" # Read command line parameters while [ $# -gt 0 ]; do @@ -445,6 +454,9 @@ while [ $# -gt 0 ]; do --debugger-path) debugger_path="$2" shift 2 ;; + --hot-reload) + hot_reload="yes" + shift ;; -*) die "Error: invalid option switch ($1)" ;; *) -- 2.30.2