From 4ba967cc98b83e07286e3249a3fe03a33ae6a645 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:18:04 +0800 Subject: [PATCH 1/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/main.go | 1 + kernel/model/process.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/kernel/main.go b/kernel/main.go index 69e3d6c9b..14f92c102 100644 --- a/kernel/main.go +++ b/kernel/main.go @@ -51,6 +51,7 @@ func main() { go sql.AutoFlushTreeQueue() go treenode.AutoFlushBlockTree() go cache.LoadAssets() + go model.HookDesktopUIProc() model.WatchAssets() model.HandleSignal() } diff --git a/kernel/model/process.go b/kernel/model/process.go index 7ce0f82e8..9ec6da9bb 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -19,9 +19,14 @@ package model import ( "os" "os/signal" + "strconv" + "strings" "syscall" + "time" + goPS "github.com/mitchellh/go-ps" "github.com/siyuan-note/logging" + "github.com/siyuan-note/siyuan/kernel/util" ) func HandleSignal() { @@ -31,3 +36,39 @@ func HandleSignal() { logging.LogInfof("received os signal [%s], exit kernel process now", s) Close(false, 1) } + +func HookDesktopUIProc() { + if util.ContainerStd != util.Container { + return + } + + time.Sleep(30 * time.Second) + existUIProc := false + for range time.Tick(7 * time.Second) { + util.UIProcessIDs.Range(func(uiProcIDArg, _ interface{}) bool { + uiProcID, err := strconv.Atoi(uiProcIDArg.(string)) + if nil != err { + logging.LogErrorf("invalid UI proc ID [%s]: %s", uiProcIDArg, err) + return true + } + + proc, err := goPS.FindProcess(uiProcID) + if nil != err { + logging.LogErrorf("find UI proc [%d] failed: %s", uiProcID, err) + return true + } + + if strings.Contains(strings.ToLower(proc.Executable()), "siyuan") { + existUIProc = true + return false + } + + return true + }) + + if !existUIProc { + logging.LogInfof("no active UI proc, exit kernel process now") + Close(false, 1) + } + } +} From f5095ad1b8f24a1bbe92009cf468b8b56b3f7cc1 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:18:29 +0800 Subject: [PATCH 2/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/process.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/model/process.go b/kernel/model/process.go index 9ec6da9bb..1a94cf896 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -38,7 +38,7 @@ func HandleSignal() { } func HookDesktopUIProc() { - if util.ContainerStd != util.Container { + if util.ContainerStd != util.Container || "dev" == util.Mode { return } From 3c65021b839b491030e746784cd562139bcb3ccd Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:21:52 +0800 Subject: [PATCH 3/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/process.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/model/process.go b/kernel/model/process.go index 1a94cf896..1c3b4805d 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -38,10 +38,15 @@ func HandleSignal() { } func HookDesktopUIProc() { - if util.ContainerStd != util.Container || "dev" == util.Mode { + if util.ContainerStd != util.Container { return } + uiProcName := "siyuan" + if "dev" == util.Mode { + uiProcName = "electron" + } + time.Sleep(30 * time.Second) existUIProc := false for range time.Tick(7 * time.Second) { @@ -58,7 +63,7 @@ func HookDesktopUIProc() { return true } - if strings.Contains(strings.ToLower(proc.Executable()), "siyuan") { + if strings.Contains(strings.ToLower(proc.Executable()), uiProcName) { existUIProc = true return false } From bdec1215a0ad956f98f7cc47bfe99bea104cd861 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:27:27 +0800 Subject: [PATCH 4/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/process.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/kernel/model/process.go b/kernel/model/process.go index 1c3b4805d..0f73e8165 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -42,12 +42,7 @@ func HookDesktopUIProc() { return } - uiProcName := "siyuan" - if "dev" == util.Mode { - uiProcName = "electron" - } - - time.Sleep(30 * time.Second) + uiProcNames := []string{"siyuan", "electron"} existUIProc := false for range time.Tick(7 * time.Second) { util.UIProcessIDs.Range(func(uiProcIDArg, _ interface{}) bool { @@ -63,11 +58,13 @@ func HookDesktopUIProc() { return true } - if strings.Contains(strings.ToLower(proc.Executable()), uiProcName) { - existUIProc = true - return false + procName := strings.ToLower(proc.Executable()) + for _, name := range uiProcNames { + if strings.Contains(procName, name) { + existUIProc = true + return false + } } - return true }) From 402100362990671d19a090c2d3851a01940d87a2 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:28:58 +0800 Subject: [PATCH 5/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/process.go | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/model/process.go b/kernel/model/process.go index 0f73e8165..8785a9b69 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -42,6 +42,7 @@ func HookDesktopUIProc() { return } + time.Sleep(30 * time.Second) uiProcNames := []string{"siyuan", "electron"} existUIProc := false for range time.Tick(7 * time.Second) { From 8d77634807d6bbec42c038f0b2dde0401c9f46aa Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:34:44 +0800 Subject: [PATCH 6/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/conf.go | 6 +++++- kernel/model/process.go | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/model/conf.go b/kernel/model/conf.go index 00c9a09dc..842f246a0 100644 --- a/kernel/model/conf.go +++ b/kernel/model/conf.go @@ -675,7 +675,6 @@ func clearWorkspaceTemp() { os.RemoveAll(filepath.Join(util.TempDir, "import")) os.RemoveAll(filepath.Join(util.TempDir, "repo")) os.RemoveAll(filepath.Join(util.TempDir, "os")) - os.RemoveAll(filepath.Join(util.DataDir, "assets", ".siyuan", "assets.json")) // 退出时自动删除超过 7 天的安装包 https://github.com/siyuan-note/siyuan/issues/6128 install := filepath.Join(util.TempDir, "install") @@ -720,5 +719,10 @@ func clearWorkspaceTemp() { } } + // 老版本遗留文件清理 + os.RemoveAll(filepath.Join(util.DataDir, "assets", ".siyuan", "assets.json")) + os.RemoveAll(filepath.Join(util.WorkspaceDir, "backup")) + os.RemoveAll(filepath.Join(util.WorkspaceDir, "sync")) + logging.LogInfof("cleared workspace temp") } diff --git a/kernel/model/process.go b/kernel/model/process.go index 8785a9b69..0f73e8165 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -42,7 +42,6 @@ func HookDesktopUIProc() { return } - time.Sleep(30 * time.Second) uiProcNames := []string{"siyuan", "electron"} existUIProc := false for range time.Tick(7 * time.Second) { From 674aba347724299cf23bfb629aadbec1debf7d85 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:37:33 +0800 Subject: [PATCH 7/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/process.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/model/process.go b/kernel/model/process.go index 0f73e8165..a493efe6e 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -38,7 +38,7 @@ func HandleSignal() { } func HookDesktopUIProc() { - if util.ContainerStd != util.Container { + if util.ContainerStd != util.Container || "dev" == util.Mode { return } From 9854969cce2d814efbd8f38cac62bea5c35f4444 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:40:30 +0800 Subject: [PATCH 8/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/process.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/model/process.go b/kernel/model/process.go index a493efe6e..4c7f84d92 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -58,6 +58,10 @@ func HookDesktopUIProc() { return true } + if nil == proc { + return true + } + procName := strings.ToLower(proc.Executable()) for _, name := range uiProcNames { if strings.Contains(procName, name) { From d752f006caa84c0657cff8f33e9c66712b1f68ac Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 6 Jan 2023 18:44:56 +0800 Subject: [PATCH 9/9] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AE=20Electr?= =?UTF-8?q?on=20=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/7002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/electron/main.js | 15 +++++++-------- kernel/model/process.go | 1 + 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index 0d386bc1f..712db5a03 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -53,10 +53,8 @@ try { } } catch (e) { console.error(e) - require('electron'). - dialog. - showErrorBox('创建配置目录失败 Failed to create config directory', - '思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.') + require('electron').dialog.showErrorBox('创建配置目录失败 Failed to create config directory', + '思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.') app.exit() } @@ -897,6 +895,7 @@ app.on('before-quit', (event) => { }) const {powerMonitor} = require('electron') +const {write} = require("fs"); powerMonitor.on('suspend', () => { writeLog('system suspend') @@ -934,11 +933,11 @@ powerMonitor.on('resume', async () => { return } - writeLog('sync after system resume') workspaces.forEach(item => { const currentURL = new URL(item.browserWindow.getURL()) - fetch(getServer(currentURL.port) + '/api/sync/performSync', - {method: 'POST'}) + const server = getServer(currentURL.port) + writeLog('sync after system resume [' + server + '/api/sync/performSync' + ']') + fetch(server + '/api/sync/performSync', {method: 'POST'}) }) }) @@ -946,6 +945,6 @@ powerMonitor.on('shutdown', () => { writeLog('system shutdown') workspaces.forEach(item => { const currentURL = new URL(item.browserWindow.getURL()) - fetch(getServer(currentURL.port) + '/api/system/exit', {method: 'POST'}) + fetch(getServer(currentURL.port) + '/api/system/exit', {method: 'POST'}) }) }) diff --git a/kernel/model/process.go b/kernel/model/process.go index 4c7f84d92..2e00c5e1d 100644 --- a/kernel/model/process.go +++ b/kernel/model/process.go @@ -42,6 +42,7 @@ func HookDesktopUIProc() { return } + time.Sleep(30 * time.Second) uiProcNames := []string{"siyuan", "electron"} existUIProc := false for range time.Tick(7 * time.Second) {