mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
fix(notify): change the delivery format of config and eliminate some redundant steps of sending messages
This commit is contained in:
@@ -58,10 +58,7 @@ type SBatchSendParams struct {
|
||||
|
||||
type IServiceConfigStore interface {
|
||||
GetConfigs(service string) ([]SConfig, error)
|
||||
GetConfig(service, domainId string) (SConfig, error)
|
||||
SetConfig(service string, config SConfig) error
|
||||
HasSystemConfig(service string) (bool, error)
|
||||
BatchCheckConfig(service string, domainIds []string) ([]bool, error)
|
||||
}
|
||||
|
||||
type SNotification struct {
|
||||
|
||||
+18
-33
@@ -95,7 +95,7 @@ func (cm *SConfigManager) ValidateCreateData(ctx context.Context, userCred mccli
|
||||
if input.Content == nil {
|
||||
return input, httperrors.NewMissingParameterError("content")
|
||||
}
|
||||
config, err := cm.Config(input.Type, input.ProjectDomainId)
|
||||
config, err := cm.Config(input.Type, input.ProjectDomainId, input.Attribution)
|
||||
if err == nil && config != nil {
|
||||
return input, httperrors.NewDuplicateResourceError("duplicate type %q", input.Type)
|
||||
}
|
||||
@@ -171,10 +171,7 @@ func (c *SConfig) PostCreate(ctx context.Context, userCred mcclient.TokenCredent
|
||||
log.Errorf("unable to unmarshal: %v", err)
|
||||
return
|
||||
}
|
||||
NotifyService.AddConfig(ctx, c.Type, notifyv2.SConfig{
|
||||
Config: configMap,
|
||||
DomainId: c.DomainId,
|
||||
})
|
||||
NotifyService.AddConfig(ctx, c.Type, c.Config())
|
||||
err = c.StartRepullSubcontactTask(ctx, userCred)
|
||||
if err != nil {
|
||||
log.Errorf("unable to StartRepullSubcontactTask: %v", err)
|
||||
@@ -201,7 +198,7 @@ func (c *SConfig) PostUpdate(ctx context.Context, userCred mcclient.TokenCredent
|
||||
|
||||
func (c *SConfig) PreDelete(ctx context.Context, userCred mcclient.TokenCredential) {
|
||||
c.SStandaloneResourceBase.PreDelete(ctx, userCred)
|
||||
NotifyService.DeleteConfig(ctx, c.Type, c.DomainId)
|
||||
NotifyService.DeleteConfig(ctx, c.Type, c.Config().DomainId)
|
||||
}
|
||||
func (c *SConfig) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) {
|
||||
err := c.StartRepullSubcontactTask(ctx, userCred)
|
||||
@@ -534,10 +531,10 @@ func (self *SConfigManager) Configs(contactType string) ([]SConfig, error) {
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
func (self *SConfigManager) Config(contactType, domainId string) (*SConfig, error) {
|
||||
func (self *SConfigManager) Config(contactType, domainId string, attribution string) (*SConfig, error) {
|
||||
q := self.Query()
|
||||
q = q.Equals("type", contactType)
|
||||
if len(domainId) == 0 {
|
||||
if attribution == api.CONFIG_ATTRIBUTION_SYSTEM {
|
||||
q = q.Equals("attribution", api.CONFIG_ATTRIBUTION_SYSTEM)
|
||||
} else {
|
||||
q = q.Equals("domain_id", domainId).Equals("attribution", api.CONFIG_ATTRIBUTION_DOMAIN)
|
||||
@@ -590,35 +587,11 @@ func (self *SConfigManager) GetConfigs(contactType string) ([]notifyv2.SConfig,
|
||||
}
|
||||
ret := make([]notifyv2.SConfig, 0, len(configs))
|
||||
for i := range configs {
|
||||
c := make(map[string]string)
|
||||
err := configs[i].Content.Unmarshal(&c)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fail unmarshal config content")
|
||||
}
|
||||
ret = append(ret, notifyv2.SConfig{
|
||||
Config: c,
|
||||
DomainId: configs[i].DomainId,
|
||||
})
|
||||
ret = append(ret, configs[i].Config())
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SConfigManager) GetConfig(contactType, domainId string) (notifyv2.SConfig, error) {
|
||||
config, err := self.Config(contactType, domainId)
|
||||
if err != nil {
|
||||
return notifyv2.SConfig{}, err
|
||||
}
|
||||
ret := make(map[string]string)
|
||||
err = config.Content.Unmarshal(&ret)
|
||||
if err != nil {
|
||||
return notifyv2.SConfig{}, errors.Wrap(err, "fail unmarshal config content")
|
||||
}
|
||||
return notifyv2.SConfig{
|
||||
Config: ret,
|
||||
DomainId: config.DomainId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (self *SConfigManager) SetConfig(contactType string, config notifyv2.SConfig) error {
|
||||
content := jsonutils.Marshal(config.Config)
|
||||
sConfig := &SConfig{
|
||||
@@ -634,6 +607,18 @@ func (self *SConfigManager) SetConfig(contactType string, config notifyv2.SConfi
|
||||
return self.TableSpec().InsertOrUpdate(context.Background(), sConfig)
|
||||
}
|
||||
|
||||
func (self *SConfig) Config() notifyv2.SConfig {
|
||||
c := make(map[string]string)
|
||||
_ = self.Content.Unmarshal(&c)
|
||||
sc := notifyv2.SConfig{
|
||||
Config: c,
|
||||
}
|
||||
if self.Attribution == api.CONFIG_ATTRIBUTION_DOMAIN {
|
||||
sc.DomainId = self.DomainId
|
||||
}
|
||||
return sc
|
||||
}
|
||||
|
||||
func intersection(sa1, sa2 []string) []string {
|
||||
set1 := sets.NewString(sa1...)
|
||||
set2 := sets.NewString(sa2...)
|
||||
|
||||
+2
-68
@@ -41,7 +41,7 @@ import (
|
||||
const (
|
||||
// ErrSendServiceNotFound means SRpcService's SendSerivces hasn't this Send Service.
|
||||
ErrSendServiceNotFound = errors.Error("No such send service")
|
||||
ErrSendServiceNotInit = errors.Error("Send service hasn't been init")
|
||||
// ErrSendServiceNotInit = errors.Error("Send service hasn't been init")
|
||||
)
|
||||
|
||||
// SRpcService provide rpc service about sending message for notify module and manage these services.
|
||||
@@ -134,53 +134,8 @@ func (self *SRpcService) BatchSend(ctx context.Context, contactType string, args
|
||||
if len(args.RemoteTemplate) == 0 && contactType == api.MOBILE {
|
||||
return nil, fmt.Errorf("empty remote template for mobile type notification")
|
||||
}
|
||||
domainIds := make([]string, len(args.Receivers))
|
||||
for i := range domainIds {
|
||||
domainIds[i] = args.Receivers[i].DomainId
|
||||
}
|
||||
checks, err := self.configStore.BatchCheckConfig(contactType, domainIds)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "BatchCheckConfig")
|
||||
}
|
||||
hasSystem, err := self.configStore.HasSystemConfig(contactType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "HasSystemConfig")
|
||||
}
|
||||
ret := make([]*apis.FailedRecord, 0)
|
||||
receiverIndex := 0
|
||||
for i := range checks {
|
||||
if checks[i] {
|
||||
receiverIndex++
|
||||
continue
|
||||
}
|
||||
if hasSystem {
|
||||
args.Receivers[receiverIndex].DomainId = ""
|
||||
receiverIndex++
|
||||
continue
|
||||
}
|
||||
ret = append(ret, &apis.FailedRecord{
|
||||
Receiver: args.Receivers[i],
|
||||
Reason: fmt.Sprintf("no %q config for in domain %q and system", contactType, domainIds[i]),
|
||||
})
|
||||
args.Receivers = append(args.Receivers[:receiverIndex], args.Receivers[receiverIndex+1:]...)
|
||||
}
|
||||
f := func(service *apis.SendNotificationClient) (interface{}, error) {
|
||||
// check ready
|
||||
domainIds := make([]string, len(args.Receivers))
|
||||
for i := range domainIds {
|
||||
domainIds[i] = args.Receivers[i].DomainId
|
||||
}
|
||||
output, err := service.Ready(ctx, &apis.ReadyInput{DomainIds: domainIds})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !output.Ok {
|
||||
// if NOINIT, try to restart server and send again
|
||||
service, err = self.restartService(ctx, contactType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "restart service %s failed", contactType)
|
||||
}
|
||||
}
|
||||
return service.BatchSend(ctx, &args)
|
||||
}
|
||||
|
||||
@@ -290,28 +245,7 @@ func (self *SRpcService) execute(ctx context.Context, f func(client *apis.SendNo
|
||||
self.closeService(ctx, serviceName)
|
||||
return nil, ErrSendServiceNotFound
|
||||
}
|
||||
|
||||
if st.Message() != ErrSendServiceNotInit.Error() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// if NOINIT, try to restart server and send again
|
||||
sendService, err = self.restartService(ctx, serviceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "restart service %s failed", serviceName)
|
||||
}
|
||||
|
||||
ret, err = f(sendService)
|
||||
if err != nil {
|
||||
st := status.Convert(err)
|
||||
if st.Code() == codes.Unavailable {
|
||||
// sock is bad
|
||||
self.closeService(ctx, serviceName)
|
||||
|
||||
return nil, errors.Wrap(ErrSendServiceNotFound, serviceName)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user