Skip to content

Commit

Permalink
Best version yet
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jan 25, 2025
1 parent c594390 commit 2bece24
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
65 changes: 32 additions & 33 deletions app/action_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package app

import (
"fmt"
"os"
"time"
"strings"

coreFile "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
_ "github.com/TrueBlocks/trueblocks-khedra/v2/pkg/env"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/types"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/validate"
"github.com/TrueBlocks/trueblocks-sdk/v4/services"
"github.com/urfave/cli/v2"
)

Expand All @@ -29,41 +29,40 @@ func (k *KhedraApp) daemonAction(c *cli.Context) error {
if err := validate.TryConnect(chain.Name, rpc, 5); err != nil {
return err
}
k.Info("Connected to", "chain", chain.Name, "rpc", rpc)
}
}
fmt.Printf("Sleeping for 10 seconds")
cnt := 0
for {
if cnt >= 1 {
break
}
cnt++
if os.Getenv("TEST_MODE") != "true" {
time.Sleep(time.Second)

var activeServices []services.Servicer
chains := strings.Split(strings.ReplaceAll(k.config.ChainList(), " ", ""), ",")
scraperSvc := services.NewScrapeService(
k.progLogger,
"all",
chains,
k.config.Services["scraper"].Sleep,
k.config.Services["scraper"].BatchSize,
)
monitorSvc := services.NewMonitorService(nil)
apiSvc := services.NewApiService(k.progLogger)
ipfsSvc := services.NewIpfsService(k.progLogger)
controlService := services.NewControlService(k.progLogger)
activeServices = append(activeServices, controlService)
activeServices = append(activeServices, scraperSvc)
activeServices = append(activeServices, monitorSvc)
activeServices = append(activeServices, apiSvc)
activeServices = append(activeServices, ipfsSvc)
k.Info("Starting khedra daemon", "services", len(activeServices))
serviceManager := services.NewServiceManager(activeServices, k.progLogger)
for _, svc := range activeServices {
if controlSvc, ok := svc.(*services.ControlService); ok {
controlSvc.AttachServiceManager(serviceManager)
}
fmt.Printf(".")
}
fmt.Println(".")

// if _, proceed, err := app.Load Config(); !proceed {
// return
// } else if err != nil {
// k.Fatal(err.Error())
// } else {
// k.Info("Starting Khedra with", "services", len(k.ActiveServices))
// // TODO: The following should happen in Load Config
// for _, svc := range k.ActiveServices {
// if controlSvc, ok := svc.(*services.ControlService); ok {
// controlSvc.AttachServiceManager(k)
// }
// }
// // TODO: The previous should happen in Load Config
// if err := k.StartAllServices(); err != nil {
// a.Fatal(err)
// }
// HandleSignals()
if err := serviceManager.StartAllServices(); err != nil {
k.Fatal(err.Error())
}
serviceManager.HandleSignals()
select {}

// select {}
// }
return nil
}
15 changes: 15 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"log/slog"
"os"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/types"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/utils"
"github.com/urfave/cli/v2"
)

Expand All @@ -20,6 +22,19 @@ type KhedraApp struct {
func NewKhedraApp() *KhedraApp {
var err error
k := KhedraApp{}

// If khedra is already running, one of these ports is serving the
// control API. We need to make sure it's not running and fail if
// it is.
cntlSvcPorts := []string{"8338", "8337", "8336", "8335"}
for _, port := range cntlSvcPorts {
if utils.PingServer("http://localhost:" + port) {
msg := fmt.Sprintf("Error: Khedra is already running (control service port :%s is in use). Quitting...", port)
fmt.Println(colors.Red+msg, colors.Off)
os.Exit(1)
}
}

if k.chainList, err = types.UpdateChainList(); err != nil {
fmt.Println(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ TELL THE USER WHICH SERVICES ARE BEING STARTED
RUN INIT EVERY TIME WE START?
FOR RUNNING CORE
os.Setenv("XDG_CONFIG_HOME", a.Config.ConfigPath)
os.Setenv("TB_SETTINGS_DEFAULTCHAIN", "mainnet")
os.Setenv("TB_SETTINGS_INDEXPATH", a.Config.IndexPath())
os.Setenv("TB_SETTINGS_CACHEPATH", a.Config.CachePath())
os.Setenv("TB_ SETTINGS_DEFAULTCHAIN", "mainnet")
os.Setenv("TB_ SETTINGS_INDEXPATH", a.Config.IndexPath())
os.Setenv("TB_ SETTINGS_CACHEPATH", a.Config.CachePath())
for chain, providerUrl := range a.Config.ProviderMap {
envKey := "TB_CHAINS_" + strings.ToUpper(chain) + "_RPCPROVIDER"
os.Setenv(envKey, providerUrl)
Expand Down
28 changes: 28 additions & 0 deletions pkg/utils/ping_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"context"
"net/http"
"time"
)

// PingServer sends a GET request to the provided URL and returns true if
// the server responds with a 200 status code.
func PingServer(serverUrl string) bool {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

req, err := http.NewRequestWithContext(ctx, "GET", serverUrl, nil)
if err != nil {
return false
}

clientHTTP := &http.Client{}
resp, err := clientHTTP.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()

return resp.StatusCode == http.StatusOK
}

0 comments on commit 2bece24

Please sign in to comment.