mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 09:49:28 +08:00
cron / timer updates
This commit is contained in:
19
hub/main.go
19
hub/main.go
@@ -67,12 +67,11 @@ func main() {
|
||||
return err
|
||||
}
|
||||
usersAuthOptions := usersCollection.AuthOptions()
|
||||
usersAuthOptions.AllowUsernameAuth = false
|
||||
if os.Getenv("DISABLE_PASSWORD_AUTH") == "true" {
|
||||
usersAuthOptions.AllowEmailAuth = false
|
||||
usersAuthOptions.AllowUsernameAuth = false
|
||||
} else {
|
||||
usersAuthOptions.AllowEmailAuth = true
|
||||
usersAuthOptions.AllowUsernameAuth = true
|
||||
}
|
||||
usersCollection.SetOptions(usersAuthOptions)
|
||||
if err := app.Dao().SaveCollection(usersCollection); err != nil {
|
||||
@@ -99,11 +98,13 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
|
||||
// set up cron jobs
|
||||
// set up cron jobs / ticker for system updates
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
// 15 second ticker for system updates
|
||||
go startSystemUpdateTicker()
|
||||
// cron job to delete old records
|
||||
scheduler := cron.New()
|
||||
// delete records that are older than the display period
|
||||
scheduler.MustAdd("delete old records", "8 */2 * * *", func() {
|
||||
scheduler.MustAdd("delete old records", "8 * * * *", func() {
|
||||
deleteOldRecords("system_stats", "1m", time.Hour)
|
||||
deleteOldRecords("container_stats", "1m", time.Hour)
|
||||
deleteOldRecords("system_stats", "10m", 12*time.Hour)
|
||||
@@ -151,12 +152,6 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
|
||||
// start ticker for server updates
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
go serverUpdateTicker()
|
||||
return nil
|
||||
})
|
||||
|
||||
// immediately create connection for new servers
|
||||
app.OnModelAfterCreate("systems").Add(func(e *core.ModelEvent) error {
|
||||
go updateSystem(e.Model.(*models.Record))
|
||||
@@ -206,7 +201,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func serverUpdateTicker() {
|
||||
func startSystemUpdateTicker() {
|
||||
ticker := time.NewTicker(15 * time.Second)
|
||||
for range ticker.C {
|
||||
updateSystems()
|
||||
|
@@ -140,11 +140,10 @@ func twoDecimals(value float64) float64 {
|
||||
|
||||
/* Delete records of specified collection and type that are older than timeLimit */
|
||||
func deleteOldRecords(collection string, recordType string, timeLimit time.Duration) {
|
||||
// log.Println("Deleting old", recordType, "records...")
|
||||
timeLimitStamp := time.Now().UTC().Add(timeLimit).Format("2006-01-02 15:04:05")
|
||||
timeLimitStamp := time.Now().UTC().Add(-timeLimit).Format("2006-01-02 15:04:05")
|
||||
records, _ := app.Dao().FindRecordsByExpr(collection,
|
||||
dbx.NewExp("type = {:type}", dbx.Params{"type": recordType}),
|
||||
dbx.NewExp("created > {:created}", dbx.Params{"created": timeLimitStamp}),
|
||||
dbx.NewExp("created < {:created}", dbx.Params{"created": timeLimitStamp}),
|
||||
)
|
||||
for _, record := range records {
|
||||
if err := app.Dao().DeleteRecord(record); err != nil {
|
||||
|
Reference in New Issue
Block a user