148 lines
2.7 KiB
Go
148 lines
2.7 KiB
Go
package f5
|
|
|
|
import (
|
|
)
|
|
|
|
type AsyncTask struct {
|
|
status int32
|
|
debugInfo string
|
|
cb func(*AsyncTask)
|
|
succCb func(*AsyncTask)
|
|
failCb func(*AsyncTask)
|
|
exitCb func(*AsyncTask)
|
|
preExecTimes int64
|
|
execTimes int64
|
|
lockKeys map[string]*taskLock
|
|
}
|
|
|
|
func (this *AsyncTask) init(cb func(*AsyncTask)) *AsyncTask {
|
|
this.cb = cb
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) GetExecTimes() int64 {
|
|
return this.execTimes
|
|
}
|
|
|
|
func (this *AsyncTask) IsRunning() bool {
|
|
return this.status == 0
|
|
}
|
|
|
|
func (this *AsyncTask) IsSucc() bool {
|
|
return this.status > 0
|
|
}
|
|
|
|
func (this *AsyncTask) IsFail() bool {
|
|
return this.status < 0
|
|
}
|
|
|
|
func (this *AsyncTask) SetSucc() {
|
|
if !this.IsRunning() {
|
|
panic("task is not runing")
|
|
}
|
|
this.status = 1
|
|
this.ClearLocks()
|
|
if this.succCb != nil {
|
|
this.succCb(this)
|
|
}
|
|
if this.exitCb != nil {
|
|
this.exitCb(this)
|
|
}
|
|
}
|
|
|
|
func (this *AsyncTask) SetFail() {
|
|
if !this.IsRunning() {
|
|
panic("task is not runing")
|
|
}
|
|
this.status = -1
|
|
this.ClearLocks()
|
|
if this.failCb != nil {
|
|
this.failCb(this)
|
|
}
|
|
if this.exitCb != nil {
|
|
this.exitCb(this)
|
|
}
|
|
}
|
|
|
|
func (this *AsyncTask) Continue() *AsyncTask {
|
|
if !this.IsRunning() {
|
|
panic("task is not runing")
|
|
}
|
|
if len(this.lockKeys) > 0 {
|
|
panic("lockTask not support continue")
|
|
}
|
|
GetApp().RegisterMainThreadCb(
|
|
func () {
|
|
this.cb(this)
|
|
this.execTimes += 1
|
|
})
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) checkDo() *AsyncTask {
|
|
if !this.IsRunning() {
|
|
panic("task is not runing")
|
|
}
|
|
if this.allIsReady() {
|
|
if this.preExecTimes <= 0 {
|
|
this.preExecTimes++
|
|
if this.preExecTimes > 1 {
|
|
panic("locktask preExectimes > 0")
|
|
}
|
|
GetApp().RegisterMainThreadCb(
|
|
func () {
|
|
this.cb(this)
|
|
if this.execTimes > 0 {
|
|
panic("locktask only run once")
|
|
}
|
|
this.execTimes += 1
|
|
})
|
|
}
|
|
}
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) OnSucc(cb func(*AsyncTask)) *AsyncTask {
|
|
this.succCb = cb
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) OnFail(cb func(*AsyncTask)) *AsyncTask {
|
|
this.failCb = cb
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) OnExit(cb func(*AsyncTask)) *AsyncTask {
|
|
this.exitCb = cb
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) allIsReady() bool {
|
|
var allReady = true
|
|
for _, lock := range(this.lockKeys) {
|
|
if !_app.isFirstAsyncTask(lock.key, &lock.entry) {
|
|
allReady = false
|
|
break
|
|
}
|
|
}
|
|
return allReady
|
|
}
|
|
|
|
func (this *AsyncTask) ClearLocks() {
|
|
for _, lock := range(this.lockKeys) {
|
|
lock.entry.DelInit()
|
|
}
|
|
for _, lock := range(this.lockKeys) {
|
|
_app.clearEmptyPendingAsyncTask(lock.key)
|
|
}
|
|
for _, lock := range(this.lockKeys) {
|
|
_app.notifyNextTask(lock.key)
|
|
}
|
|
}
|
|
|
|
func NewAsyncTask(cb func(*AsyncTask)) *AsyncTask {
|
|
p := new(AsyncTask)
|
|
p.lockKeys = make(map[string]*taskLock)
|
|
return p.init(cb).Continue()
|
|
}
|