From 5f89f207770b0ed770a1fee80dcd3869bb2c0e60 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 10 May 2024 17:23:20 +0800 Subject: [PATCH] 1 --- server/mailserver/app/app.go | 11 +++- server/mailserver/common/types.go | 1 + server/mailserver/mail/mailmgr.go | 87 ++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/server/mailserver/app/app.go b/server/mailserver/app/app.go index 4daf02e8..2e440c3e 100644 --- a/server/mailserver/app/app.go +++ b/server/mailserver/app/app.go @@ -5,6 +5,7 @@ import ( "main/constant" "mt" "main/middleware" + . "main/global" ) type app struct { @@ -49,8 +50,16 @@ func (this *app) registerDataSources() { mt.Table.MailDb.GetById(0).GetPasswd(), mt.Table.MailDb.GetById(0).GetDatabase(), 3) + f5.GetJsStyleDb().RegisterDataSource( + constant.MAIL_DB, + mt.Table.MailDb.GetById(0).GetHost(), + mt.Table.MailDb.GetById(0).GetPort(), + mt.Table.MailDb.GetById(0).GetUser(), + mt.Table.MailDb.GetById(0).GetPasswd(), + mt.Table.MailDb.GetById(0).GetDatabase(), + 1) } func (this *app) HasTask() bool { - return false + return GetMailMgr().HasTask() } diff --git a/server/mailserver/common/types.go b/server/mailserver/common/types.go index c6fc1bd0..8759930b 100644 --- a/server/mailserver/common/types.go +++ b/server/mailserver/common/types.go @@ -58,4 +58,5 @@ type MailMgr interface{ CaGetUnreadMailCnt(c *gin.Context) CaGetAttachment(c *gin.Context) CaDeleteMails(c *gin.Context) + HasTask() bool } diff --git a/server/mailserver/mail/mailmgr.go b/server/mailserver/mail/mailmgr.go index b4616c9b..b5cfcbf9 100644 --- a/server/mailserver/mail/mailmgr.go +++ b/server/mailserver/mail/mailmgr.go @@ -10,6 +10,13 @@ import ( "github.com/gin-gonic/gin" ) +type dbEvent struct { + idx int64 + eventName string + param1 string + entry q5.ListHead +} + type userGroup struct { groupId int64 userHash sync.Map @@ -23,9 +30,13 @@ type mailMgr struct { groupHash sync.Map //int64 => *userGroup lastSyncEventIdx int64 pullingEvent bool + procingEvent bool + eventQueue q5.Queue + timerWp *f5.TimerWp } func (this *mailMgr) Init() { + this.eventQueue.Init() this.syncEvent() this.loadMails() this.loadGroups() @@ -35,9 +46,18 @@ func (this *mailMgr) Init() { f5.GetApp().RegisterCaHandle("Mail", "getUnreadMailCnt", this.CaGetUnreadMailCnt) f5.GetApp().RegisterCaHandle("Mail", "getAttachment", this.CaGetAttachment) f5.GetApp().RegisterCaHandle("Mail", "deleteMails", this.CaDeleteMails) + this.timerWp = f5.GetTimer().SetIntervalWp( + 300, + func (e int32, args* q5.Args) { + if e == q5.TIMER_EXEC_EVENT { + this.updateDbEvent() + } + }) } func (this *mailMgr) UnInit() { + f5.GetTimer().Delete(this.timerWp) + this.timerWp = nil } func (this *mailMgr) loadMails() { @@ -306,21 +326,84 @@ func (this *mailMgr) syncEvent() { }) } +func (this *mailMgr) addDbEvent(e *dbEvent) { + this.eventQueue.Push(&e.entry) +} + +func (this *mailMgr) updateDbEvent() { + if this.eventQueue.IsEmpty() { + return + } + if this.procingEvent { + return + } + this.eventQueue.Fetch() + for !this.eventQueue.WorkList.Empty() { + e := this.eventQueue.WorkList.FirstEntry().(*dbEvent) + if e.eventName == constant.EVENT_MAIL_UPDATE { + this.procMailUpdate(e) + break + } else if e.eventName == constant.EVENT_UPSER_GROUP_UPDATE { + this.procGroupUpdate(e) + break + } else { + e.entry.DelInit() + } + } +} + func (this *mailMgr) pullEvent() { if this.pullingEvent { return } this.pullingEvent = true - go f5.GetJsStyleDb().SelectCustomQuery( + f5.GetJsStyleDb().SelectCustomQuery( constant.MAIL_DB, fmt.Sprintf("SELECT * FROM t_event WHERE idx > %d;", this.lastSyncEventIdx), func(err error, ds *f5.DataSet) { - this.pullingEvent = false if err != nil { + this.pullingEvent = false return } for ds.Next() { + e := new(dbEvent) + e.idx = q5.ToInt64(ds.GetByName("idx")) + e.eventName = ds.GetByName("event_name") + e.param1 = ds.GetByName("param1") + e.entry.Init(e) + _mailMgr.addDbEvent(e) this.lastSyncEventIdx = q5.ToInt64(ds.GetByName("idx")) } + this.pullingEvent = false + }) +} + +func (this* mailMgr) HasTask() bool { + return false +} + +func (this* mailMgr) procMailUpdate(e *dbEvent) { + this.procingEvent = true + f5.GetJsStyleDb().OrmSelectOne( + constant.MAIL_DB, + "t_mail", + [][]string{ + {"mail_id", e.param1}, + }, + func (err error, ds *f5.DataSet) { + + }) +} + +func (this* mailMgr) procGroupUpdate(e *dbEvent) { + this.procingEvent = true + f5.GetJsStyleDb().OrmSelectOne( + constant.MAIL_DB, + "t_group", + [][]string{ + {"group_id", e.param1}, + }, + func (err error, ds *f5.DataSet) { + }) }