Skip to content

Commit

Permalink
fix cannot start issue with error substrate endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mohaijiang committed Apr 19, 2022
1 parent 87f1253 commit 6102936
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/frontend/node_modules
/frontend/dist
#/frontend/dist
/frontend/package-lock.json
Dockerfile
hamster-gateway
#hamster-gateway
.dockerignore
22 changes: 5 additions & 17 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ package cmd

import (
"fmt"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/hamster-shared/hamster-gateway/core"
context2 "github.com/hamster-shared/hamster-gateway/core/context"
chain2 "github.com/hamster-shared/hamster-gateway/core/modules/chain"
"github.com/hamster-shared/hamster-gateway/core/modules/config"
"github.com/hamster-shared/hamster-gateway/core/modules/time"
"github.com/hamster-shared/hamster-gateway/core/modules/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

// daemonCmd represents the daemon command
Expand Down Expand Up @@ -77,23 +74,14 @@ func NewContext(accountId, chainAddress string) context2.CoreContext {
cfg.ChainApi = chainAddress
}
_ = cm.Save(cfg)
substrateApi, err := gsrpc.NewSubstrateAPI(cfg.ChainApi)
if err != nil {
logrus.Error(err)
os.Exit(1)
}
reportClient, err := chain2.NewChainClient(cm, substrateApi)
if err != nil {
logrus.Error(err)
return context2.CoreContext{}
}

timeService := utils.NewTimerService()
stateService := time.NewStateService(reportClient, cm)
stateService := time.NewStateService(cm)

context := context2.CoreContext{
Cm: cm,
ReportClient: reportClient,
SubstrateApi: substrateApi,
Cm: cm,
//ReportClient: reportClient,
//SubstrateApi: substrateApi,
TimerService: timeService,
StateService: stateService,
}
Expand Down
8 changes: 6 additions & 2 deletions core/corehttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func setBootState(gin *MyContext) {
}

if op.Option {
gin.CoreContext.StateService.Start()
err := gin.CoreContext.StateService.Start()
if err != nil {
gin.JSON(http.StatusBadRequest, BadRequest(err.Error()))
}
} else {
gin.CoreContext.StateService.Stop()
}
Expand All @@ -64,7 +67,8 @@ func getBootState(gin *MyContext) {

func getP2pBW(gin *MyContext) {
nd := gin.CoreContext.StateService.Node
if !nd.IsOnline {

if nd == nil || !nd.IsOnline {
gin.JSON(http.StatusBadRequest, BadRequest("Not Online"))
return
}
Expand Down
31 changes: 20 additions & 11 deletions core/modules/time/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package time

import (
"fmt"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
chain2 "github.com/hamster-shared/hamster-gateway/core/modules/chain"
"github.com/hamster-shared/hamster-gateway/core/modules/config"
"github.com/hamster-shared/hamster-gateway/core/modules/p2p"
Expand All @@ -20,19 +21,32 @@ type StateService struct {
cm *config.ConfigManager
}

func NewStateService(reportClient chain2.ReportClient, cm *config.ConfigManager) *StateService {
func NewStateService(cm *config.ConfigManager) *StateService {
return &StateService{
reportClient: reportClient,
cm: cm,
cm: cm,
}
}

func (s *StateService) Start() {
func (s *StateService) Start() error {

if s.Running() {
return
return nil
}
cf, err := s.cm.GetConfig()

substrateApi, err := gsrpc.NewSubstrateAPI(cf.ChainApi)
if err != nil {
log.Error(err)
return err
}
reportClient, err := chain2.NewChainClient(s.cm, substrateApi)
if err != nil {
log.Error(err)
return err
}

s.reportClient = reportClient

s.ctx, s.cancel = context.WithCancel(context.Background())

node, err := p2p.RunDaemon(s.ctx)
Expand All @@ -42,12 +56,6 @@ func (s *StateService) Start() {
os.Exit(1)
}

cf, err := s.cm.GetConfig()
if err != nil {
log.Error("run ipfs daemon fail")
os.Exit(1)
}

localAddress := fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", cf.PublicIp, cf.PublicPort, node.Identity.String())

// 2: blockchain registration
Expand Down Expand Up @@ -77,6 +85,7 @@ func (s *StateService) Start() {
}
}(s.ctx)

return nil
}

func (s *StateService) Stop() {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/locales/lang/en/boot/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default {
accessPort: 'access port',
type: 'virtualization type',
resourceInfo: 'Resource information',
setStateFail: 'Set state fail',
};
1 change: 1 addition & 0 deletions frontend/src/locales/lang/zh-CN/boot/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export default {
accessPort: '访问端口',
type: '虚拟化类型',
resourceInfo: '资源信息',
setStateFail: '修改状态失败',
};
10 changes: 9 additions & 1 deletion frontend/src/views/gateway/boot/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import { Switch } from 'ant-design-vue';
import { getBootStateApi, setBootStateApi } from '/@/api/gateway/boot';
import { getConfigApi } from '/@/api/gateway/initialization';
import { message } from 'ant-design-vue';
const { pkg } = __APP_INFO__;
const { name } = pkg;
Expand All @@ -42,7 +43,14 @@
const onChange = function (checked) {
loading.value = true;
setBootStateApi(checked)
.then(() => {})
.then(() => {
console.log("success")
})
.catch(err => {
console.log(err)
option.value = !checked
message.error(t('boot.boot.setStateFail'));
})
.finally(() => {
loading.value = false;
});
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/views/gateway/bw/VisitAnalysis.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { onMounted,onUnmounted, ref, Ref } from 'vue';
import { useECharts } from '/@/hooks/web/useECharts';
import {getBandwidthApi} from '/@/api/gateway/bw'
import { getBootStateApi } from '/@/api/gateway/boot';
defineProps({
...basicProps,
Expand All @@ -24,7 +25,18 @@
const timer = ref()
const getData = function () {
const ensureData = function () {
getBootStateApi().then(state => {
if(state){
getData()
}
})
}
const getData = function (){
getBandwidthApi().then(data => {
if(timeArray.value.length >= xAxisLength){
Expand Down Expand Up @@ -115,7 +127,7 @@
onMounted(() => {
timer.value = setInterval(getData,2000);
timer.value = setInterval(ensureData,2000);
});
Expand Down

0 comments on commit 6102936

Please sign in to comment.