Skip to content

Commit

Permalink
Remove single_instance module and refactor existing instance check
Browse files Browse the repository at this point in the history
The single_instance module has been removed, which included the deletion of multiple related files. The method to check if an instance of the application is already running has been refactored in the main.go file. Additionally, a timeout was added for the "PickFiles" method in ui_impl.go to prevent unnecessary waiting. This leads to cleaner code and potentially faster execution times.
  • Loading branch information
qianlifeng committed Apr 28, 2024
1 parent 747a121 commit 5ce387a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 132 deletions.
1 change: 0 additions & 1 deletion Wox/go.mod
Expand Up @@ -38,7 +38,6 @@ require (
github.com/tidwall/pretty v1.2.1
github.com/tmc/langchaingo v0.1.8
github.com/wissance/stringFormatter v1.2.0
github.com/xeonx/timeago v1.0.0-rc5
go.uber.org/zap v1.27.0
golang.design/x/hotkey v0.4.1
golang.org/x/image v0.15.0
Expand Down
2 changes: 0 additions & 2 deletions Wox/go.sum
Expand Up @@ -181,8 +181,6 @@ github.com/vcaesar/tt v0.20.0 h1:9t2Ycb9RNHcP0WgQgIaRKJBB+FrRdejuaL6uWIHuoBA=
github.com/vcaesar/tt v0.20.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
github.com/wissance/stringFormatter v1.2.0 h1:lB0zcJkTA1O4Eb2qSTJmyapla/LihQt6NpJLghwWSb0=
github.com/wissance/stringFormatter v1.2.0/go.mod h1:H7Mz15+5i8ypmv6bLknM/uD+U1teUW99PlW0DNCNscA=
github.com/xeonx/timeago v1.0.0-rc5 h1:pwcQGpaH3eLfPtXeyPA4DmHWjoQt0Ea7/++FwpxqLxg=
github.com/xeonx/timeago v1.0.0-rc5/go.mod h1:qDLrYEFynLO7y5Ho7w3GwgtYgpy5UfhcXIIQvMKVDkA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
Expand Down
60 changes: 46 additions & 14 deletions Wox/main.go
@@ -1,9 +1,12 @@
package main

import (
"context"
"fmt"
"golang.design/x/hotkey/mainthread"
"os"
"runtime"
"strconv"
"strings"
"time"
"wox/i18n"
Expand All @@ -13,7 +16,6 @@ import (
"wox/ui"
"wox/util"
"wox/util/hotkey"
"wox/util/single_instance"
)

import _ "wox/plugin/host" // import all hosts
Expand Down Expand Up @@ -49,23 +51,22 @@ func main() {
}
util.GetLogger().Info(ctx, fmt.Sprintf("server port: %d", serverPort))

existingPort, lockErr := single_instance.Lock(serverPort)
if lockErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("there is existing instance running, port: %d, lock return: %s", existingPort, lockErr.Error()))

if existingPort > 0 {
_, postShowErr := util.HttpPost(ctx, fmt.Sprintf("http://localhost:%d/show", existingPort), "")
if postShowErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to show existing instance: %s", postShowErr.Error()))
return
}
// check if there is existing instance running
if existingPort := getExistingInstancePort(ctx); existingPort > 0 {
util.GetLogger().Error(ctx, fmt.Sprintf("there is existing instance running, port: %d", existingPort))
_, postShowErr := util.HttpPost(ctx, fmt.Sprintf("http://localhost:%d/show", existingPort), "")
if postShowErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to show existing instance: %s", postShowErr.Error()))
} else {
util.GetLogger().Error(ctx, "failed to get existing instance port")
util.GetLogger().Info(ctx, "show existing instance successfully, bye~")
}

return
} else {
util.GetLogger().Info(ctx, "lock server port success")
util.GetLogger().Info(ctx, "no existing instance found")
writeErr := os.WriteFile(util.GetLocation().GetAppLockPath(), []byte(fmt.Sprintf("%d", serverPort)), 0644)
if writeErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to write lock file: %s", writeErr.Error()))
}
}

extractErr := resource.Extract(ctx)
Expand Down Expand Up @@ -138,3 +139,34 @@ func main() {
ui.GetUIManager().StartWebsocketAndWait(ctx, serverPort)
})
}

// retrieves the instance port from the existing instance lock file.
// It returns 0 if the lock file doesn't exist or fails to read the file.
func getExistingInstancePort(ctx context.Context) int {
filePath := util.GetLocation().GetAppLockPath()
if !util.IsFileExists(filePath) {
return 0
}

file, err := os.ReadFile(filePath)
if err != nil {
return 0
}

port, err := strconv.Atoi(string(file))
if err != nil {
return 0
}

//check if the port is valid
response, err := util.HttpGet(ctx, fmt.Sprintf("http://localhost:%d/ping", port))
if err != nil {
return 0
}

if !strings.Contains(string(response), "pong") {
return 0
}

return port
}
7 changes: 6 additions & 1 deletion Wox/ui/ui_impl.go
Expand Up @@ -85,8 +85,13 @@ func (u *uiImpl) send(ctx context.Context, method string, data any) (responseDat
return "", err
}

var timeout = time.Second * 2
if method == "PickFiles" {
// pick files may take a long time
timeout = time.Second * 180
}
select {
case <-time.NewTimer(time.Second * 60).C:
case <-time.NewTimer(timeout).C:
logger.Error(ctx, fmt.Sprintf("invoke ui method %s response timeout", method))
return "", fmt.Errorf("request timeout, request id: %s", requestID)
case response := <-resultChan:
Expand Down
2 changes: 1 addition & 1 deletion Wox/util/location.go
Expand Up @@ -193,6 +193,6 @@ func (l *Location) GetUIAppPath() string {
return ""
}

func (l *Location) GetAppLockFilePath() string {
func (l *Location) GetAppLockPath() string {
return path.Join(l.GetWoxDataDirectory(), "wox.lock")
}
1 change: 0 additions & 1 deletion Wox/util/single_instance/README.md

This file was deleted.

28 changes: 0 additions & 28 deletions Wox/util/single_instance/single_instance.go

This file was deleted.

32 changes: 0 additions & 32 deletions Wox/util/single_instance/single_instance_posix.go

This file was deleted.

52 changes: 0 additions & 52 deletions Wox/util/single_instance/single_instance_windows.go

This file was deleted.

0 comments on commit 5ce387a

Please sign in to comment.