Skip to content

Commit

Permalink
feat(start_cmd): Added start handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PeepoFrog committed Jun 7, 2024
1 parent e1813a4 commit 3bf2e8a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/shidai/internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

interxhandler "github.com/kiracore/sekin/src/shidai/internal/interx_handler"
interxhelper "github.com/kiracore/sekin/src/shidai/internal/interx_handler/interx_helper"
"github.com/kiracore/sekin/src/shidai/internal/logger"
mnemonicmanager "github.com/kiracore/sekin/src/shidai/internal/mnemonic_manager"
sekaihandler "github.com/kiracore/sekin/src/shidai/internal/sekai_handler"
Expand Down Expand Up @@ -41,6 +42,7 @@ var (
CommandHandlers = map[string]HandlerFunc{
"join": handleJoinCommand,
"status": handleStatusCommand,
"start": handleStartComamnd,
}
)

Expand Down Expand Up @@ -128,6 +130,10 @@ func handleJoinCommand(args map[string]interface{}) (string, error) {
if err != nil {
return "", fmt.Errorf("unable to start interx: %w", err)
}
err = interxhelper.CheckInterxStart(ctx)
if err != nil {
return "", err
}
// Example of using the IP, and similar for other fields
// This function would contain the logic specific to handling a join command
return fmt.Sprintf("Join command processed for IP: %s", ip), nil
Expand All @@ -143,3 +149,26 @@ func handleStatusCommand(args map[string]interface{}) (string, error) {

return "", nil
}

func handleStartComamnd(args map[string]interface{}) (string, error) {
err := sekaihandler.StartSekai()
if err != nil {
return "", fmt.Errorf("unable to start sekai: %w", err)
}
ctx := context.Background()

err = sekaihelper.CheckSekaiStart(ctx)
if err != nil {
return "", err
}

err = interxhandler.StartInterx()
if err != nil {
return "", fmt.Errorf("unable to start interx: %w", err)
}
err = interxhelper.CheckInterxStart(ctx)
if err != nil {
return "", err
}
return "Sekai and Interx started successfully", nil
}
80 changes: 80 additions & 0 deletions src/shidai/internal/interx_handler/interx_helper/interx_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package interxhelper

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/kiracore/sekin/src/shidai/internal/logger"
"github.com/kiracore/sekin/src/shidai/internal/types"
"github.com/kiracore/sekin/src/shidai/internal/types/endpoints/interx"
"go.uber.org/zap"
)

var log = logger.GetLogger()

func CheckInterxStart(ctx context.Context) error {
timeout := time.Second * 60
log.Debug("Checking if interx is started with timeout ", zap.Duration("timeout", timeout))
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

for {
select {
case <-ctx.Done():
return ctx.Err()
default:
status, err := GetInterxStatus(ctx, types.INTERX_CONTAINER_ADDRESS, strconv.Itoa(types.DEFAULT_INTERX_PORT))
if err != nil {
log.Warn("ERROR when getting interx status:", zap.Error(err))
time.Sleep(time.Second)
continue
}
latestBlock, err := strconv.Atoi(status.InterxInfo.LatestBlockHeight)
log.Debug("Latest block:", zap.Int("latestBlock", latestBlock))
if err != nil {
log.Warn("ERROR when converting latest block to string", zap.Error(err))
continue
// return err
}
if latestBlock > 0 {
return nil
}
}
}
return nil
}

func GetInterxStatus(ctx context.Context, ip, port string) (*interx.Status, error) {
client := &http.Client{}
ctxWithTO, c := context.WithTimeout(ctx, time.Second*10)
defer c()
// log.Printf("Getting net_info from: %v", ip)
url := fmt.Sprintf("http://%v:%v/api/status", ip, port)
req, err := http.NewRequestWithContext(ctxWithTO, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var nodeStatus interx.Status
err = json.Unmarshal(b, &nodeStatus)
if err != nil {
return nil, err
}
return &nodeStatus, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ func CheckSekaiStart(ctx context.Context) error {
case <-ctx.Done():
return ctx.Err()
default:

status, err := GetSekaidStatus(ctx, types.SEKAI_CONTAINER_ADDRESS, "26657")
if err != nil {
log.Warn("ERROR when getting sekai status:", zap.Error(err))
// return err
time.Sleep(time.Second)
continue
}
latestBlock, err := strconv.Atoi(status.Result.SyncInfo.LatestBlockHeight)
Expand All @@ -68,7 +67,6 @@ func CheckSekaiStart(ctx context.Context) error {
if latestBlock > 0 {
return nil
}
time.Sleep(time.Second)
}
}
}

0 comments on commit 3bf2e8a

Please sign in to comment.