github.com/Axway/agent-sdk@v1.1.101/pkg/traceability/healthcheck.go (about)

     1  package traceability
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Axway/agent-sdk/pkg/jobs"
     7  	hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
     8  	"github.com/Axway/agent-sdk/pkg/util/log"
     9  )
    10  
    11  // traceabilityHealthCheck -
    12  type traceabilityHealthCheck struct {
    13  	jobs.Job
    14  	logger  log.FieldLogger
    15  	ready   bool
    16  	prevErr error
    17  }
    18  
    19  func newTraceabilityHealthCheckJob() *traceabilityHealthCheck {
    20  	return &traceabilityHealthCheck{
    21  		logger: log.NewFieldLogger().WithComponent("traceabilityHealthCheck").WithPackage(traceabilityStr),
    22  	}
    23  }
    24  
    25  // Ready -
    26  func (j *traceabilityHealthCheck) Ready() bool {
    27  	j.ready = j.checkConnections() == nil
    28  	return j.ready
    29  }
    30  
    31  // Status -
    32  func (j *traceabilityHealthCheck) Status() error {
    33  	return j.prevErr
    34  }
    35  
    36  // Execute -
    37  func (j *traceabilityHealthCheck) Execute() error {
    38  	return j.checkConnections()
    39  }
    40  
    41  func (j *traceabilityHealthCheck) healthcheck(name string) *hc.Status {
    42  	// Create the default status
    43  	status := &hc.Status{
    44  		Result: hc.OK,
    45  	}
    46  
    47  	if !j.ready || j.prevErr != nil {
    48  		status.Result = hc.FAIL
    49  		status.Details = "agent not connected to traceability yet"
    50  	}
    51  
    52  	if j.prevErr != nil {
    53  		status.Details = fmt.Sprintf("connection error: %s Failed. %s", name, j.prevErr.Error())
    54  	}
    55  	return status
    56  }
    57  
    58  func (j *traceabilityHealthCheck) checkConnections() error {
    59  	client, err := getClient()
    60  	if err != nil {
    61  		j.logger.WithError(err).Error("could not get traceability client")
    62  		return err
    63  	}
    64  	j.prevErr = client.Connect()
    65  	if j.prevErr != nil {
    66  		j.logger.WithError(j.prevErr).Error("connection failed")
    67  	} else {
    68  		j.logger.Trace("connection to traceability succeeded")
    69  	}
    70  	return j.prevErr
    71  }