github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/calcium.go (about)

     1  package calcium
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/panjf2000/ants/v2"
     9  	"github.com/projecteru2/core/cluster"
    10  	"github.com/projecteru2/core/discovery"
    11  	"github.com/projecteru2/core/discovery/helium"
    12  	"github.com/projecteru2/core/log"
    13  	"github.com/projecteru2/core/resource"
    14  	"github.com/projecteru2/core/resource/cobalt"
    15  	"github.com/projecteru2/core/source"
    16  	"github.com/projecteru2/core/source/github"
    17  	"github.com/projecteru2/core/source/gitlab"
    18  	"github.com/projecteru2/core/store"
    19  	storefactory "github.com/projecteru2/core/store/factory"
    20  	"github.com/projecteru2/core/types"
    21  	"github.com/projecteru2/core/utils"
    22  	"github.com/projecteru2/core/wal"
    23  )
    24  
    25  // Calcium implement the cluster
    26  type Calcium struct {
    27  	config     types.Config
    28  	store      store.Store
    29  	rmgr       resource.Manager
    30  	source     source.Source
    31  	watcher    discovery.Service
    32  	wal        wal.WAL
    33  	pool       *ants.PoolWithFunc
    34  	identifier string
    35  }
    36  
    37  // New returns a new cluster config
    38  func New(ctx context.Context, config types.Config, t *testing.T) (*Calcium, error) {
    39  	logger := log.WithFunc("calcium.New")
    40  	// set store
    41  	store, err := storefactory.NewStore(config, t)
    42  	if err != nil {
    43  		logger.Error(ctx, err)
    44  		return nil, err
    45  	}
    46  
    47  	// set scm
    48  	var scm source.Source
    49  	scmtype := strings.ToLower(config.Git.SCMType)
    50  	switch scmtype {
    51  	case cluster.Gitlab:
    52  		scm, err = gitlab.New(config)
    53  	case cluster.Github:
    54  		scm, err = github.New(config)
    55  	default:
    56  		logger.Warn(ctx, "SCM not set, build API disabled")
    57  	}
    58  	if err != nil {
    59  		logger.Error(ctx, err, "SCM failed")
    60  		return nil, err
    61  	}
    62  
    63  	// set watcher
    64  	watcher := helium.New(ctx, config.GRPCConfig, store)
    65  
    66  	// set resource plugin manager
    67  	rmgr, err := cobalt.New(config)
    68  	if err != nil {
    69  		logger.Error(ctx, err)
    70  		return nil, err
    71  	}
    72  	if err := rmgr.LoadPlugins(ctx, t); err != nil {
    73  		logger.Error(ctx, err)
    74  		return nil, err
    75  	}
    76  
    77  	// init pool
    78  	pool, err := utils.NewPool(config.MaxConcurrency)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	cal := &Calcium{store: store, config: config, source: scm, watcher: watcher, rmgr: rmgr, pool: pool}
    84  
    85  	cal.wal, err = enableWAL(config, cal, store)
    86  	if err != nil {
    87  		logger.Error(ctx, err)
    88  		return nil, err
    89  	}
    90  
    91  	cal.identifier, err = config.Identifier()
    92  	if err != nil {
    93  		logger.Error(ctx, err)
    94  		return nil, err
    95  	}
    96  
    97  	return cal, pool.Invoke(func() { cal.InitMetrics(ctx) })
    98  }
    99  
   100  // DisasterRecover .
   101  func (c *Calcium) DisasterRecover(ctx context.Context) {
   102  	c.wal.Recover(ctx)
   103  }
   104  
   105  // Finalizer use for defer
   106  func (c *Calcium) Finalizer() {
   107  	// TODO some resource recycle
   108  	c.pool.Release()
   109  }
   110  
   111  // GetIdentifier returns the identifier of calcium
   112  func (c *Calcium) GetIdentifier() string {
   113  	return c.identifier
   114  }
   115  
   116  func (c *Calcium) GetStore() store.Store {
   117  	return c.store
   118  }