From 66b958d952c50764f9e63b38045189199451ccbe Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Wed, 11 Mar 2020 14:42:44 +0000 Subject: [PATCH] Add janitor job to clean up old files Leave 10 most recent ttyrecs Leave 5 most recent SOTW dump files --- Makefile | 2 ++ deps/Dockerfile | 1 + deps/clean.sh | 28 +++++++++++++++++++++++++++ deps/link_sotw.sh | 6 +++++- nethack-launcher/cleanup.go | 19 ++++++++++++++++++ nethack-launcher/print_user_screen.go | 4 ++-- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100755 deps/clean.sh create mode 100644 nethack-launcher/cleanup.go diff --git a/Makefile b/Makefile index 3d90dbe..7bd7af9 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ all: stat run: go run \ nethack-launcher/check_dir.go \ + nethack-launcher/cleanup.go \ nethack-launcher/create_initial_files.go \ nethack-launcher/create_user_files.go \ nethack-launcher/janitor.go \ @@ -47,6 +48,7 @@ build_docker: fetch_sotw cp deps/run_nethack.sh stage.tmp/ cp deps/reclist.c stage.tmp/ cp deps/link_sotw.sh stage.tmp/ + cp deps/clean.sh stage.tmp/ cp deps/scores.dat stage.tmp/ wget -O stage.tmp/redis-server https://cryo.unixvoid.com/bin/redis/5.0.7/redis-server chmod +x stage.tmp/redis-server diff --git a/deps/Dockerfile b/deps/Dockerfile index 2821cb6..1f3fb5e 100644 --- a/deps/Dockerfile +++ b/deps/Dockerfile @@ -110,6 +110,7 @@ COPY chowner.sh /bin/ COPY nethackrc /.nethackrc COPY run_nethack.sh / COPY link_sotw.sh /bin/ +COPY clean.sh /bin/ ADD sotw.tar.gz / RUN mv /shadow-of-the-wyrm* /sotw/ COPY scores.dat /sotw/ diff --git a/deps/clean.sh b/deps/clean.sh new file mode 100755 index 0000000..f00ddd9 --- /dev/null +++ b/deps/clean.sh @@ -0,0 +1,28 @@ +#!/bin/bash +HACKDIR=$1 +USERDIR=$2 + +### +# clean old ttyrecs +### +cd /$HACKDIR/user/$USERDIR/ttyrec/ +ls -1tr | head -n -10 | xargs -d '\n' rm -f -- + + +### +# clean sotw files +### +cd /$HACKDIR/user/$USERDIR/sotw/ + +# there are always 4 symlinked files +# leave 5 of the latest dumps + +FILES=$(ls -1tr *.txt | head -n -9) + +for f in $FILES; do + #test -h $f || echo "removing old file $f" + if [ ! -h $f ]; then + echo "removing old file $f" + rm -rf $f + fi +done diff --git a/deps/link_sotw.sh b/deps/link_sotw.sh index 2f69b0a..aeacdcc 100755 --- a/deps/link_sotw.sh +++ b/deps/link_sotw.sh @@ -16,6 +16,10 @@ done # remove inital score file rm -rf $USERDIR/sotw/scores.dat - # link in shared score file from hackdir ln -s $HACKDIR/scores.dat $USERDIR/sotw/ + +# remove initial config file +rm -rf $USERDIR/sotw/swyrm.ini +# copy in sotw config file +cp $HACKDIR/swyrm.ini $USERDIR/sotw/ diff --git a/nethack-launcher/cleanup.go b/nethack-launcher/cleanup.go new file mode 100644 index 0000000..12736f3 --- /dev/null +++ b/nethack-launcher/cleanup.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "os/exec" +) + +func runCleanup(username string) { + // clean up ttyrecs in //user//ttyrec/ + // clean up sotw dumps, aka: + // non-symlinked .txt files in //user//sotw/ + + // run cleanup file + out, err := exec.Command("/bin/clean.sh", config.NethackLauncher.HackDir, username).Output() + if err != nil { + fmt.Println(err) + } + fmt.Printf("%s\n", out) +} diff --git a/nethack-launcher/print_user_screen.go b/nethack-launcher/print_user_screen.go index b0a5bab..0601cc6 100644 --- a/nethack-launcher/print_user_screen.go +++ b/nethack-launcher/print_user_screen.go @@ -94,7 +94,7 @@ func printUserScreen(redisClient *redis.Client, username string) string { currentTime := time.Now().UTC() fulltime := currentTime.Format("2006-01-02.03:04:05") go runGame(username, fulltime) - //go runCleanup(username) + go runCleanup(username) watcher := startWatcher(username, fulltime, redisClient) wg.Wait() close(watcher) @@ -104,7 +104,7 @@ func printUserScreen(redisClient *redis.Client, username string) string { currentTime := time.Now().UTC() fulltime := currentTime.Format("2006-01-02.03:04:05") go runSotwGame(username, fulltime) - //go runCleanup(username) + go runCleanup(username) watcher := startWatcher(username, fulltime, redisClient) wg.Wait() close(watcher)