This commit is contained in:
azw 2023-08-13 10:31:04 +08:00
parent 81321f5d0c
commit 0917867fac
3 changed files with 50 additions and 30 deletions

65
app.go
View File

@ -9,7 +9,14 @@ import (
"q5"
)
type App_ struct {
type UserApp interface {
GetPkgName() string
Init()
Update()
UnInit()
}
type F5App struct {
nodeId int
instanceId int
terminated bool
@ -29,11 +36,11 @@ type App_ struct {
imMsgHandlers [1024]func(int16, q5.Args)
maxRunDelay int64
maxScheduleTime int64
updateFunc func ()
userApp UserApp
}
func (this *App_) Init(updateFunc func ()) {
this.updateFunc = updateFunc
func (this *F5App) init(userApp UserApp) {
this.userApp = userApp
this.nowTime = time.Now()
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
_Timer = &q5.XTimer{}
@ -61,15 +68,17 @@ func (this *App_) Init(updateFunc func ()) {
os.Getpid())
this.installTimer()
go this.goLoopTimer()
this.userApp.Init()
}
func (this *App_) UnInit() {
func (this *F5App) unInit() {
this.chGoLoopTimerExit <- 1
_Timer.UnInit()
_Timer = nil
this.userApp.UnInit()
}
func (this *App_) Run() {
func (this *F5App) run() {
for !this.terminated {
this.nowTime = time.Now()
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
@ -77,7 +86,7 @@ func (this *App_) Run() {
beginTick := q5.GetTickCount()
_Timer.Update()
this.dispatchIMMsg()
this.updateFunc()
this.userApp.Update()
this.schedule()
endTick := q5.GetTickCount()
@ -87,32 +96,32 @@ func (this *App_) Run() {
}
}
func (this *App_) NewUuid() int64 {
func (this *F5App) NewUuid() int64 {
return 0
}
func (this *App_) GetInstanceId() uint32 {
func (this *F5App) GetInstanceId() uint32 {
return uint32(this.instanceId)
}
func (this *App_) GetNodeId() uint32 {
func (this *F5App) GetNodeId() uint32 {
return uint32(this.nodeId)
}
func (this *App_) GetPkgName() string {
func (this *F5App) GetPkgName() string {
return this.pkgName
}
func (this *App_) SetPkgName(pkgName string) {
func (this *F5App) SetPkgName(pkgName string) {
this.pkgName = pkgName
}
func (this *App_) HasFlag(flag int32) bool {
func (this *F5App) HasFlag(flag int32) bool {
_, ok := this.flags[flag]
return ok
}
func (this *App_) AddIMMsg(msgId int16, params q5.Args) {
func (this *F5App) AddIMMsg(msgId int16, params q5.Args) {
p := new(IMMsgNode)
p.msgId = msgId
p.params = params
@ -130,11 +139,11 @@ func (this *App_) AddIMMsg(msgId int16, params q5.Args) {
this.loopCond.Broadcast()
}
func (this *App_) RegisterIMMsgHandle(msgId int16, handle func(int16,q5.Args)) {
func (this *F5App) RegisterIMMsgHandle(msgId int16, handle func(int16,q5.Args)) {
this.imMsgHandlers[msgId] = handle
}
func (this *App_) goLoopTimer() {
func (this *F5App) goLoopTimer() {
var waitMs int64 = 1000 * 10
for {
select {
@ -151,33 +160,33 @@ func (this *App_) goLoopTimer() {
}
}
func (this *App_) schedule() {
func (this *F5App) schedule() {
this.chGoLoopWait <- Timer().GetIdleTime()
this.loopCond.L.Lock()
this.loopCond.Wait()
this.loopCond.L.Unlock()
}
func (this *App_) NotifyLoopCond() {
func (this *F5App) NotifyLoopCond() {
this.loopCond.Broadcast()
}
func (this *App_) NowUnix() int64 {
func (this *F5App) NowUnix() int64 {
return this.nowUnixNano / int64(time.Second)
}
func (this *App_) NowUnixMilli() int64 {
func (this *F5App) NowUnixMilli() int64 {
return this.nowUnixNano / int64(time.Millisecond)
}
func (this *App_) NowUnixNano() int64 {
func (this *F5App) NowUnixNano() int64 {
return this.nowUnixNano
}
func (this *App_) outputRuningLog() {
func (this *F5App) outputRuningLog() {
}
func (this *App_) dispatchIMMsg() {
func (this *F5App) dispatchIMMsg() {
this.imMsgMutex.Lock()
this.imWorkNode = this.imTopNode
this.imTopNode = nil
@ -193,15 +202,15 @@ func (this *App_) dispatchIMMsg() {
}
}
func (this *App_) installTimer() {
func (this *F5App) installTimer() {
Timer().SetInterval(1000 * 60,
func (ev int32, params *q5.Args) {
if ev == q5.TIMER_EXEC_EVENT {
SysLog().Info("max_run_delay:%d max_schedule_time:%d",
App.maxRunDelay,
App.maxScheduleTime)
App.maxRunDelay = 0
App.maxScheduleTime = 0
App().maxRunDelay,
App().maxScheduleTime)
App().maxRunDelay = 0
App().maxScheduleTime = 0
}
})
}

View File

@ -2,7 +2,7 @@ package f5
import "q5"
var App *App_
var app *F5App
var _Timer *q5.XTimer
var _SysLog *SysLog_
var _TgLog *TGLog_
@ -18,3 +18,14 @@ func SysLog() *SysLog_ {
func TgLog() *TGLog_ {
return _TgLog
}
func App() *F5App {
return app
}
func Run(userApp UserApp) {
app = new(F5App)
app.init(userApp)
app.run()
app.unInit()
}

View File

@ -125,7 +125,7 @@ func (this *SysLog_) goSaveToFile() {
this.botNode = nil
this.msgMutex.Unlock()
if workNode != nil {
logDir := fmt.Sprintf(SYS_LOG_ROOT, App.GetPkgName())
logDir := fmt.Sprintf(SYS_LOG_ROOT, App().GetPkgName())
fileName := fmt.Sprintf(TGLOG_FILENAME, os.Getpid(), time.Now().Format("20060102"))
q5.ForceCreateDir(logDir)
if f, err := os.OpenFile(logDir + fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666); err == nil {