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

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 { func (this *FileListDB) Close() error {
if this.isClosed {
return nil
}
this.isClosed = true this.isClosed = true
this.isReady = false this.isReady = false

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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