finclip-app-manager/domain/service/license_check.go

126 lines
3.0 KiB
Go
Raw Normal View History

2023-10-31 14:07:26 +08:00
package service
import (
"context"
"finclip-app-manager/domain/entity"
"finclip-app-manager/infrastructure/config"
impl "finclip-app-manager/infrastructure/db/repo"
"gitlab.finogeeks.club/finclip-backend/apm"
"sync"
"time"
)
const (
LICENSE_VALID_EXPIRE = 60
LICENSE_IS_VALID = 0
BUNDLE_ID_COUNT_INVAILD = 1
)
var LicenseValid int32
var mutex = sync.Mutex{}
func GetLicenseValid() int32 {
result := LicenseValid
return result
}
func LicenceCheckStart() {
//LICENSE_VALID_EXPIRE跑一次
go func() {
updateLicenseValid(context.Background())
t := time.NewTicker(time.Second * LICENSE_VALID_EXPIRE)
for {
select {
case <-t.C:
func() {
span, ctx := apm.ApmClient().CreateLocalSpan(context.Background())
span.SetOperationName("updateLicenseValid")
defer span.End()
go updateLicenseValid(ctx)
}()
}
}
}()
}
func updateLicenseValid(ctx context.Context) {
var upErr error = nil
defer func(e error) {
if e != nil {
log.Debugf("updateLicenseValid panic err:%s", e.Error())
upValid(LICENSE_IS_VALID)
}
}(upErr)
licenseInfo, err := hCaller.GetLicense(ctx)
if err != nil {
upErr = err
log.Errorf("updateLicenseValid get license info err:%s", err.Error())
return
}
log.Debugf("updateLicenseValid get license info:%+v", licenseInfo)
bindingTotal, err := getBindingNum(ctx)
if err != nil {
upErr = err
log.Errorf("updateLicenseValid get binding count err:%s", err.Error())
return
}
log.Debugf("updateLicenseValid get binding count:%d", bindingTotal)
bundleIdTotal, err := getBundleIdNum(ctx)
if err != nil {
upErr = err
log.Errorf("updateLicenseValid get bundle id count err:%s", err.Error())
return
}
log.Debugf("updateLicenseValid get bundle id count:%d", bundleIdTotal)
//获取小程序数量
appNum, err := getAppNum(ctx)
if err != nil {
upErr = err
log.Errorf("updateLicenseValid get app count err:%s", err.Error())
return
}
//校验应用数量
if (licenseInfo.BundleIdCount < bundleIdTotal) ||
(licenseInfo.CooAppCount < bindingTotal) ||
(licenseInfo.AppCount < appNum) {
log.Errorf("updateLicenseValid bundle id or app num over limit,"+
"license info:%+v,bundle id num:%d,appNum:%d", licenseInfo, bundleIdTotal, appNum)
upValid(BUNDLE_ID_COUNT_INVAILD)
} else {
upValid(LICENSE_IS_VALID)
}
}
func upValid(result int32) {
mutex.Lock()
defer mutex.Unlock()
LicenseValid = result
log.Debugf("upValid valid:%d", LicenseValid)
}
func getAppNum(ctx context.Context) (int, error) {
appRepo := impl.InitAppRepo()
count, err := appRepo.AppCount(ctx, "")
if err != nil {
log.Errorf("license valid update err:%s", err.Error())
}
return count, err
}
func getBindingNum(ctx context.Context) (int, error) {
bindingRepo := impl.InitBindingRepo()
return bindingRepo.GetCountByStatus(ctx, "Valid", entity.BINGING_PLATFORM_ALL)
}
func getBundleIdNum(ctx context.Context) (int, error) {
if config.GetConfig().IsPrivateEnv() {
return impl.InitBundleRepo().AllCount(ctx)
}
return 0, nil
}