From 702f5da610775ae4f945e9f42daa04c5cfc27473 Mon Sep 17 00:00:00 2001 From: libeanim Date: Tue, 6 Jun 2017 12:23:12 +0200 Subject: [PATCH] Add garbage collector command to launcher (#20) * Add garbage collector command to launcher The seafile garbage collector can be started via the 'gc' command option of the launcher. This will stop the seafile-server inside the container, then run the 'seaf-gc.sh' script and redirect its output to '/var/log/gc.log'. Afterwards the whole container will be restarted. * Move garbage collector execution logic to scripts/gc.sh The file gc.sh stops the seafile server, starts the garbage collector and restarts the seafile server when the cleanup is finished. The output of the 'seaf-gc.sh' script is piped to the file '/var/log/gc.log' in append mode. * Add 'scripts/gc.sh' to watch_controller function in 'scripts/start.py' The 'start.py' script monitors the seafile-server in a 'watch_controller' function and terminates the container when the server crashed. However, during a garbage collector cleanup the server needs to be shut down and therefore it is necessary that the 'watch_controller' function only terminates the container if the server is offline and no cleanup is in progress. * Add the gc command info to README * Preseve the exit code of seaf-gc.sh * Fix the perm of scripts/gc.sh --- README.md | 1 + launcher | 18 +++++++++++++++++- scripts/gc.sh | 30 ++++++++++++++++++++++++++++++ scripts/start.py | 5 +++-- 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100755 scripts/gc.sh diff --git a/README.md b/README.md index f652a90..b2677f1 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ Commands: enter: Use docker exec to enter a container logs: Docker logs for container rebuild: Rebuild a container (destroy old, bootstrap, start new) + gc: Start the seafile garbage collector (stops seafile, starts gc, restarts seafile) ``` If the environment variable "SUPERVISED" is set to true, the container won't be detached, allowing a process monitoring tool to manage the restart behaviour of the container. diff --git a/launcher b/launcher index 4939204..712a1c3 100755 --- a/launcher +++ b/launcher @@ -11,6 +11,7 @@ usage () { echo " logs: View the Docker container logs" echo " bootstrap: Bootstrap the container based on a template" echo " rebuild: Rebuild the container (destroy old, bootstrap, start new)" + echo " gc: Start the seafile garbage collector (stops seafile, starts gc, restarts seafile)" echo echo "Options:" echo " --skip-prereqs Don't check launcher prerequisites" @@ -224,6 +225,21 @@ restart() { start } +gc() { + ensure_container_running + ( + loginfo "Stop seafile and start the garbage collector" + set +e +x + docker exec -it seafile /scripts/gc.sh + gc_error=$? + if [[ $gc_error -eq 0 ]]; then + loginfo "Garbage collector finished!" + else + err_and_quit "Garbage collector exited with code $gc_error" + fi + ) +} + check_prereqs() { if [[ $SKIP_PREREQS == "true" ]]; then return 0 @@ -391,7 +407,7 @@ main() { while [[ $# -gt 0 ]] do case "$1" in - bootstrap|start|stop|restart|enter|destroy|logs|rebuild|manual-upgrade) + bootstrap|start|stop|restart|enter|destroy|logs|rebuild|manual-upgrade|gc) action=${1//-/_} ; shift 1 ;; -h|--help) ( usage ; exit 1) ; shift 1 ;; -v|--verbose) verbose=true ; shift 1 ;; diff --git a/scripts/gc.sh b/scripts/gc.sh new file mode 100755 index 0000000..b7049c6 --- /dev/null +++ b/scripts/gc.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +# Before +SEAFILE_DIR=/opt/seafile/seafile-server-latest + +$SEAFILE_DIR/seafile.sh stop + +echo "Waiting for the server to shut down properly..." +sleep 5 + +# Do it +( + set +e + $SEAFILE_DIR/seaf-gc.sh | tee -a /var/log/gc.log + # We want to presevent the exit code of seaf-gc.sh + exit "${PIPESTATUS[0]}" +) + +gc_exit_code=$? + +# After + +echo "Giving the server some time..." +sleep 3 + +$SEAFILE_DIR/seafile.sh start + +exit $gc_exit_code diff --git a/scripts/start.py b/scripts/start.py index 913638f..91897c7 100755 --- a/scripts/start.py +++ b/scripts/start.py @@ -25,8 +25,9 @@ def watch_controller(): maxretry = 4 retry = 0 while retry < maxretry: - controller_pid = get_command_output('ps aux | grep seafile-controller |grep -v grep || true').strip() - if not controller_pid: + controller_pid = get_command_output('ps aux | grep seafile-controller | grep -v grep || true').strip() + garbage_collector_pid = get_command_output('ps aux | grep /scripts/gc.sh | grep -v grep || true').strip() + if not controller_pid and not garbage_collector_pid: retry += 1 else: retry = 0