This commit is contained in:
azw 2023-08-13 10:45:53 +08:00
parent 0917867fac
commit f38d0d1419
3 changed files with 37 additions and 33 deletions

54
app.go
View File

@ -9,6 +9,10 @@ import (
"q5" "q5"
) )
type F5App interface {
GetPkgName() string
}
type UserApp interface { type UserApp interface {
GetPkgName() string GetPkgName() string
Init() Init()
@ -16,7 +20,7 @@ type UserApp interface {
UnInit() UnInit()
} }
type F5App struct { type app struct {
nodeId int nodeId int
instanceId int instanceId int
terminated bool terminated bool
@ -39,7 +43,7 @@ type F5App struct {
userApp UserApp userApp UserApp
} }
func (this *F5App) init(userApp UserApp) { func (this *app) init(userApp UserApp) {
this.userApp = userApp this.userApp = userApp
this.nowTime = time.Now() this.nowTime = time.Now()
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano()) atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
@ -71,14 +75,14 @@ func (this *F5App) init(userApp UserApp) {
this.userApp.Init() this.userApp.Init()
} }
func (this *F5App) unInit() { func (this *app) unInit() {
this.chGoLoopTimerExit <- 1 this.chGoLoopTimerExit <- 1
_Timer.UnInit() _Timer.UnInit()
_Timer = nil _Timer = nil
this.userApp.UnInit() this.userApp.UnInit()
} }
func (this *F5App) run() { func (this *app) run() {
for !this.terminated { for !this.terminated {
this.nowTime = time.Now() this.nowTime = time.Now()
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano()) atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
@ -96,32 +100,32 @@ func (this *F5App) run() {
} }
} }
func (this *F5App) NewUuid() int64 { func (this *app) NewUuid() int64 {
return 0 return 0
} }
func (this *F5App) GetInstanceId() uint32 { func (this *app) GetInstanceId() uint32 {
return uint32(this.instanceId) return uint32(this.instanceId)
} }
func (this *F5App) GetNodeId() uint32 { func (this *app) GetNodeId() uint32 {
return uint32(this.nodeId) return uint32(this.nodeId)
} }
func (this *F5App) GetPkgName() string { func (this *app) GetPkgName() string {
return this.pkgName return this.pkgName
} }
func (this *F5App) SetPkgName(pkgName string) { func (this *app) SetPkgName(pkgName string) {
this.pkgName = pkgName this.pkgName = pkgName
} }
func (this *F5App) HasFlag(flag int32) bool { func (this *app) HasFlag(flag int32) bool {
_, ok := this.flags[flag] _, ok := this.flags[flag]
return ok return ok
} }
func (this *F5App) AddIMMsg(msgId int16, params q5.Args) { func (this *app) AddIMMsg(msgId int16, params q5.Args) {
p := new(IMMsgNode) p := new(IMMsgNode)
p.msgId = msgId p.msgId = msgId
p.params = params p.params = params
@ -139,11 +143,11 @@ func (this *F5App) AddIMMsg(msgId int16, params q5.Args) {
this.loopCond.Broadcast() this.loopCond.Broadcast()
} }
func (this *F5App) RegisterIMMsgHandle(msgId int16, handle func(int16,q5.Args)) { func (this *app) RegisterIMMsgHandle(msgId int16, handle func(int16,q5.Args)) {
this.imMsgHandlers[msgId] = handle this.imMsgHandlers[msgId] = handle
} }
func (this *F5App) goLoopTimer() { func (this *app) goLoopTimer() {
var waitMs int64 = 1000 * 10 var waitMs int64 = 1000 * 10
for { for {
select { select {
@ -160,33 +164,33 @@ func (this *F5App) goLoopTimer() {
} }
} }
func (this *F5App) schedule() { func (this *app) schedule() {
this.chGoLoopWait <- Timer().GetIdleTime() this.chGoLoopWait <- Timer().GetIdleTime()
this.loopCond.L.Lock() this.loopCond.L.Lock()
this.loopCond.Wait() this.loopCond.Wait()
this.loopCond.L.Unlock() this.loopCond.L.Unlock()
} }
func (this *F5App) NotifyLoopCond() { func (this *app) NotifyLoopCond() {
this.loopCond.Broadcast() this.loopCond.Broadcast()
} }
func (this *F5App) NowUnix() int64 { func (this *app) NowUnix() int64 {
return this.nowUnixNano / int64(time.Second) return this.nowUnixNano / int64(time.Second)
} }
func (this *F5App) NowUnixMilli() int64 { func (this *app) NowUnixMilli() int64 {
return this.nowUnixNano / int64(time.Millisecond) return this.nowUnixNano / int64(time.Millisecond)
} }
func (this *F5App) NowUnixNano() int64 { func (this *app) NowUnixNano() int64 {
return this.nowUnixNano return this.nowUnixNano
} }
func (this *F5App) outputRuningLog() { func (this *app) outputRuningLog() {
} }
func (this *F5App) dispatchIMMsg() { func (this *app) dispatchIMMsg() {
this.imMsgMutex.Lock() this.imMsgMutex.Lock()
this.imWorkNode = this.imTopNode this.imWorkNode = this.imTopNode
this.imTopNode = nil this.imTopNode = nil
@ -202,15 +206,15 @@ func (this *F5App) dispatchIMMsg() {
} }
} }
func (this *F5App) installTimer() { func (this *app) installTimer() {
Timer().SetInterval(1000 * 60, Timer().SetInterval(1000 * 60,
func (ev int32, params *q5.Args) { func (ev int32, params *q5.Args) {
if ev == q5.TIMER_EXEC_EVENT { if ev == q5.TIMER_EXEC_EVENT {
SysLog().Info("max_run_delay:%d max_schedule_time:%d", SysLog().Info("max_run_delay:%d max_schedule_time:%d",
App().maxRunDelay, _app.maxRunDelay,
App().maxScheduleTime) _app.maxScheduleTime)
App().maxRunDelay = 0 _app.maxRunDelay = 0
App().maxScheduleTime = 0 _app.maxScheduleTime = 0
} }
}) })
} }

View File

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

View File

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