mirror of
https://git.cyberia.club/cyberia/matrix-synapse-diskspace-janitor
synced 2024-05-09 12:06:34 +02:00
add deployment code
This commit is contained in:
parent
a11f555307
commit
7c92979311
26
deployment/matrix-disk-space-janitor.service
Normal file
26
deployment/matrix-disk-space-janitor.service
Normal file
|
@ -0,0 +1,26 @@
|
|||
# this file goes in /lib/systemd/system/matrix-diskspace-janitor.service
|
||||
# then run systemctl daemon-reload
|
||||
# and systemctl enable --now matrix-diskspace-janitor.service
|
||||
|
||||
# you can read the logs with journalctl -u matrix-diskspace-janitor.service -n 100 --no-pager
|
||||
|
||||
[Unit]
|
||||
Description=https://git.cyberia.club/cyberia/matrix-synapse-diskspace-janitor
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=always
|
||||
|
||||
# wait at least 5 seconds before restarting if it crashes
|
||||
RestartSec=5
|
||||
|
||||
WorkingDirectory=/opt/matrix-diskspace-janitor
|
||||
|
||||
ExecStart=/opt/matrix-diskspace-janitor/janitor
|
||||
|
||||
User=caddy
|
||||
Group=caddy
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
|
@ -60,7 +60,11 @@ type DeleteProgress struct {
|
|||
|
||||
func initFrontend(config *Config, db *DBModel) FrontendApp {
|
||||
|
||||
cssBytes, err := os.ReadFile(filepath.Join(".", "frontend", "static", "app.css"))
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "can't initFrontend because can't get working directory:"))
|
||||
}
|
||||
cssBytes, err := os.ReadFile(filepath.Join(currentDirectory, "frontend", "static", "app.css"))
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "can't initFrontend because can't read cssBytes:"))
|
||||
}
|
||||
|
@ -247,7 +251,7 @@ func initFrontend(config *Config, db *DBModel) FrontendApp {
|
|||
|
||||
app.reloadTemplates()
|
||||
|
||||
staticFilesDir := "./frontend/static"
|
||||
staticFilesDir := filepath.Join(currentDirectory, "frontend/static")
|
||||
log.Printf("serving static files from %s", staticFilesDir)
|
||||
app.Router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(staticFilesDir))))
|
||||
|
||||
|
|
20
main.go
20
main.go
|
@ -54,8 +54,17 @@ func main() {
|
|||
|
||||
validateConfig(&config)
|
||||
|
||||
os.MkdirAll("data", 0644)
|
||||
os.MkdirAll("data/sessions", 0644)
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Printf(
|
||||
"🧹 matrix-synapse-diskspace-janitor starting up with UID %d EUID %d GID %d EGID %d workingDirectory '%s'\n",
|
||||
os.Getuid(), os.Geteuid(), os.Getgid(), os.Getegid(), currentDirectory,
|
||||
)
|
||||
|
||||
os.MkdirAll("data", 0755)
|
||||
os.MkdirAll("data/sessions", 0755)
|
||||
|
||||
db := initDatabase(&config)
|
||||
matrixAdmin = initMatrixAdmin(&config)
|
||||
|
@ -104,7 +113,7 @@ func runScheduledTask(db *DBModel, config *Config, measureMediaSize bool, stateG
|
|||
log.Printf("ERROR!: runScheduledTask can't GetDBTableSizes: %s\n", err)
|
||||
}
|
||||
log.Println("Saving data/dbTableSizes.json...")
|
||||
err = WriteJsonFile[[]DBTableSize]("data/dbTableSizes.json", tables)
|
||||
err = WriteJsonFile("data/dbTableSizes.json", tables)
|
||||
if err != nil {
|
||||
log.Printf("ERROR!: runScheduledTask can't write data/dbTableSizes.json: %s\n", err)
|
||||
}
|
||||
|
@ -139,9 +148,6 @@ func runScheduledTask(db *DBModel, config *Config, measureMediaSize bool, stateG
|
|||
PostgresBytes: postgresBytes,
|
||||
}
|
||||
|
||||
const
|
||||
const diskUsagePercent =
|
||||
|
||||
log.Println("Saving data/diskUsage.json...")
|
||||
err = WriteJsonFile("data/diskUsage.json", diskUsage)
|
||||
if err != nil {
|
||||
|
@ -194,8 +200,6 @@ func runScheduledTask(db *DBModel, config *Config, measureMediaSize bool, stateG
|
|||
log.Printf("ERROR!: runScheduledTask can't write data/janitorState.json: %s\n", err)
|
||||
}
|
||||
|
||||
|
||||
|
||||
log.Println("runScheduledTask completed!")
|
||||
isRunningScheduledTask = false
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
errors "git.sequentialread.com/forest/pkg-errors"
|
||||
)
|
||||
|
@ -10,6 +11,11 @@ import (
|
|||
func WriteJsonFile[T any](path string, object T) error {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path = filepath.Join(currentDirectory, path)
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -23,6 +29,11 @@ func ReadJsonFile[T any](path string) (T, error) {
|
|||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
var object T
|
||||
currentDirectory, err := os.Getwd()
|
||||
if err != nil {
|
||||
return object, err
|
||||
}
|
||||
path = filepath.Join(currentDirectory, path)
|
||||
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
return object, nil
|
||||
|
|
Loading…
Reference in a new issue