Pārlūkot izejas kodu

:bug: 退出应用时数据库未完全写入 https://github.com/siyuan-note/siyuan/issues/6318

Liang Ding 2 gadi atpakaļ
vecāks
revīzija
db04bfa572

+ 14 - 4
kernel/model/conf.go

@@ -353,9 +353,9 @@ var exitLock = sync.Mutex{}
 // force:是否不执行同步过程而直接退出
 // force:是否不执行同步过程而直接退出
 // execInstallPkg:是否执行新版本安装包
 // execInstallPkg:是否执行新版本安装包
 //
 //
-//		0:默认按照设置项 System.DownloadInstallPkg 检查并推送提示
-//	 1:不执行新版本安装
-//	 2:执行新版本安装
+//	  0:默认按照设置项 System.DownloadInstallPkg 检查并推送提示
+//		 1:不执行新版本安装
+//		 2:执行新版本安装
 func Close(force bool, execInstallPkg int) (exitCode int) {
 func Close(force bool, execInstallPkg int) (exitCode int) {
 	exitLock.Lock()
 	exitLock.Lock()
 	defer exitLock.Unlock()
 	defer exitLock.Unlock()
@@ -363,7 +363,16 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
 	logging.LogInfof("exiting kernel [force=%v, execInstallPkg=%d]", force, execInstallPkg)
 	logging.LogInfof("exiting kernel [force=%v, execInstallPkg=%d]", force, execInstallPkg)
 
 
 	util.PushMsg(Conf.Language(95), 10000*60)
 	util.PushMsg(Conf.Language(95), 10000*60)
-	WaitForWritingFiles()
+
+	wg := sync.WaitGroup{}
+	go func() {
+		wg.Add(1)
+		time.Sleep(util.FrontendQueueInterval)
+		WaitForWritingFiles()
+		time.Sleep(50 * time.Millisecond)
+		sql.WaitForWritingDatabase()
+		wg.Done()
+	}()
 	if !force {
 	if !force {
 		SyncData(false, true, false)
 		SyncData(false, true, false)
 		if 0 != ExitSyncSucc {
 		if 0 != ExitSyncSucc {
@@ -371,6 +380,7 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
 			return
 			return
 		}
 		}
 	}
 	}
+	wg.Wait()
 
 
 	//util.UIProcessIDs.Range(func(key, _ interface{}) bool {
 	//util.UIProcessIDs.Range(func(key, _ interface{}) bool {
 	//	pid := key.(string)
 	//	pid := key.(string)

+ 2 - 1
kernel/model/outline.go

@@ -22,10 +22,11 @@ import (
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/ast"
 	"github.com/emirpasic/gods/stacks/linkedliststack"
 	"github.com/emirpasic/gods/stacks/linkedliststack"
 	"github.com/siyuan-note/siyuan/kernel/treenode"
 	"github.com/siyuan-note/siyuan/kernel/treenode"
+	"github.com/siyuan-note/siyuan/kernel/util"
 )
 )
 
 
 func Outline(rootID string) (ret []*Path, err error) {
 func Outline(rootID string) (ret []*Path, err error) {
-	time.Sleep(512 * time.Millisecond /* 前端队列轮询间隔 */)
+	time.Sleep(util.FrontendQueueInterval)
 	WaitForWritingFiles()
 	WaitForWritingFiles()
 
 
 	ret = []*Path{}
 	ret = []*Path{}

+ 1 - 1
kernel/model/search.go

@@ -45,7 +45,7 @@ type EmbedBlock struct {
 }
 }
 
 
 func SearchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {
 func SearchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {
-	time.Sleep(512 * time.Millisecond /* 前端队列轮询间隔 */)
+	time.Sleep(util.FrontendQueueInterval)
 	WaitForWritingFiles()
 	WaitForWritingFiles()
 	return searchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
 	return searchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
 }
 }

+ 2 - 6
kernel/sql/queue.go

@@ -32,10 +32,6 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/util"
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 )
 
 
-const (
-	upsertTreesFlushDelay = 3000
-)
-
 var (
 var (
 	operationQueue      []*treeQueueOperation
 	operationQueue      []*treeQueueOperation
 	upsertTreeQueueLock = sync.Mutex{}
 	upsertTreeQueueLock = sync.Mutex{}
@@ -57,7 +53,7 @@ type treeQueueOperation struct {
 func AutoFlushTreeQueue() {
 func AutoFlushTreeQueue() {
 	for {
 	for {
 		flushTreeQueue()
 		flushTreeQueue()
-		time.Sleep(time.Duration(upsertTreesFlushDelay) * time.Millisecond)
+		time.Sleep(util.SQLFlushInterval)
 	}
 	}
 }
 }
 
 
@@ -78,7 +74,7 @@ func WaitForWritingDatabase() {
 }
 }
 
 
 func isWritingDatabase() bool {
 func isWritingDatabase() bool {
-	time.Sleep(time.Duration(upsertTreesFlushDelay+50) * time.Millisecond)
+	time.Sleep(util.SQLFlushInterval + 50*time.Millisecond)
 	if 0 < len(operationQueue) || util.IsMutexLocked(&txLock) {
 	if 0 < len(operationQueue) || util.IsMutexLocked(&txLock) {
 		return true
 		return true
 	}
 	}

+ 8 - 0
kernel/util/runtime.go

@@ -89,3 +89,11 @@ func SetNetworkProxy(proxyURL string) {
 		logging.LogInfof("use network proxy [%s]", proxyURL)
 		logging.LogInfof("use network proxy [%s]", proxyURL)
 	}
 	}
 }
 }
+
+const (
+	// FrontendQueueInterval 为前端请求队列轮询间隔。
+	FrontendQueueInterval = 512 * time.Millisecond
+
+	// SQLFlushInterval 为数据库事务队列写入间隔。
+	SQLFlushInterval = 3000 * time.Millisecond
+)