Skip to content

Commit

Permalink
Merge pull request goharbor#4910 from vmware/fix_none_zero_issue
Browse files Browse the repository at this point in the history
Improve the error handling capabilities of job service
  • Loading branch information
reasonerjt authored May 10, 2018
2 parents 9fd9ea0 + 4db7080 commit cf79e9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
28 changes: 25 additions & 3 deletions src/jobservice/job/impl/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"context"
"errors"
"fmt"
"math"
"reflect"
"time"

"github.com/vmware/harbor/src/adminserver/client"
"github.com/vmware/harbor/src/common"
Expand All @@ -19,6 +21,10 @@ import (
"github.com/vmware/harbor/src/jobservice/logger"
)

const (
maxRetryTimes = 5
)

//Context ...
type Context struct {
//System context
Expand Down Expand Up @@ -51,9 +57,25 @@ func NewContext(sysCtx context.Context, adminClient client.Client) *Context {

//Init ...
func (c *Context) Init() error {
configs, err := c.adminClient.GetCfgs()
if err != nil {
return err
var (
counter = 0
err error
configs map[string]interface{}
)

for counter == 0 || err != nil {
counter++
configs, err = c.adminClient.GetCfgs()
if err != nil {
logger.Errorf("Job context initialization error: %s\n", err.Error())
if counter < maxRetryTimes {
backoff := (int)(math.Pow(2, (float64)(counter))) + 2*counter + 5
logger.Infof("Retry in %d seconds", backoff)
time.Sleep(time.Duration(backoff) * time.Second)
} else {
return fmt.Errorf("job context initialization error: %s (%d times tried)", err.Error(), counter)
}
}
}

db := getDBFromConfig(configs)
Expand Down
7 changes: 2 additions & 5 deletions src/jobservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"flag"
"fmt"

"github.com/vmware/harbor/src/adminserver/client"
"github.com/vmware/harbor/src/jobservice/config"
Expand All @@ -22,15 +21,13 @@ func main() {

//Missing config file
if configPath == nil || utils.IsEmptyStr(*configPath) {
fmt.Println("Config file should be specified")
flag.Usage()
return
logger.Fatal("Config file should be specified")
}

//Load configurations
if err := config.DefaultConfig.Load(*configPath, true); err != nil {
fmt.Printf("Failed to load configurations with error: %s\n", err)
return
logger.Fatalf("Failed to load configurations with error: %s\n", err)
}

//Set job context initializer
Expand Down
9 changes: 7 additions & 2 deletions src/jobservice/runtime/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ func (bs *Bootstrap) LoadAndRun() {
logSweeper := logger.NewSweeper(ctx, config.GetLogBasePath(), config.GetLogArchivePeriod())
logSweeper.Start()

//To indicate if any errors occurred
var err error
//Block here
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM, os.Kill)
select {
case <-sig:
case err := <-rootContext.ErrorChan:
logger.Errorf("Server error:%s\n", err)
case err = <-rootContext.ErrorChan:
}

//Call cancel to send termination signal to other interested parts.
Expand Down Expand Up @@ -125,6 +126,10 @@ func (bs *Bootstrap) LoadAndRun() {
rootContext.WG.Wait()
close <- true

if err != nil {
logger.Fatalf("Server exit with error: %s\n", err)
}

logger.Infof("Server gracefully exit")
}

Expand Down

0 comments on commit cf79e9e

Please sign in to comment.