Skip to content

Commit

Permalink
Cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
abing258 committed Mar 20, 2024
1 parent ff7d720 commit 5d5e321
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions pkg/service/icp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
WalletTopUp = "dfx ledger top-up %s --amount %s --network %s --identity %s"
AccountId = "dfx ledger account-id --identity %s"
GetPrincipal = "dfx identity get-principal--identity %s"
CanisterStatus = "dfx canister status %s --network %s"
CanisterStatus = "dfx canister status %s --network %s --identity %s"
)

type IcpService struct {
Expand Down Expand Up @@ -279,7 +279,7 @@ func (i *IcpService) RechargeCanister(userId uint, rechargeCanisterParam paramet
if err != nil {
return vo, err
}
data, err := i.queryCanisterStatus(rechargeCanisterParam.CanisterId)
data, err := i.queryCanisterStatus(rechargeCanisterParam.CanisterId, userIcp.IdentityName)
if err != nil {
return vo, err
}
Expand Down Expand Up @@ -436,9 +436,9 @@ func (i *IcpService) QueryIcpCanisterList(projectId string, page, size int) (*vo
return &pageData, err
}
for _, canister := range canisters {
logger.Info(canister.Cycles)
identityName := strconv.Itoa(int(canister.FkUserId))
if !canister.Cycles.Valid {
data, err := i.queryCanisterStatus(canister.CanisterId)
data, err := i.queryCanisterStatus(canister.CanisterId, identityName)
logger.Debugf("balance data is %s:", data.Balance)
if err == nil {
logger.Info("start save balance")
Expand All @@ -455,7 +455,7 @@ func (i *IcpService) QueryIcpCanisterList(projectId string, page, size int) (*vo
} else {
isThreeHoursAgo := isTimeThreeHoursAgo(canister.UpdateTime.Time, time.Now())
if isThreeHoursAgo {
data, err := i.queryCanisterStatus(canister.CanisterId)
data, err := i.queryCanisterStatus(canister.CanisterId, identityName)
if err == nil {
canister.Cycles = sql.NullString{
String: data.Balance,
Expand All @@ -478,10 +478,10 @@ func (i *IcpService) QueryIcpCanisterList(projectId string, page, size int) (*vo
return &pageData, nil
}

func (i *IcpService) queryCanisterStatus(canisterId string) (vo.CanisterStatusRes, error) {
func (i *IcpService) queryCanisterStatus(canisterId string, identityName string) (vo.CanisterStatusRes, error) {
var res vo.CanisterStatusRes
canisterStatusSprintf := CanisterStatus
canisterCmd := fmt.Sprintf(canisterStatusSprintf, canisterId, i.network)
canisterCmd := fmt.Sprintf(canisterStatusSprintf, canisterId, i.network, identityName)
logger.Infof("exec cmd is %s", canisterCmd)
cmd := exec.Command("bash", "-c", canisterCmd)
out, err := cmd.CombinedOutput()
Expand Down
5 changes: 3 additions & 2 deletions pkg/service/workflow_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -783,7 +784,7 @@ func (w *WorkflowService) syncInternetComputerDeploy(projectId uuid.UUID, workfl

// 使用 First 查询满足条件的第一条数据
if err := w.db.Model(db.IcpCanister{}).Where("project_id = ? and canister_id = ?", projectId.String(), canisterId).First(&icpCanister).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
fmt.Println("数据不存在")
icpCanister.CanisterId = canisterId
icpCanister.CreateTime = sql.NullTime{Time: time.Now(), Valid: true}
Expand All @@ -796,7 +797,7 @@ func (w *WorkflowService) syncInternetComputerDeploy(projectId uuid.UUID, workfl

var project db.Project
if err := w.db.Model(db.Project{}).Where("id = ?", projectId.String()).First(&project).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
fmt.Println("project 数据不存在")
} else {
fmt.Println("查询数据时发生错误:", err)
Expand Down

0 comments on commit 5d5e321

Please sign in to comment.