作者 郑周

1. 项目启动,增加并发锁

@@ -2,6 +2,7 @@ package service @@ -2,6 +2,7 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis"
5 "strconv" 6 "strconv"
6 "strings" 7 "strings"
7 "time" 8 "time"
@@ -482,6 +483,15 @@ func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (inter @@ -482,6 +483,15 @@ func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (inter
482 } 483 }
483 484
484 func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) (interface{}, error) { 485 func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) (interface{}, error) {
  486 + lock := xredis.NewLockProjectId(int(in.Id))
  487 + err := lock.Lock()
  488 + if err != nil {
  489 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  490 + }
  491 + defer func() {
  492 + lock.UnLock()
  493 + }()
  494 +
485 transactionContext, err := factory.ValidateStartTransaction(in) 495 transactionContext, err := factory.ValidateStartTransaction(in)
486 if err != nil { 496 if err != nil {
487 return nil, err 497 return nil, err
@@ -562,10 +572,14 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) @@ -562,10 +572,14 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand)
562 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 572 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
563 } 573 }
564 574
  575 + tasks, err := taskRepository.Find(map[string]interface{}{"projectId": project.Id})
  576 + if err != nil {
  577 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  578 + }
  579 + if len(tasks) == 0 {
565 now := time.Now().Local() 580 now := time.Now().Local()
566 year, month, day := now.Date() 581 year, month, day := now.Date()
567 nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻 582 nowO := time.Date(year, month, day, 0, 0, 0, 0, time.Local) // 当前时间0点0分0秒时刻
568 -  
569 for i := range project.Template.LinkNodes { 583 for i := range project.Template.LinkNodes {
570 node := project.Template.LinkNodes[i] 584 node := project.Template.LinkNodes[i]
571 task := &domain.NodeTask{ 585 task := &domain.NodeTask{
@@ -611,6 +625,7 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) @@ -611,6 +625,7 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand)
611 if err != nil { 625 if err != nil {
612 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 626 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
613 } 627 }
  628 + }
614 629
615 if err := transactionContext.CommitTransaction(); err != nil { 630 if err := transactionContext.CommitTransaction(); err != nil {
616 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 631 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  1 +package xredis
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/go-redsync/redsync/v4"
  7 +)
  8 +
  9 +type LockProjectId struct {
  10 + m *redsync.Mutex
  11 +}
  12 +
  13 +func NewLockProjectId(id int) *LockProjectId {
  14 + key := fmt.Sprintf("performance:project_id:%d", id)
  15 + return &LockProjectId{
  16 + m: rsync.NewMutex(key),
  17 + }
  18 +}
  19 +
  20 +func (lk *LockProjectId) Lock() error {
  21 + return lk.m.Lock()
  22 +}
  23 +
  24 +func (lk *LockProjectId) UnLock() (bool, error) {
  25 + return lk.m.Unlock()
  26 +}