123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- package service
- import (
- "context"
- "fmt"
- "sync"
- "go-common/app/admin/ep/saga/conf"
- "go-common/app/admin/ep/saga/model"
- "go-common/library/log"
- )
- const _baseBranch = "master"
- // syncalldataproc ...
- func (s *Service) syncweekdataproc() {
- //the blow object will be sync or update all time data
- go s.SyncMember()
- go s.SyncRunners()
- go s.SyncContacts(context.TODO())
- }
- // syncalldataproc ...
- func (s *Service) syncalldataproc() {
- if err := s.SyncBranch(); err != nil {
- return
- }
- // AggregateBranch base on branch, if SyncBranch err, so return
- s.AggregateBranch()
- }
- // syncdataproc ...
- func (s *Service) syncdataproc() {
- var (
- wg sync.WaitGroup
- done = func(f func() error) {
- defer wg.Done()
- f()
- }
- )
- wg.Add(2)
- go done(s.SyncIssues)
- go done(s.SyncPipelines)
- wg.Wait()
- wg.Add(1)
- go done(s.SyncCommit)
- wg.Wait()
- wg.Add(2)
- go done(s.SyncJobs)
- go done(s.SyncMR)
- wg.Wait()
- wg.Add(3)
- go done(s.SyncMRNote)
- go done(s.SyncMRAwardEmoji)
- go done(s.SyncMRDiscussion)
- wg.Wait()
- s.AggregateMR()
- }
- // SyncCommit ...
- func (s *Service) SyncCommit() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- allPage int
- allNum int
- result *model.SyncResult
- )
- log.Info("===================== SyncCommit start ========================")
- for _, projectID := range projectIDs {
- if result, err = s.SyncProjectCommit(projectID); err != nil {
- log.Error("SyncCommit projectID(%d), err(%+v)", projectID, err)
- go s.WechatFailData(model.DataTypeCommit, projectID, result, err)
- return
- }
- log.Info(">>>>>>>>> SyncCommit projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)
- if result != nil && len(result.FailData) > 0 {
- go s.WechatFailData(model.DataTypeCommit, projectID, result, nil)
- }
- allPage = allPage + result.TotalPage
- allNum = allNum + result.TotalNum
- }
- log.Info("===================== SyncCommit finished totalPage(%d), totalNum(%d)========================", allPage, allNum)
- return
- }
- // SyncMR ...
- func (s *Service) SyncMR() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- allPage int
- allNum int
- result *model.SyncResult
- )
- log.Info("===================== SyncMR start ========================")
- for _, projectID := range projectIDs {
- if result, err = s.SyncProjectMR(context.TODO(), projectID); err != nil {
- log.Error("SyncMR projectID(%d), err(%+v)", projectID, err)
- go s.WechatFailData(model.DataTypeMR, projectID, result, err)
- return
- }
- log.Info(">>>>>>>>> SyncMR projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)
- if result != nil && len(result.FailData) > 0 {
- go s.WechatFailData(model.DataTypeMR, projectID, result, nil)
- }
- allPage = allPage + result.TotalPage
- allNum = allNum + result.TotalNum
- }
- log.Info("===================== SyncMR finished totalPage(%d), totalNum(%d)========================", allPage, allNum)
- return
- }
- // AggregateMR ...
- func (s *Service) AggregateMR() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- )
- log.Info("===================== AggMR start ========================")
- for _, projectID := range projectIDs {
- if err = s.AggregateProjectMR(context.TODO(), projectID); err != nil {
- log.Error("AggMR projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> AggMR projectID(%d) complete", projectID)
- }
- log.Info("===================== AggMR finished ========================")
- return
- }
- // SyncJobs ...
- func (s *Service) SyncJobs() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- allPage int
- allNum int
- result *model.SyncResult
- )
- log.Info("===================== SyncJobs start ========================")
- for _, projectID := range projectIDs {
- if result, err = s.SyncProjectJobs(projectID); err != nil {
- log.Error("SyncJobs projectID(%d), err(%+v)", projectID, err)
- go s.WechatFailData(model.DataTypeJob, projectID, result, err)
- return
- }
- log.Info(">>>>>>>>> SyncJobs projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)
- if result != nil && len(result.FailData) > 0 {
- go s.WechatFailData(model.DataTypeJob, projectID, result, nil)
- }
- allPage = allPage + result.TotalPage
- allNum = allNum + result.TotalNum
- }
- log.Info("===================== SyncJobs finished totalPage(%d), totalNum(%d)========================", allPage, allNum)
- return
- }
- // SyncPipelines ...
- func (s *Service) SyncPipelines() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- allPage int
- allNum int
- result *model.SyncResult
- )
- log.Info("===================== SyncPipelines start ========================")
- for _, projectID := range projectIDs {
- if result, err = s.SyncProjectPipelines(projectID); err != nil {
- log.Error("SyncPipelines projectID(%d), err(%+v)", projectID, err)
- go s.WechatFailData(model.DataTypePipeline, projectID, result, err)
- return
- }
- log.Info(">>>>>>>>> SyncPipelines projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)
- if result != nil && len(result.FailData) > 0 {
- go s.WechatFailData(model.DataTypePipeline, projectID, result, nil)
- }
- allPage = allPage + result.TotalPage
- allNum = allNum + result.TotalNum
- }
- log.Info("===================== SyncPipelines finished totalPage(%d), totalNum(%d)========================", allPage, allNum)
- return
- }
- // SyncMRNote ...
- func (s *Service) SyncMRNote() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- totalPage int
- totalNum int
- page int
- num int
- )
- log.Info("===================== SyncNote start ========================")
- for _, projectID := range projectIDs {
- if page, num, err = s.SyncProjectNotes(context.TODO(), projectID); err != nil {
- log.Error("SyncNotes projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> SyncNote projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)
- totalPage = totalPage + page
- totalNum = totalNum + num
- }
- log.Info("===================== SyncNote finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)
- return
- }
- // SyncMember ...
- func (s *Service) SyncMember() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- totalPage int
- totalNum int
- page int
- num int
- )
- log.Info("===================== SyncMember start ========================")
- for _, projectID := range projectIDs {
- if page, num, err = s.SyncProjectMember(context.TODO(), projectID); err != nil {
- log.Error("SyncMember projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> SyncMember projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)
- totalPage = totalPage + page
- totalNum = totalNum + num
- }
- log.Info("===================== SyncMember finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)
- return
- }
- // SyncMRAwardEmoji ...
- func (s *Service) SyncMRAwardEmoji() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- totalPage int
- totalNum int
- page int
- num int
- )
- log.Info("===================== SyncMRAwardEmoji start ========================")
- for _, projectID := range projectIDs {
- if page, num, err = s.SyncProjectAwardEmoji(context.TODO(), projectID); err != nil {
- log.Error("SyncMRAwardEmoji projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> SyncMRAwardEmoji projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)
- totalPage = totalPage + page
- totalNum = totalNum + num
- }
- log.Info("===================== SyncMRAwardEmoji finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)
- return
- }
- // SyncMRDiscussion ...
- func (s *Service) SyncMRDiscussion() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- totalPage int
- totalNum int
- page int
- num int
- )
- log.Info("===================== SyncMRDiscussion start ========================")
- for _, projectID := range projectIDs {
- if page, num, err = s.SyncProjectDiscussion(context.TODO(), projectID); err != nil {
- log.Error("SyncMRDiscussion projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> SyncMRDiscussion projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)
- totalPage = totalPage + page
- totalNum = totalNum + num
- }
- log.Info("===================== SyncMRDiscussion finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)
- return
- }
- // SyncRunners ...
- func (s *Service) SyncRunners() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- totalPage int
- totalNum int
- page int
- num int
- )
- log.Info("===================== SyncRunners start ========================")
- for _, projectID := range projectIDs {
- if page, num, err = s.SyncAllRunners(projectID); err != nil {
- log.Error("SyncRunners projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> SyncRunners projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)
- totalPage = totalPage + page
- totalNum = totalNum + num
- }
- log.Info("===================== SyncRunners finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)
- return
- }
- // SyncIssues ...
- func (s *Service) SyncIssues() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- totalPage int
- totalNum int
- page int
- num int
- )
- log.Info("===================== SyncIssues start ========================")
- for _, projectID := range projectIDs {
- if page, num, err = s.SyncAllIssues(projectID); err != nil {
- log.Error("SyncIssues projectID(%d), err(%+v)", projectID, err)
- return
- }
- log.Info(">>>>>>>>> SyncIssues projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)
- totalPage = totalPage + page
- totalNum = totalNum + num
- }
- log.Info("===================== SyncIssues finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)
- return
- }
- // SyncBranch ...
- func (s *Service) SyncBranch() (err error) {
- var (
- projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- allPage int
- allNum int
- result *model.SyncResult
- )
- log.Info("===================== SyncBranch start ========================")
- for _, projectID := range projectIDs {
- if result, err = s.SyncProjectBranch(context.TODO(), projectID); err != nil {
- log.Error("SyncBranch projectID(%d), err(%+v)", projectID, err)
- go s.WechatFailData(model.DataTypeBranch, projectID, result, err)
- return
- }
- log.Info(">>>>>>>>> SyncBranch projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)
- if result != nil && len(result.FailData) > 0 {
- go s.WechatFailData(model.DataTypeBranch, projectID, result, nil)
- }
- allPage = allPage + result.TotalPage
- allNum = allNum + result.TotalNum
- }
- log.Info("===================== SyncBranch finished totalPage(%d), totalNum(%d)========================", allPage, allNum)
- return
- }
- // AggregateBranch ...
- func (s *Service) AggregateBranch() (err error) {
- var projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
- log.Info("===================== SyncAggregateBranch start ========================")
- for _, projectID := range projectIDs {
- if err = s.AggregateProjectBranch(context.TODO(), projectID, _baseBranch); err != nil {
- log.Error("SyncAggregateBranch projectID(%d), err(%+v)", projectID, err)
- return
- }
- }
- log.Info("===================== SyncAggregateBranch finished ========================")
- return
- }
- // WechatFailData ...
- func (s *Service) WechatFailData(dataType string, projectID int, result *model.SyncResult, error error) (err error) {
- var (
- ctx = context.Background()
- users = conf.Conf.Property.SyncData.WechatUser
- content string
- )
- title := "[SAGA-ADMIN] SYNC ERROR !!!"
- subject := fmt.Sprintf("%s\n\nDataType ( %s )\nProjectID ( %d )", title, dataType, projectID)
- if error != nil {
- return s.wechat.PushMsg(ctx, users, fmt.Sprintf("%s\n\n%s", subject, error.Error()))
- }
- if result == nil || len(result.FailData) <= 0 {
- return
- }
- content = "LIST : \n"
- for _, data := range result.FailData {
- if dataType == model.DataTypeJob || dataType == model.DataTypePipeline || dataType == model.DataTypeMR {
- content += fmt.Sprintf("%d, ", data.ChildID)
- } else if dataType == model.DataTypeCommit || dataType == model.DataTypeBranch {
- content += fmt.Sprintf("%s\n", data.ChildIDStr)
- }
- }
- return s.wechat.PushMsg(ctx, users, fmt.Sprintf("%s\n\n%s", subject, content))
- }
|