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

     1  package agent
     2  
     3  import (
     4  	"fmt"
     5  
     6  	hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
     7  	"github.com/Axway/agent-sdk/pkg/util/log"
     8  )
     9  
    10  type healthChecker interface {
    11  	Healthcheck(_ string) *hc.Status
    12  }
    13  
    14  type centralHealthCheckJob struct {
    15  	logger        log.FieldLogger
    16  	healthChecker healthChecker
    17  }
    18  
    19  func newCentralHealthCheckJob(checker healthChecker) *centralHealthCheckJob {
    20  	logger := log.NewFieldLogger().WithPackage("agent.sdk").WithComponent("centralHealthCheckJob")
    21  	return &centralHealthCheckJob{
    22  		logger:        logger,
    23  		healthChecker: checker,
    24  	}
    25  }
    26  
    27  func (c *centralHealthCheckJob) Execute() error {
    28  	return c.check()
    29  }
    30  
    31  func (c *centralHealthCheckJob) Status() error {
    32  	return c.check()
    33  }
    34  
    35  func (c *centralHealthCheckJob) Ready() bool {
    36  	return true
    37  }
    38  
    39  func (c *centralHealthCheckJob) check() error {
    40  	status := c.healthChecker.Healthcheck("")
    41  	if status == nil || status.Result != hc.OK {
    42  		err := fmt.Errorf("central health check status is not OK")
    43  		c.logger.WithError(err).Error(status.Details)
    44  		return err
    45  	}
    46  	return nil
    47  }