Skip to content

Commit

Permalink
Merge pull request #1976 from anyproto/go-4571-windows-instant-shutdo…
Browse files Browse the repository at this point in the history
…wn-electron

GO-4571 handle stdin message to shutdown on windows
  • Loading branch information
requilence authored Dec 30, 2024
2 parents f33a6b0 + 5bbc9b2 commit 57da8ab
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 104 deletions.
24 changes: 21 additions & 3 deletions cmd/grpcserver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"bufio"
"context"
"errors"
"fmt"
Expand All @@ -13,6 +14,7 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
Expand Down Expand Up @@ -224,8 +226,25 @@ func main() {

startReportMemory(mw)

shutdown := func() {
server.Stop()
proxy.Close()
mw.AppShutdown(context.Background(), &pb.RpcAppShutdownRequest{})
}
// do not change this, js client relies on this msg to ensure that server is up and parse address
fmt.Println(grpcWebStartedMessagePrefix + webaddr)
if runtime.GOOS == "windows" {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
message := scanner.Text()
if message == "shutdown" {
fmt.Println("[anytype-heart] Shutdown: received shutdown msg, closing components...")
// Perform cleanup or exit
shutdown()
return
}
}
}

for {
sig := <-signalChan
Expand All @@ -235,9 +254,8 @@ func main() {
}
continue
}
server.Stop()
proxy.Close()
mw.AppShutdown(context.Background(), &pb.RpcAppShutdownRequest{})
fmt.Printf("[anytype-heart] Shutdown: received OS signal (%s), closing components...\n", sig.String())
shutdown()
return
}
}
Expand Down
1 change: 0 additions & 1 deletion core/anytype/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ func Bootstrap(a *app.App, components ...app.Component) {
Register(recordsbatcher.New()).
Register(configfetcher.New()).
Register(process.New()).
Register(core.New()).
Register(core.NewTempDirService()).
Register(treemanager.New()).
Register(block.New()).
Expand Down
9 changes: 8 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ func New() *Middleware {
}

func (mw *Middleware) AppShutdown(cctx context.Context, request *pb.RpcAppShutdownRequest) *pb.RpcAppShutdownResponse {
mw.applicationService.Stop()
err := mw.applicationService.Stop()
// using fmt.Println instead of log because we want it only in stdout
if err != nil {
fmt.Println("[anytype-heart] Shutdown: error during closing components: ", err)
} else {
fmt.Println("[anytype-heart] Shutdown: graceful shutdown finished")
}
// intentionally do not pass the error to the client
return &pb.RpcAppShutdownResponse{
Error: &pb.RpcAppShutdownResponseError{
Code: pb.RpcAppShutdownResponseError_NULL,
Expand Down
99 changes: 0 additions & 99 deletions pkg/lib/core/core.go

This file was deleted.

3 changes: 3 additions & 0 deletions pkg/lib/core/tmp_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"github.com/anyproto/any-sync/app"

"github.com/anyproto/anytype-heart/core/wallet"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
)

var log = logging.Logger("anytype-core")

const tmpDir = "tmp"

type TempDirProvider interface {
Expand Down

0 comments on commit 57da8ab

Please sign in to comment.