Skip to content

Commit 27c4161

Browse files
authored
chore: update cogito and simplify MCP logics (#6413)
* chore: update cogito and simplify MCP logics Signed-off-by: Ettore Di Giacinto <[email protected]> * Refine signal handling Signed-off-by: Ettore Di Giacinto <[email protected]> --------- Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 459b6ab commit 27c4161

File tree

13 files changed

+144
-278
lines changed

13 files changed

+144
-278
lines changed

cmd/launcher/main.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package main
22

33
import (
44
"log"
5-
"os"
6-
"os/signal"
7-
"syscall"
85

96
"fyne.io/fyne/v2"
107
"fyne.io/fyne/v2/app"
118
"fyne.io/fyne/v2/driver/desktop"
129
coreLauncher "github.com/mudler/LocalAI/cmd/launcher/internal"
10+
"github.com/mudler/LocalAI/pkg/signals"
1311
)
1412

1513
func main() {
@@ -42,7 +40,12 @@ func main() {
4240
}
4341

4442
// Setup signal handling for graceful shutdown
45-
setupSignalHandling(launcher)
43+
signals.RegisterGracefulTerminationHandler(func() {
44+
// Perform cleanup
45+
if err := launcher.Shutdown(); err != nil {
46+
log.Printf("Error during shutdown: %v", err)
47+
}
48+
})
4649

4750
// Initialize the launcher state
4851
go func() {
@@ -67,26 +70,3 @@ func main() {
6770
// Run the application in background (window only shown when "Settings" is clicked)
6871
myApp.Run()
6972
}
70-
71-
// setupSignalHandling sets up signal handlers for graceful shutdown
72-
func setupSignalHandling(launcher *coreLauncher.Launcher) {
73-
// Create a channel to receive OS signals
74-
sigChan := make(chan os.Signal, 1)
75-
76-
// Register for interrupt and terminate signals
77-
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
78-
79-
// Handle signals in a separate goroutine
80-
go func() {
81-
sig := <-sigChan
82-
log.Printf("Received signal %v, shutting down gracefully...", sig)
83-
84-
// Perform cleanup
85-
if err := launcher.Shutdown(); err != nil {
86-
log.Printf("Error during shutdown: %v", err)
87-
}
88-
89-
// Exit the application
90-
os.Exit(0)
91-
}()
92-
}

core/cli/explorer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"time"
66

77
cliContext "github.com/mudler/LocalAI/core/cli/context"
8-
"github.com/mudler/LocalAI/core/cli/signals"
98
"github.com/mudler/LocalAI/core/explorer"
109
"github.com/mudler/LocalAI/core/http"
10+
"github.com/mudler/LocalAI/pkg/signals"
11+
"github.com/rs/zerolog/log"
1112
)
1213

1314
type ExplorerCMD struct {
@@ -46,7 +47,11 @@ func (e *ExplorerCMD) Run(ctx *cliContext.Context) error {
4647

4748
appHTTP := http.Explorer(db)
4849

49-
signals.Handler(nil)
50+
signals.RegisterGracefulTerminationHandler(func() {
51+
if err := appHTTP.Shutdown(); err != nil {
52+
log.Error().Err(err).Msg("error during shutdown")
53+
}
54+
})
5055

5156
return appHTTP.Listen(e.Address)
5257
}

core/cli/federated.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55

66
cliContext "github.com/mudler/LocalAI/core/cli/context"
7-
"github.com/mudler/LocalAI/core/cli/signals"
87
"github.com/mudler/LocalAI/core/p2p"
8+
"github.com/mudler/LocalAI/pkg/signals"
99
)
1010

1111
type FederatedCLI struct {
@@ -20,7 +20,11 @@ func (f *FederatedCLI) Run(ctx *cliContext.Context) error {
2020

2121
fs := p2p.NewFederatedServer(f.Address, p2p.NetworkID(f.Peer2PeerNetworkID, p2p.FederatedID), f.Peer2PeerToken, !f.RandomWorker, f.TargetWorker)
2222

23-
signals.Handler(nil)
23+
c, cancel := context.WithCancel(context.Background())
2424

25-
return fs.Start(context.Background())
25+
signals.RegisterGracefulTerminationHandler(func() {
26+
cancel()
27+
})
28+
29+
return fs.Start(c)
2630
}

core/cli/run.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"github.com/mudler/LocalAI/core/application"
1111
cli_api "github.com/mudler/LocalAI/core/cli/api"
1212
cliContext "github.com/mudler/LocalAI/core/cli/context"
13-
"github.com/mudler/LocalAI/core/cli/signals"
1413
"github.com/mudler/LocalAI/core/config"
1514
"github.com/mudler/LocalAI/core/http"
1615
"github.com/mudler/LocalAI/core/p2p"
1716
"github.com/mudler/LocalAI/internal"
17+
"github.com/mudler/LocalAI/pkg/signals"
1818
"github.com/mudler/LocalAI/pkg/system"
1919
"github.com/rs/zerolog"
2020
"github.com/rs/zerolog/log"
@@ -226,8 +226,11 @@ func (r *RunCMD) Run(ctx *cliContext.Context) error {
226226
return err
227227
}
228228

229-
// Catch signals from the OS requesting us to exit, and stop all backends
230-
signals.Handler(app.ModelLoader())
229+
signals.RegisterGracefulTerminationHandler(func() {
230+
if err := app.ModelLoader().StopAllGRPC(); err != nil {
231+
log.Error().Err(err).Msg("error while stopping all grpc backends")
232+
}
233+
})
231234

232235
return appHTTP.Listen(r.Address)
233236
}

core/cli/signals/signals.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

core/cli/worker/worker_llamacpp.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
cliContext "github.com/mudler/LocalAI/core/cli/context"
1313
"github.com/mudler/LocalAI/core/config"
14-
"github.com/mudler/LocalAI/core/cli/signals"
1514
"github.com/mudler/LocalAI/core/gallery"
1615
"github.com/mudler/LocalAI/pkg/model"
1716
"github.com/mudler/LocalAI/pkg/system"
@@ -85,8 +84,6 @@ func (r *LLamaCPP) Run(ctx *cliContext.Context) error {
8584

8685
args = append([]string{grpcProcess}, args...)
8786

88-
signals.Handler(nil)
89-
9087
return syscall.Exec(
9188
grpcProcess,
9289
args,

core/cli/worker/worker_p2p.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"time"
1010

1111
cliContext "github.com/mudler/LocalAI/core/cli/context"
12-
"github.com/mudler/LocalAI/core/cli/signals"
1312
"github.com/mudler/LocalAI/core/p2p"
13+
"github.com/mudler/LocalAI/pkg/signals"
1414
"github.com/mudler/LocalAI/pkg/system"
1515
"github.com/phayes/freeport"
1616
"github.com/rs/zerolog/log"
@@ -48,6 +48,9 @@ func (r *P2P) Run(ctx *cliContext.Context) error {
4848

4949
address := "127.0.0.1"
5050

51+
c, cancel := context.WithCancel(context.Background())
52+
defer cancel()
53+
5154
if r.NoRunner {
5255
// Let override which port and address to bind if the user
5356
// configure the llama-cpp service on its own
@@ -59,7 +62,7 @@ func (r *P2P) Run(ctx *cliContext.Context) error {
5962
p = r.RunnerPort
6063
}
6164

62-
_, err = p2p.ExposeService(context.Background(), address, p, r.Token, p2p.NetworkID(r.Peer2PeerNetworkID, p2p.WorkerID))
65+
_, err = p2p.ExposeService(c, address, p, r.Token, p2p.NetworkID(r.Peer2PeerNetworkID, p2p.WorkerID))
6366
if err != nil {
6467
return err
6568
}
@@ -101,13 +104,15 @@ func (r *P2P) Run(ctx *cliContext.Context) error {
101104
}
102105
}()
103106

104-
_, err = p2p.ExposeService(context.Background(), address, fmt.Sprint(port), r.Token, p2p.NetworkID(r.Peer2PeerNetworkID, p2p.WorkerID))
107+
_, err = p2p.ExposeService(c, address, fmt.Sprint(port), r.Token, p2p.NetworkID(r.Peer2PeerNetworkID, p2p.WorkerID))
105108
if err != nil {
106109
return err
107110
}
108111
}
109112

110-
signals.Handler(nil)
113+
signals.RegisterGracefulTerminationHandler(func() {
114+
cancel()
115+
})
111116

112117
for {
113118
time.Sleep(1 * time.Second)

0 commit comments

Comments
 (0)