Browse Source

:art: Improve kernel stability by eliminating some data races https://github.com/siyuan-note/siyuan/issues/9842

Daniel 1 year ago
parent
commit
8c4adbc48a
2 changed files with 22 additions and 0 deletions
  1. 6 0
      kernel/model/ocr.go
  2. 16 0
      kernel/util/tesseract.go

+ 6 - 0
kernel/model/ocr.go

@@ -17,6 +17,8 @@ import (
 )
 )
 
 
 func OCRAssetsJob() {
 func OCRAssetsJob() {
+	util.WaitForTesseractInit()
+
 	if !util.TesseractEnabled {
 	if !util.TesseractEnabled {
 		return
 		return
 	}
 	}
@@ -25,6 +27,10 @@ func OCRAssetsJob() {
 }
 }
 
 
 func autoOCRAssets() {
 func autoOCRAssets() {
+	if !util.TesseractEnabled {
+		return
+	}
+
 	defer logging.Recover()
 	defer logging.Recover()
 
 
 	assetsPath := util.GetDataAssetsAbsPath()
 	assetsPath := util.GetDataAssetsAbsPath()

+ 16 - 0
kernel/util/tesseract.go

@@ -26,6 +26,7 @@ import (
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
+	"sync/atomic"
 	"time"
 	"time"
 
 
 	"github.com/88250/gulu"
 	"github.com/88250/gulu"
@@ -136,9 +137,21 @@ func Tesseract(imgAbsPath string) string {
 	return ret
 	return ret
 }
 }
 
 
+var tesseractInited = atomic.Bool{}
+
+func WaitForTesseractInit() {
+	for {
+		if tesseractInited.Load() {
+			return
+		}
+		time.Sleep(time.Second)
+	}
+}
+
 func InitTesseract() {
 func InitTesseract() {
 	ver := getTesseractVer()
 	ver := getTesseractVer()
 	if "" == ver {
 	if "" == ver {
+		tesseractInited.Store(true)
 		return
 		return
 	}
 	}
 
 
@@ -146,6 +159,7 @@ func InitTesseract() {
 	if 1 > len(langs) {
 	if 1 > len(langs) {
 		logging.LogWarnf("no tesseract langs found")
 		logging.LogWarnf("no tesseract langs found")
 		TesseractEnabled = false
 		TesseractEnabled = false
+		tesseractInited.Store(true)
 		return
 		return
 	}
 	}
 
 
@@ -162,6 +176,7 @@ func InitTesseract() {
 			TesseractEnabled = enabledBool
 			TesseractEnabled = enabledBool
 			if !enabledBool {
 			if !enabledBool {
 				logging.LogInfof("tesseract-ocr disabled by env")
 				logging.LogInfof("tesseract-ocr disabled by env")
+				tesseractInited.Store(true)
 				return
 				return
 			}
 			}
 		}
 		}
@@ -169,6 +184,7 @@ func InitTesseract() {
 
 
 	TesseractLangs = filterTesseractLangs(langs)
 	TesseractLangs = filterTesseractLangs(langs)
 	logging.LogInfof("tesseract-ocr enabled [ver=%s, maxSize=%s, langs=%s]", ver, humanize.Bytes(TesseractMaxSize), strings.Join(TesseractLangs, "+"))
 	logging.LogInfof("tesseract-ocr enabled [ver=%s, maxSize=%s, langs=%s]", ver, humanize.Bytes(TesseractMaxSize), strings.Join(TesseractLangs, "+"))
+	tesseractInited.Store(true)
 }
 }
 
 
 func filterTesseractLangs(langs []string) (ret []string) {
 func filterTesseractLangs(langs []string) (ret []string) {