feat: replace std log with cylogger, add flag-based init, enhance logs,
update Go version and deps, and streamline Docker build/deploy scripts
This commit is contained in:
99
main.go
99
main.go
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
|
||||
"github.com/djherbis/times"
|
||||
)
|
||||
|
||||
@@ -35,25 +37,25 @@ func parseDuration(date string) int64 {
|
||||
var parts = strings.Split(date, "_")
|
||||
for _, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
log.Printf("Parsing date part: %s\n", part)
|
||||
logger.Info("Parsing date part: %s", part)
|
||||
var value = valueRegex.FindString(part)
|
||||
var unit = unitRegex.FindString(part)
|
||||
|
||||
if value == "" || unit == "" {
|
||||
log.Println("Invalid date part: " + part)
|
||||
logger.Error("Invalid date part: " + part)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := timeUnits[unit]; !ok {
|
||||
log.Println("Invalid date unit: " + unit)
|
||||
logger.Error("Invalid date unit: " + unit)
|
||||
continue
|
||||
}
|
||||
log.Printf("Part %s parsed as: Value: %s, Unit: %s\n", part, value, unit)
|
||||
logger.Info("Part %s parsed as: Value: %s, Unit: %s", part, value, unit)
|
||||
|
||||
var valueMs, _ = strconv.ParseInt(value, 10, 16)
|
||||
valueMs = valueMs * timeUnits[unit]
|
||||
milliseconds += valueMs
|
||||
log.Printf("Adding %dms to duration, now: %d\n", valueMs, milliseconds)
|
||||
logger.Info("Adding %dms to duration, now: %d", valueMs, milliseconds)
|
||||
}
|
||||
|
||||
return milliseconds
|
||||
@@ -68,31 +70,32 @@ func getEnv(key, def string) string {
|
||||
}
|
||||
|
||||
func scanRoot() {
|
||||
log.Println("Scanning root directory...")
|
||||
log := logger.Default.WithPrefix("scanRoot")
|
||||
log.Info("Scanning root directory...")
|
||||
filepath.Walk(constants.ROOT, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
log.Printf("Error scanning %s: %s\n", path, err)
|
||||
log.Error("Error scanning %s: %s", path, err)
|
||||
return nil
|
||||
}
|
||||
path = filepath.ToSlash(path)
|
||||
|
||||
if path == constants.ROOT {
|
||||
log.Printf("Skipping root directory %s...\n", path)
|
||||
log.Info("Skipping root directory %s...", path)
|
||||
return nil
|
||||
}
|
||||
|
||||
// I forgot why this code was here... It doesn't make sense to me now
|
||||
// if info.IsDir() {
|
||||
// log.Printf("Skipping directory %s...\n", path)
|
||||
// log.Info("Skipping directory %s...", path)
|
||||
// return filepath.SkipDir
|
||||
// }
|
||||
|
||||
// We hope that IGNORED_DIRECTORIES is a small list, so we can afford to iterate over it
|
||||
// In fact iteration should be faster for small lists rather than hashing
|
||||
for _, ignoredDir := range constants.IGNORED_DIRECTORIES {
|
||||
log.Println(constants.IGNORED_DIRECTORIES, len(constants.IGNORED_DIRECTORIES))
|
||||
log.Info("Ignored directories: %s", constants.IGNORED_DIRECTORIES)
|
||||
if strings.HasPrefix(path, ignoredDir) {
|
||||
log.Printf("Ignoring directory %s\n", path)
|
||||
log.Info("Ignoring directory %s", path)
|
||||
return filepath.SkipDir
|
||||
}
|
||||
}
|
||||
@@ -103,16 +106,17 @@ func scanRoot() {
|
||||
}
|
||||
|
||||
func scanArchive() {
|
||||
log.Println("Scanning archive...")
|
||||
log := logger.Default.WithPrefix("scanArchive")
|
||||
log.Info("Scanning archive...")
|
||||
filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
log.Printf("Error scanning %s: %s\n", path, err)
|
||||
log.Error("Error scanning %s: %s", path, err)
|
||||
return nil
|
||||
}
|
||||
path = filepath.ToSlash(path)
|
||||
|
||||
if path == constants.ROOT_ARCHIVE {
|
||||
log.Printf("Skipping root directory %s...\n", path)
|
||||
log.Info("Skipping root directory %s...", path)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -123,6 +127,7 @@ func scanArchive() {
|
||||
|
||||
func processFile(path string, info os.FileInfo) {
|
||||
var now = time.Now().UnixMilli()
|
||||
log := logger.Default.WithPrefix("processFile").WithPrefix(path)
|
||||
|
||||
var timeType = "accessed"
|
||||
if constants.USE_MODTIME {
|
||||
@@ -135,17 +140,18 @@ func processFile(path string, info os.FileInfo) {
|
||||
} else {
|
||||
fileTime = times.Get(info).AccessTime().UnixMilli()
|
||||
}
|
||||
|
||||
|
||||
var timeDelta = now - fileTime
|
||||
log.Printf("File %s last %s at %d, %dms ago\n", path, timeType, fileTime, timeDelta)
|
||||
log.Info("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
|
||||
if timeDelta > constants.ARCHIVE_THRESHOLD {
|
||||
log.Printf("File %s was %s more than %dms ago, archiving...\n", path, timeType, constants.ARCHIVE_THRESHOLD)
|
||||
log.Info("File %s was %s more than %dms ago, archiving...", path, timeType, constants.ARCHIVE_THRESHOLD)
|
||||
archiveFile(path)
|
||||
}
|
||||
}
|
||||
|
||||
func processArchiveFile(path string, info os.FileInfo) {
|
||||
var now = time.Now().UnixMilli()
|
||||
log := logger.Default.WithPrefix("processArchiveFile").WithPrefix(path)
|
||||
|
||||
var timeType = "accessed"
|
||||
if constants.USE_MODTIME {
|
||||
@@ -160,10 +166,10 @@ func processArchiveFile(path string, info os.FileInfo) {
|
||||
}
|
||||
|
||||
var timeDelta = now - int64(fileTime)
|
||||
log.Printf("File %s last %s at %d, %dms ago\n", path, timeType, fileTime, timeDelta)
|
||||
|
||||
log.Info("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
|
||||
|
||||
if timeDelta > constants.DELETE_THRESHOLD {
|
||||
log.Printf("File %s was %s more than %dms ago, deleting...\n", path, timeType, constants.DELETE_THRESHOLD)
|
||||
log.Info("File %s was %s more than %dms ago, deleting...", path, timeType, constants.DELETE_THRESHOLD)
|
||||
deleteFile(path)
|
||||
}
|
||||
}
|
||||
@@ -171,32 +177,35 @@ func processArchiveFile(path string, info os.FileInfo) {
|
||||
func archiveFile(path string) {
|
||||
// defer os.Exit(1)
|
||||
var newPath = constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
|
||||
log.Printf("Archiving file %s to %s...\n", path, newPath)
|
||||
log := logger.Default.WithPrefix("archiveFile").WithPrefix(path)
|
||||
log.Info("Archiving file %s to %s...", path, newPath)
|
||||
|
||||
os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
|
||||
var err = os.Rename(path, newPath)
|
||||
if err != nil {
|
||||
log.Printf("Error archiving file %s: %s\n", path, err)
|
||||
log.Error("Error archiving file %s: %s", path, err)
|
||||
return
|
||||
}
|
||||
numFilesArchived++
|
||||
}
|
||||
|
||||
func deleteFile(path string) {
|
||||
log := logger.Default.WithPrefix("deleteFile").WithPrefix(path)
|
||||
// defer os.Exit(1)
|
||||
log.Printf("Deleting file %s...\n", path)
|
||||
log.Info("Deleting file %s...", path)
|
||||
var err = os.Remove(path)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting file %s: %s\n", path, err)
|
||||
log.Error("Error deleting file %s: %s", path, err)
|
||||
return
|
||||
}
|
||||
numFilesDeleted++
|
||||
}
|
||||
|
||||
func cleanRoot() {
|
||||
log := logger.Default.WithPrefix("cleanRoot")
|
||||
var files, err = os.ReadDir(constants.ROOT)
|
||||
if err != nil {
|
||||
log.Printf("Error reading root directory %s: %s\n", constants.ROOT, err)
|
||||
log.Error("Error reading root directory %s: %s", constants.ROOT, err)
|
||||
return
|
||||
}
|
||||
for _, file := range files {
|
||||
@@ -205,15 +214,15 @@ func cleanRoot() {
|
||||
}
|
||||
var empty, err = isDirEmpty(constants.ROOT + "/" + file.Name())
|
||||
if err != nil {
|
||||
log.Printf("Error checking if directory %s is empty: %s\n", file.Name(), err)
|
||||
log.Error("Error checking if directory %s is empty: %s", file.Name(), err)
|
||||
continue
|
||||
}
|
||||
log.Printf("Directory %s isempty: %t\n", file.Name(), empty)
|
||||
log.Info("Directory %s isempty: %t", file.Name(), empty)
|
||||
if empty {
|
||||
log.Printf("Deleting empty directory %s\n", file.Name())
|
||||
log.Info("Deleting empty directory %s", file.Name())
|
||||
var err = os.RemoveAll(constants.ROOT + "/" + file.Name())
|
||||
if err != nil {
|
||||
log.Printf("Error deleting empty directory %s: %s\n", file.Name(), err)
|
||||
log.Error("Error deleting empty directory %s: %s", file.Name(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -221,16 +230,17 @@ func cleanRoot() {
|
||||
func isDirEmpty(dirPath string) (bool, error) {
|
||||
var empty = true
|
||||
var ferr error = nil
|
||||
log := logger.Default.WithPrefix("isDirEmpty").WithPrefix(dirPath)
|
||||
|
||||
filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
log.Printf("Error scanning %s: %s\n", path, err)
|
||||
log.Error("Error scanning %s: %s", path, err)
|
||||
ferr = err
|
||||
return nil
|
||||
}
|
||||
if !info.IsDir() {
|
||||
empty = false
|
||||
log.Printf("Directory %s is not empty, found %s\n", dirPath, path)
|
||||
log.Info("Directory %s is not empty, found %s", dirPath, path)
|
||||
return filepath.SkipAll
|
||||
}
|
||||
return nil
|
||||
@@ -252,7 +262,7 @@ func doRun() {
|
||||
scanRoot()
|
||||
scanArchive()
|
||||
cleanRoot()
|
||||
log.Printf("Archived %d files, deleted %d files\n", numFilesArchived, numFilesDeleted)
|
||||
logger.Info("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted)
|
||||
numFilesArchived = 0
|
||||
numFilesDeleted = 0
|
||||
}
|
||||
@@ -260,12 +270,14 @@ func doRun() {
|
||||
var constants = Constants{}
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0b111)
|
||||
flag.Parse()
|
||||
logger.InitFlag()
|
||||
// Important: Access times don’t accumulate.
|
||||
// This implies that archiving the file won't alter its access time.
|
||||
// Therefore, assign X as the ARCHIVE_TIME and X + Y as the DELETE_TIME,
|
||||
// where X represents the duration it can exist in the folder,
|
||||
// and Y represents the duration it can exist in the archive.
|
||||
logger.Info("Starting directory cleaner")
|
||||
|
||||
var ROOT = filepath.ToSlash(strings.TrimSpace(getEnv("ROOT", "/tmp")))
|
||||
var ROOT_ARCHIVE = filepath.ToSlash(strings.TrimSpace(getEnv("ROOT_ARCHIVE", ROOT+"/archive")))
|
||||
@@ -284,6 +296,14 @@ func main() {
|
||||
var DELETE_THRESHOLD = parseDuration(getEnv("DELETE_THRESHOLD", "12h"))
|
||||
var SCAN_INTERVAL = time.Duration(parseDuration(getEnv("SCAN_INTERVAL", "1m")) * 1e6)
|
||||
var USE_MODTIME = strings.TrimSpace(getEnv("USE_MODTIME", "false")) == "true"
|
||||
logger.Info("Input args parsed as:")
|
||||
logger.Info("ROOT: %s", ROOT)
|
||||
logger.Info("ROOT_ARCHIVE: %s", ROOT_ARCHIVE)
|
||||
logger.Info("IGNORED_DIRECTORIES: %s", IGNORED_DIRECTORIES)
|
||||
logger.Info("ARCHIVE_THRESHOLD: %d", ARCHIVE_THRESHOLD)
|
||||
logger.Info("DELETE_THRESHOLD: %d", DELETE_THRESHOLD)
|
||||
logger.Info("SCAN_INTERVAL: %d", SCAN_INTERVAL.Milliseconds())
|
||||
logger.Info("USE_MODTIME: %s", strconv.FormatBool(USE_MODTIME))
|
||||
|
||||
constants.ROOT = ROOT
|
||||
constants.ROOT_ARCHIVE = ROOT_ARCHIVE
|
||||
@@ -293,18 +313,9 @@ func main() {
|
||||
constants.SCAN_INTERVAL = SCAN_INTERVAL
|
||||
constants.USE_MODTIME = USE_MODTIME
|
||||
|
||||
log.Println("Input args parsed as:")
|
||||
log.Printf("ROOT: %s\n", ROOT)
|
||||
log.Printf("ROOT_ARCHIVE: %s\n", ROOT_ARCHIVE)
|
||||
log.Printf("IGNORED_DIRECTORIES: %s\n", IGNORED_DIRECTORIES)
|
||||
log.Printf("ARCHIVE_THRESHOLD: %d\n", ARCHIVE_THRESHOLD)
|
||||
log.Printf("DELETE_THRESHOLD: %d\n", DELETE_THRESHOLD)
|
||||
log.Printf("SCAN_INTERVAL: %d\n", SCAN_INTERVAL.Milliseconds())
|
||||
log.Printf("USE_MODTIME: %s\n", strconv.FormatBool(USE_MODTIME))
|
||||
|
||||
doRun()
|
||||
for {
|
||||
log.Printf("Running at %d", time.Now().UnixMilli())
|
||||
logger.Info("Running at %d", time.Now().UnixMilli())
|
||||
time.Sleep(SCAN_INTERVAL)
|
||||
doRun()
|
||||
}
|
||||
|
Reference in New Issue
Block a user