mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
startMaintainer()
|
registerMaintenanceTasks()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,45 +1,37 @@
|
||||||
package dbmodule
|
package dbmodule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
|
"github.com/safing/portbase/modules"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func registerMaintenanceTasks() {
|
||||||
maintenanceShortTickDuration = 10 * time.Minute
|
module.NewTask("basic maintenance", maintainBasic).Repeat(10 * time.Minute).MaxDelay(10 * time.Minute)
|
||||||
maintenanceLongTickDuration = 1 * time.Hour
|
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 startMaintainer() {
|
|
||||||
module.AddWorkers(1)
|
|
||||||
go maintenanceWorker()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func maintenanceWorker() {
|
func maintainBasic(ctx context.Context, task *modules.Task) {
|
||||||
ticker := time.NewTicker(maintenanceShortTickDuration)
|
|
||||||
longTicker := time.NewTicker(maintenanceLongTickDuration)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
err := database.Maintain()
|
err := database.Maintain()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("database: maintenance error: %s", err)
|
log.Errorf("database: maintenance error: %s", err)
|
||||||
}
|
}
|
||||||
case <-longTicker.C:
|
}
|
||||||
|
|
||||||
|
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()
|
err := database.MaintainRecordStates()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("database: record states maintenance error: %s", err)
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue