FreePad/lib/controllers/controllers_taskmanager.go

51 lines
947 B
Go
Raw Normal View History

2022-05-19 01:55:14 +03:00
package controllers
import (
2022-05-20 01:59:53 +03:00
"fmt"
"os"
"strconv"
2022-05-19 01:55:14 +03:00
"time"
2022-05-20 01:59:53 +03:00
"github.com/JustKato/FreePad/lib/objects"
2022-05-19 01:55:14 +03:00
)
func TaskManager() {
2022-05-20 01:59:53 +03:00
// Get the cleanup interval
cleanupIntervalString, exists := os.LookupEnv("CLEANUP_MAX_AGE")
if !exists {
cleanupIntervalString = "-1"
}
2022-05-19 01:55:14 +03:00
2022-05-20 01:59:53 +03:00
if cleanupIntervalString == "-1" {
// Do not cleanup
return
2022-05-19 01:55:14 +03:00
}
2022-05-20 01:59:53 +03:00
// Try and parse the string as an int
cleanupInterval, err := strconv.Atoi(cleanupIntervalString)
if err != nil {
cleanupInterval = 1
}
// Run all handlers
go cleanupHandler(cleanupInterval)
go savePostHandler()
}
func savePostHandler() {
// Save the views cache
fmt.Println("[Task::Save]: File save registered")
for range time.NewTicker(time.Minute * 1).C {
objects.SavePostViewsCache()
2022-05-20 01:59:53 +03:00
}
}
2022-05-19 01:55:14 +03:00
func cleanupHandler(cleanupInterval int) {
fmt.Println("[Task::Cleanup]: Cleanup task registered")
for range time.NewTicker(time.Minute * 5).C {
objects.CleanupPosts(cleanupInterval)
}
2022-05-19 01:55:14 +03:00
}