regenerate template cache on a timer

default:12hr
This commit is contained in:
Lilian Jónsdóttir 2024-01-30 10:58:25 -08:00
parent 7451953d43
commit 4d8001c283
5 changed files with 39 additions and 2 deletions

View file

@ -8,6 +8,7 @@ Code for the [burning.moe](https://burning.moe) homepage
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
- [log](https://github.com/charmbracelet/log)
- [mage](https://github.com/magefile/mage)
- [tasks](https://github.com/madflojo/tasks)
- written using [helix](https://helix-editor.com/)
- tested on [void linux](https://voidlinux.org/)
- hosted on [debian linux](https://www.debian.org/)

View file

@ -5,10 +5,13 @@ package main
import (
"fmt"
"net/http"
"time"
"git.burning.moe/celediel/burning.moe/internal/config"
"git.burning.moe/celediel/burning.moe/internal/handlers"
"git.burning.moe/celediel/burning.moe/internal/render"
"github.com/madflojo/tasks"
)
// App wide config data and such
@ -22,6 +25,26 @@ func main() {
handlers.Initialise(&app)
render.Initialise(&app)
// Setup task to regenerate template cache
scheduler := tasks.New()
defer scheduler.Stop()
id, err := scheduler.Add(&tasks.Task{
Interval: time.Duration(app.CacheTimer),
TaskFunc: func() error {
err := render.RegenerateTemplateCache()
if err != nil {
return err
}
app.Logger.Infof("Regenerate cache job finished at %s", time.Now())
return nil
},
ErrFunc: func(err error) {
app.Logger.Error("Error in template regeneration cache job!", "err", err)
},
})
app.Logger.Info("Started cache regeneration task.", "interval", app.CacheTimer.String(), "id", id)
// Initialise the webserver
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.ListenPort),
@ -30,7 +53,7 @@ func main() {
// and finally, start the server
app.Logger.Printf("Starting HTTP Server on port %d", app.ListenPort)
err := srv.ListenAndServe()
err = srv.ListenAndServe()
if err != nil {
app.Logger.Fatal("Failed to start HTTP Server!", "err", err)
}

2
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/charmbracelet/log v0.3.1
github.com/go-chi/chi/v5 v5.0.11
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/madflojo/tasks v1.1.0
github.com/magefile/mage v1.15.0
)
@ -21,6 +22,7 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rs/xid v1.5.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

4
go.sum
View file

@ -19,6 +19,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/madflojo/tasks v1.1.0 h1:CHQhzdot1MdAlWLw/VOa69SRZ28VQ+gbSf9lI55aSsM=
github.com/madflojo/tasks v1.1.0/go.mod h1:h0cJ1gYqIBzczyg0tyEqGAbIltbxxhUsMMKnaQa9IPc=
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
@ -35,6 +37,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=

View file

@ -12,6 +12,7 @@ import (
// AppConfig contains data to be accessed across the app.
type AppConfig struct {
CacheTimer time.Duration
ListenPort uint16
Logger *log.Logger
LogLevel log.Level
@ -21,6 +22,7 @@ type AppConfig struct {
// defaluts contains default settings that are used if no environmental variables are set
var defaults = &AppConfig{
CacheTimer: time.Hour * 12,
ListenPort: 9001,
LogLevel: log.InfoLevel,
UseCache: true,
@ -28,6 +30,7 @@ var defaults = &AppConfig{
// ConfigDatabase contains data to be loaded from environmental variables
type ConfigDatabase struct {
CacheTimer time.Duration `env:"CACHETIMER" env-description:"How often to automatically regenerate template cache."`
LogLevel string `env:"LOGLEVEL" env-description:"Logging level. Default: warn, Possible values: debug info warn error fatal none"`
Port uint16 `env:"PORT" env-description:"server port"`
UseCache bool `env:"CACHE" env-description:"Use template cache"`
@ -47,6 +50,7 @@ func Initialise() AppConfig {
if cfg, err := loadConfig(); err == nil {
app.ListenPort = cfg.Port
app.UseCache = cfg.UseCache
app.CacheTimer = cfg.CacheTimer
app.LogLevel, err = log.ParseLevel(cfg.LogLevel)
if err != nil {
app.LogLevel = defaults.LogLevel
@ -56,7 +60,10 @@ func Initialise() AppConfig {
}
app.Logger.SetLevel(app.LogLevel)
app.Logger.Debug("Loaded config from environment:", "port", app.ListenPort, "useCache", app.UseCache, "log_level", app.LogLevel)
app.Logger.Debug("Loaded config from environment:",
"port", app.ListenPort, "useCache", app.UseCache,
"log_level", app.LogLevel, "cacheTimer", app.CacheTimer,
)
return app
}