mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-27 17:50:53 +01:00
- Added WaitForReady method to GrpcClient for blocking until the client is ready. - Updated WorkerService to utilize WaitForReady for ensuring gRPC client readiness before starting. - Refactored ModelService to consistently use GetGrpcClient for context management. - Changed logging level for received metrics in MetricServiceServer from Info to Debug. - Modified error handling in HandleError to conditionally print errors based on the environment. - Cleaned up unused GrpcClient references in various services, improving code clarity.
200 lines
4.5 KiB
Go
200 lines
4.5 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/cenkalti/backoff/v4"
|
|
"github.com/crawlab-team/crawlab/core/grpc/middlewares"
|
|
"github.com/crawlab-team/crawlab/core/interfaces"
|
|
"github.com/crawlab-team/crawlab/core/utils"
|
|
grpc2 "github.com/crawlab-team/crawlab/grpc"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/connectivity"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
type GrpcClient struct {
|
|
// dependencies
|
|
nodeCfgSvc interfaces.NodeConfigService
|
|
|
|
// settings
|
|
address string
|
|
timeout time.Duration
|
|
|
|
// internals
|
|
conn *grpc.ClientConn
|
|
err error
|
|
once sync.Once
|
|
stopped bool
|
|
stop chan struct{}
|
|
|
|
// clients
|
|
NodeClient grpc2.NodeServiceClient
|
|
TaskClient grpc2.TaskServiceClient
|
|
ModelBaseServiceClient grpc2.ModelBaseServiceClient
|
|
DependencyClient grpc2.DependencyServiceClient
|
|
MetricClient grpc2.MetricServiceClient
|
|
}
|
|
|
|
func (c *GrpcClient) Start() (err error) {
|
|
c.once.Do(func() {
|
|
// connect
|
|
err = c.connect()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// register rpc services
|
|
c.register()
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
func (c *GrpcClient) Stop() (err error) {
|
|
// set stopped flag
|
|
c.stopped = true
|
|
c.stop <- struct{}{}
|
|
log.Infof("[GrpcClient] stopped")
|
|
|
|
// skip if connection is nil
|
|
if c.conn == nil {
|
|
return nil
|
|
}
|
|
|
|
// close connection
|
|
if err := c.conn.Close(); err != nil {
|
|
return err
|
|
}
|
|
log.Infof("grpc client disconnected from %s", c.address)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *GrpcClient) WaitForReady() {
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
if c.IsReady() {
|
|
return
|
|
}
|
|
case <-c.stop:
|
|
log.Errorf("grpc client stopped")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *GrpcClient) register() {
|
|
c.NodeClient = grpc2.NewNodeServiceClient(c.conn)
|
|
c.ModelBaseServiceClient = grpc2.NewModelBaseServiceClient(c.conn)
|
|
c.TaskClient = grpc2.NewTaskServiceClient(c.conn)
|
|
c.DependencyClient = grpc2.NewDependencyServiceClient(c.conn)
|
|
c.MetricClient = grpc2.NewMetricServiceClient(c.conn)
|
|
}
|
|
|
|
func (c *GrpcClient) Context() (ctx context.Context, cancel context.CancelFunc) {
|
|
return context.WithTimeout(context.Background(), c.timeout)
|
|
}
|
|
|
|
func (c *GrpcClient) IsReady() (res bool) {
|
|
return c.conn != nil && c.conn.GetState() == connectivity.Ready
|
|
}
|
|
|
|
func (c *GrpcClient) IsClosed() (res bool) {
|
|
if c.conn != nil {
|
|
return c.conn.GetState() == connectivity.Shutdown
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c *GrpcClient) getRequestData(d interface{}) (data []byte) {
|
|
if d == nil {
|
|
return data
|
|
}
|
|
switch d.(type) {
|
|
case []byte:
|
|
data = d.([]byte)
|
|
default:
|
|
var err error
|
|
data, err = json.Marshal(d)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
return data
|
|
}
|
|
|
|
func (c *GrpcClient) connect() (err error) {
|
|
op := func() error {
|
|
// connection options
|
|
opts := []grpc.DialOption{
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
grpc.WithChainUnaryInterceptor(middlewares.GetGrpcClientAuthTokenUnaryChainInterceptor()),
|
|
grpc.WithChainStreamInterceptor(middlewares.GetGrpcClientAuthTokenStreamChainInterceptor()),
|
|
}
|
|
|
|
// create new client connection
|
|
c.conn, err = grpc.NewClient(c.address, opts...)
|
|
if err != nil {
|
|
log.Errorf("[GrpcClient] grpc client failed to connect to %s: %v", c.address, err)
|
|
return err
|
|
}
|
|
|
|
// connect
|
|
log.Infof("[GrpcClient] grpc client connecting to %s", c.address)
|
|
c.conn.Connect()
|
|
|
|
// wait for connection to be ready
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
ok := c.conn.WaitForStateChange(ctx, connectivity.Ready)
|
|
if !ok {
|
|
return fmt.Errorf("[GrpcClient] grpc client failed to connect to %s: timed out", c.address)
|
|
}
|
|
|
|
// success
|
|
log.Infof("[GrpcClient] grpc client connected to %s", c.address)
|
|
|
|
return nil
|
|
}
|
|
b := backoff.NewExponentialBackOff(
|
|
backoff.WithInitialInterval(5*time.Second),
|
|
backoff.WithMaxElapsedTime(10*time.Minute),
|
|
)
|
|
n := func(err error, duration time.Duration) {
|
|
log.Errorf("[GrpcClient] grpc client failed to connect to %s: %v, retrying in %s", c.address, err, duration)
|
|
}
|
|
return backoff.RetryNotify(op, b, n)
|
|
}
|
|
|
|
func newGrpcClient() (c *GrpcClient) {
|
|
return &GrpcClient{
|
|
address: utils.GetGrpcAddress(),
|
|
timeout: 10 * time.Second,
|
|
stop: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
var _client *GrpcClient
|
|
var _clientOnce sync.Once
|
|
|
|
func GetGrpcClient() *GrpcClient {
|
|
_clientOnce.Do(func() {
|
|
_client = newGrpcClient()
|
|
go func() {
|
|
err := _client.Start()
|
|
if err != nil {
|
|
log.Fatalf("[GrpcClient] failed to start: %v", err)
|
|
}
|
|
}()
|
|
})
|
|
return _client
|
|
}
|