🎨 桌面端自动下载更新安装包 https://github.com/siyuan-note/siyuan/issues/5837

This commit is contained in:
Liang Ding 2022-09-08 09:55:29 +08:00
parent 8f6ffd4dcd
commit 313143304b
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
7 changed files with 83 additions and 20 deletions

View file

@ -18,8 +18,8 @@ package model
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
@ -345,7 +345,15 @@ func loadLangs() (ret []*conf.Lang) {
var exitLock = sync.Mutex{}
func Close(force bool) (err error) {
// Close 退出内核进程.
//
// force是否不执行同步过程而直接退出
// execInstallPkg是否执行新版本安装包
//
// 0默认按照设置项 System.DownloadInstallPkg 检查并推送提示
// 1执行安装
// 2不执行安装
func Close(force bool, execInstallPkg int) (exitCode int) {
exitLock.Lock()
defer exitLock.Unlock()
@ -355,7 +363,7 @@ func Close(force bool) (err error) {
if !force {
SyncData(false, true, false)
if 0 != ExitSyncSucc {
err = errors.New(Conf.Language(96))
exitCode = 1
return
}
}
@ -366,13 +374,35 @@ func Close(force bool) (err error) {
// return true
//})
newVerInstallPkgPath := ""
if Conf.System.DownloadInstallPkg && 0 == execInstallPkg {
newVerInstallPkgPath = GetNewVerInstallPkgPath()
if "" != newVerInstallPkgPath {
exitCode = 2
return
}
}
if 2 == execInstallPkg && "" != newVerInstallPkgPath { // 执行新版本安装
logging.LogInfof("install new version [%s]", newVerInstallPkgPath)
cmd := exec.Command(newVerInstallPkgPath)
util.CmdAttr(cmd)
data, cmdErr := cmd.CombinedOutput()
if nil != cmdErr {
logging.LogErrorf("exec install new version failed: %s", cmdErr)
return
}
logging.LogDebugf("exec install new version output [%s]", data)
}
Conf.Close()
sql.CloseDatabase()
util.WebSocketServer.Close()
clearWorkspaceTemp()
logging.LogInfof("exited kernel")
go func() {
time.Sleep(500 * time.Millisecond)
logging.LogInfof("exited kernel")
util.WebSocketServer.Close()
os.Exit(util.ExitCodeOk)
}()
return

View file

@ -240,7 +240,7 @@ func AutoRefreshCheck() {
time.Sleep(3 * time.Minute)
checkDownloadInstallPkg()
if isExistUpdateInstallPkg() {
if "" != GetNewVerInstallPkgPath() {
util.PushMsg(Conf.Language(61), 0)
}
}()

View file

@ -34,7 +34,7 @@ func HookResident() {
for range time.Tick(time.Second * 30) {
if 0 == util.CountSessions() {
logging.LogInfof("no active session, exit kernel process now")
Close(false)
Close(false, 2)
}
}
}
@ -44,5 +44,5 @@ func HandleSignal() {
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
s := <-c
logging.LogInfof("received os signal [%s], exit kernel process now", s)
Close(false)
Close(false, 2)
}

View file

@ -34,16 +34,23 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func isExistUpdateInstallPkg() bool {
func GetNewVerInstallPkgPath() string {
if !Conf.System.DownloadInstallPkg {
return ""
}
downloadPkgURL, checksum, err := getUpdatePkg()
if nil != err {
return false
return ""
}
pkg := path.Base(downloadPkgURL)
savePath := filepath.Join(util.TempDir, "install", pkg)
localChecksum, _ := sha256Hash(savePath)
return checksum == localChecksum
ret := filepath.Join(util.TempDir, "install", pkg)
localChecksum, _ := sha256Hash(ret)
if checksum != localChecksum {
return ""
}
return ret
}
var checkDownloadInstallPkgLock = sync.Mutex{}