改进退出程序时关闭数据库写入

This commit is contained in:
GoEdgeLab
2023-06-23 17:45:39 +08:00
parent 54787a8a8e
commit 67355c85bd
12 changed files with 25 additions and 10 deletions

View File

@@ -504,6 +504,10 @@ func (this *FileListDB) CleanAll() error {
}
func (this *FileListDB) Close() error {
if this.isClosed {
return nil
}
this.isClosed = true
this.isReady = false

View File

@@ -19,7 +19,7 @@ func init() {
return
}
events.On(events.EventQuit, func() {
events.OnClose(func() {
remotelogs.Println("CACHE", "quiting cache manager")
SharedManager.UpdatePolicies([]*serverconfigs.HTTPCachePolicy{})
})

View File

@@ -24,6 +24,11 @@ func On(event Event, callback func()) {
OnKey(event, nil, callback)
}
func OnClose(callback func()) {
On(EventQuit, callback)
On(EventTerminated, callback)
}
// OnKey 使用Key增加事件回调
func OnKey(event Event, key interface{}, callback func()) {
if key == nil {

View File

@@ -161,7 +161,7 @@ ON "` + this.itemTableName + `" (
this.db = db
goman.New(func() {
events.On(events.EventQuit, func() {
events.OnClose(func() {
_ = this.Close()
this.cleanTicker.Stop()
})

View File

@@ -29,7 +29,7 @@ func init() {
SharedIPListManager.Start()
})
})
events.On(events.EventQuit, func() {
events.OnClose(func() {
SharedIPListManager.Stop()
})

View File

@@ -18,7 +18,7 @@ func init() {
return
}
events.On(events.EventQuit, func() {
events.OnClose(func() {
SharedManager.Quit()
})
}

View File

@@ -27,10 +27,9 @@ func init() {
sharedOCSPTask.Start()
})
})
events.On(events.EventQuit, func() {
events.OnClose(func() {
sharedOCSPTask.Stop()
})
}
// OCSPUpdateTask 更新OCSP任务

View File

@@ -26,7 +26,7 @@ func init() {
sharedSyncAPINodesTask.Start()
})
})
events.On(events.EventQuit, func() {
events.OnClose(func() {
sharedSyncAPINodesTask.Stop()
})
}

View File

@@ -36,7 +36,7 @@ func init() {
})
})
events.On(events.EventQuit, func() {
events.OnClose(func() {
SharedBandwidthStatManager.Cancel()
err := SharedBandwidthStatManager.Save()

View File

@@ -30,7 +30,7 @@ type DB struct {
func NewDB(path string) *DB {
var db = &DB{path: path}
events.On(events.EventQuit, func() {
events.OnClose(func() {
_ = db.Close()
})

View File

@@ -45,6 +45,9 @@ func (this *Batch) OnFail(callback func(err error)) {
}
func (this *Batch) Add(query string, args ...any) {
if this.isClosed {
return
}
this.queue <- &batchItem{
query: query,
args: args,
@@ -139,6 +142,10 @@ func (this *Batch) beginTx() *sql.Tx {
}
func (this *Batch) execItem(tx *sql.Tx, item *batchItem) error {
if this.isClosed {
return nil
}
if this.enableStat {
defer SharedQueryStatManager.AddQuery(item.query).End()
}

View File

@@ -24,7 +24,7 @@ func init() {
SharedFreeHoursManager.Start()
})
})
events.On(events.EventQuit, func() {
events.OnClose(func() {
SharedFreeHoursManager.Stop()
})
}