mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Transition dbmodule package to new module task system
This commit is contained in:
parent
3236ddf87f
commit
ac6d9db456
2 changed files with 25 additions and 33 deletions
|
@ -38,7 +38,7 @@ func start() error {
|
|||
return err
|
||||
}
|
||||
|
||||
startMaintainer()
|
||||
registerMaintenanceTasks()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,45 +1,37 @@
|
|||
package dbmodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/database"
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portbase/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
maintenanceShortTickDuration = 10 * time.Minute
|
||||
maintenanceLongTickDuration = 1 * time.Hour
|
||||
)
|
||||
|
||||
func startMaintainer() {
|
||||
module.AddWorkers(1)
|
||||
go maintenanceWorker()
|
||||
func registerMaintenanceTasks() {
|
||||
module.NewTask("basic maintenance", maintainBasic).Repeat(10 * time.Minute).MaxDelay(10 * time.Minute)
|
||||
module.NewTask("thorough maintenance", maintainThorough).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
|
||||
module.NewTask("record maintenance", maintainRecords).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
|
||||
}
|
||||
|
||||
func maintenanceWorker() {
|
||||
ticker := time.NewTicker(maintenanceShortTickDuration)
|
||||
longTicker := time.NewTicker(maintenanceLongTickDuration)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
err := database.Maintain()
|
||||
if err != nil {
|
||||
log.Errorf("database: maintenance error: %s", err)
|
||||
}
|
||||
case <-longTicker.C:
|
||||
err := database.MaintainRecordStates()
|
||||
if err != nil {
|
||||
log.Errorf("database: record states maintenance error: %s", err)
|
||||
}
|
||||
err = database.MaintainThorough()
|
||||
if err != nil {
|
||||
log.Errorf("database: thorough maintenance error: %s", err)
|
||||
}
|
||||
case <-module.ShuttingDown():
|
||||
module.FinishWorker()
|
||||
return
|
||||
}
|
||||
func maintainBasic(ctx context.Context, task *modules.Task) {
|
||||
err := database.Maintain()
|
||||
if err != nil {
|
||||
log.Errorf("database: maintenance error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func maintainThorough(ctx context.Context, task *modules.Task) {
|
||||
err := database.MaintainThorough()
|
||||
if err != nil {
|
||||
log.Errorf("database: thorough maintenance error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func maintainRecords(ctx context.Context, task *modules.Task) {
|
||||
err := database.MaintainRecordStates()
|
||||
if err != nil {
|
||||
log.Errorf("database: record states maintenance error: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue