Remove janitor (#16)

* Remove controller's janitor

* Properly handle err == nil in mapErr() function
This commit is contained in:
Nikolay Edigaryev 2023-02-01 19:02:56 +04:00 committed by GitHub
parent 6bcc02d815
commit ab3ffe82fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 64 deletions

View File

@ -79,15 +79,6 @@ func (controller *Controller) Run(ctx context.Context) error {
}
}()
// Run the janitor so that inactive workers
// will eventually be removed from the DB
go func() {
err := controller.runJanitor(controller.store)
if err != nil {
panic(err)
}
}()
// A helper function to shut down the HTTP server on context cancellation
go func() {
<-ctx.Done()

View File

@ -1,52 +0,0 @@
package controller
import (
storepkg "github.com/cirruslabs/orchard/internal/controller/store"
"github.com/cirruslabs/orchard/pkg/resource/v1"
"time"
)
const janitorInterval = 5 * time.Second
func (controller *Controller) runJanitor(store *storepkg.Store) error {
ticker := time.NewTicker(janitorInterval)
for {
if err := controller.runJanitorInner(store); err != nil {
return err
}
<-ticker.C
}
}
func (controller *Controller) runJanitorInner(store *storepkg.Store) error {
var workers []*v1.Worker
var err error
err = store.View(func(txn *storepkg.Txn) error {
workers, err = txn.ListWorkers()
return err
})
if err != nil {
return err
}
for _, worker := range workers {
worker := worker
if time.Since(worker.LastSeen).Minutes() > 1 {
controller.logger.Debugf("removing outdated worker %s", worker.Name)
err := store.Update(func(txn *storepkg.Txn) error {
return txn.DeleteWorker(worker.Name)
})
if err != nil {
return err
}
}
}
return nil
}

View File

@ -12,9 +12,13 @@ var (
)
func mapErr(err error) error {
if errors.Is(err, badger.ErrKeyNotFound) {
return ErrNotFound
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return ErrNotFound
}
return fmt.Errorf("%w: %v", ErrBadgerFailed, err)
}
return fmt.Errorf("%w: %v", ErrBadgerFailed, err)
return err
}