Don't install docker in arvbox now that arvados-server install does it.
[arvados.git] / services / keep-balance / server.go
index 0f4bb7176ae58044027a947cb821ba8db5ff9cdc..e485f5b2061f28134306d1d897b22cb62e4190e9 100644 (file)
@@ -2,18 +2,17 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-package main
+package keepbalance
 
 import (
-       "context"
        "net/http"
        "os"
        "os/signal"
        "syscall"
        "time"
 
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "github.com/jmoiron/sqlx"
        "github.com/sirupsen/logrus"
 )
 
@@ -25,11 +24,12 @@ import (
 //
 // RunOptions fields are controlled by command line flags.
 type RunOptions struct {
-       Once        bool
-       CommitPulls bool
-       CommitTrash bool
-       Logger      logrus.FieldLogger
-       Dumper      logrus.FieldLogger
+       Once                  bool
+       CommitPulls           bool
+       CommitTrash           bool
+       CommitConfirmedFields bool
+       Logger                logrus.FieldLogger
+       Dumper                logrus.FieldLogger
 
        // SafeRendezvousState from the most recent balance operation,
        // or "" if unknown. If this changes from one run to the next,
@@ -40,6 +40,7 @@ type RunOptions struct {
 
 type Server struct {
        http.Handler
+
        Cluster    *arvados.Cluster
        ArvClient  *arvados.Client
        RunOptions RunOptions
@@ -47,35 +48,38 @@ type Server struct {
 
        Logger logrus.FieldLogger
        Dumper logrus.FieldLogger
+
+       DB *sqlx.DB
 }
 
 // CheckHealth implements service.Handler.
 func (srv *Server) CheckHealth() error {
-       return nil
+       return srv.DB.Ping()
 }
 
-// Start sets up and runs the balancer.
-func (srv *Server) Start(ctx context.Context) {
-       if srv.RunOptions.Logger == nil {
-               srv.RunOptions.Logger = ctxlog.FromContext(ctx)
-       }
-
-       srv.Logger = srv.RunOptions.Logger
-       srv.Dumper = srv.RunOptions.Dumper
+// Done implements service.Handler.
+func (srv *Server) Done() <-chan struct{} {
+       return nil
+}
 
+func (srv *Server) run() {
        var err error
        if srv.RunOptions.Once {
-               _, err = srv.run()
+               _, err = srv.runOnce()
        } else {
                err = srv.runForever(nil)
        }
        if err != nil {
                srv.Logger.Error(err)
+               os.Exit(1)
+       } else {
+               os.Exit(0)
        }
 }
 
-func (srv *Server) run() (*Balancer, error) {
+func (srv *Server) runOnce() (*Balancer, error) {
        bal := &Balancer{
+               DB:             srv.DB,
                Logger:         srv.Logger,
                Dumper:         srv.Dumper,
                Metrics:        srv.Metrics,
@@ -106,7 +110,7 @@ func (srv *Server) runForever(stop <-chan interface{}) error {
                        logger.Print("=======  Consider using -commit-pulls and -commit-trash flags.")
                }
 
-               _, err := srv.run()
+               _, err := srv.runOnce()
                if err != nil {
                        logger.Print("run failed: ", err)
                } else {