2024-08-09 18:12:01 +00:00
package cli
import (
"context"
"time"
cliContext "github.com/mudler/LocalAI/core/cli/context"
"github.com/mudler/LocalAI/core/explorer"
"github.com/mudler/LocalAI/core/http"
2025-10-09 10:36:45 +00:00
"github.com/mudler/LocalAI/pkg/signals"
2025-12-21 18:33:13 +00:00
"github.com/mudler/xlog"
2024-08-09 18:12:01 +00:00
)
type ExplorerCMD struct {
2024-08-10 18:50:57 +00:00
Address string ` env:"LOCALAI_ADDRESS,ADDRESS" default:":8080" help:"Bind address for the API server" group:"api" `
PoolDatabase string ` env:"LOCALAI_POOL_DATABASE,POOL_DATABASE" default:"explorer.json" help:"Path to the pool database" group:"api" `
ConnectionTimeout string ` env:"LOCALAI_CONNECTION_TIMEOUT,CONNECTION_TIMEOUT" default:"2m" help:"Connection timeout for the explorer" group:"api" `
ConnectionErrorThreshold int ` env:"LOCALAI_CONNECTION_ERROR_THRESHOLD,CONNECTION_ERROR_THRESHOLD" default:"3" help:"Connection failure threshold for the explorer" group:"api" `
2024-08-12 17:25:44 +00:00
WithSync bool ` env:"LOCALAI_WITH_SYNC,WITH_SYNC" default:"false" help:"Enable sync with the network" group:"api" `
OnlySync bool ` env:"LOCALAI_ONLY_SYNC,ONLY_SYNC" default:"false" help:"Only sync with the network" group:"api" `
2024-08-09 18:12:01 +00:00
}
func ( e * ExplorerCMD ) Run ( ctx * cliContext . Context ) error {
db , err := explorer . NewDatabase ( e . PoolDatabase )
if err != nil {
return err
}
dur , err := time . ParseDuration ( e . ConnectionTimeout )
if err != nil {
return err
}
2024-08-12 17:25:44 +00:00
if e . WithSync {
ds := explorer . NewDiscoveryServer ( db , dur , e . ConnectionErrorThreshold )
go ds . Start ( context . Background ( ) , true )
}
if e . OnlySync {
ds := explorer . NewDiscoveryServer ( db , dur , e . ConnectionErrorThreshold )
ctx := context . Background ( )
return ds . Start ( ctx , false )
}
appHTTP := http . Explorer ( db )
2024-08-09 18:12:01 +00:00
2025-10-09 10:36:45 +00:00
signals . RegisterGracefulTerminationHandler ( func ( ) {
2025-11-14 21:57:53 +00:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 10 * time . Second )
defer cancel ( )
if err := appHTTP . Shutdown ( ctx ) ; err != nil {
2025-12-21 18:33:13 +00:00
xlog . Error ( "error during shutdown" , "error" , err )
2025-10-09 10:36:45 +00:00
}
} )
2025-09-04 19:37:28 +00:00
2025-11-14 21:57:53 +00:00
return appHTTP . Start ( e . Address )
2024-08-09 18:12:01 +00:00
}