Skip to content

Commit

Permalink
优化适配器对err的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjie727 committed Jan 16, 2024
1 parent 10eac10 commit e30d8af
Show file tree
Hide file tree
Showing 7 changed files with 634 additions and 130 deletions.
667 changes: 587 additions & 80 deletions main.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/account/BlockAccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestBlockAccount(t *testing.T) {

// Call the Add RPC with test data
req := &pb.BlockAccountRequest{
AccountName: "w_admin",
AccountName: "a_admin",
}
_, err = client.BlockAccount(context.Background(), req)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tests/config/GetAvailablePartitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestGetAvailablePartitions(t *testing.T) {
// Set up a connection to the server
conn, err := grpc.Dial("localhost:8999", grpc.WithInsecure())
conn, err := grpc.Dial("localhost:8972", grpc.WithInsecure())
if err != nil {
t.Fatalf("did not connect: %v", err)
}
Expand All @@ -20,8 +20,8 @@ func TestGetAvailablePartitions(t *testing.T) {

// Call the Add RPC with test data
req := &pb.GetAvailablePartitionsRequest{
AccountName: "a_admin820",
UserId: "test02",
AccountName: "hpc0006173409",
UserId: "test07",
}
res, err := client.GetAvailablePartitions(context.Background(), req)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tests/job/SubmitFileAsJob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestSubmitScriptAsJob(t *testing.T) {
// stdout := "slurm-%j.out"
// stderr := "slurm-%j.out"
req := &pb.SubmitScriptAsJobRequest{
UserId: "root",
Script: "sssss",
UserId: "root",
Script: "#!/bin/bash\\n#SBATCH -o job.%j.out\\n#SBATCH -p compute\\n#SBATCH --qos=low\\n#SBATCH -J myFirstJob\\n#SBATCH --nodes=1 \\n#SBATCH --ntasks-per-node=1\\n\\nmkdir angle\\ncd angle\\nfor str1 in 100 150 200 250 300\\n do\\n mkdir angle$str1 \\n cd angle$str1\\n for str2 in {1..4}\\n do\\n mkdir $str2\\n done\\n cd ..\\ndone\\n\"",
}
res, err := client.SubmitScriptAsJob(context.Background(), req)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tests/user/BlockUserInAccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func TestBlockUserInAccount(t *testing.T) {

// Call the Add RPC with test data
req := &pb.BlockUserInAccountRequest{
UserId: "test07",
AccountName: "w_admin",
UserId: "test03",
AccountName: "a_admin",
}
_, err = client.BlockUserInAccount(context.Background(), req)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tests/user/UnblockUserInAccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func TestUnblockUserInAccount(t *testing.T) {

// Call the Add RPC with test data
req := &pb.UnblockUserInAccountRequest{
UserId: "test07",
AccountName: "w_admin",
UserId: "test03",
AccountName: "a_admin",
}
_, err = client.UnblockUserInAccount(context.Background(), req)
if err != nil {
Expand Down
77 changes: 37 additions & 40 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,27 @@ func ExecuteShellCommand(command string) int {

// 简单执行shell命令函数
func RunCommand(command string) (string, error) {
// cmd := exec.Command("bash", "-c", command)
// output, err := cmd.CombinedOutput()
// if err != nil {
// return "", err
// }
// return strings.TrimSpace(string(output)), nil
cmd := exec.Command("bash", "-c", command)
output, err := cmd.CombinedOutput()

// 创建一个 bytes.Buffer 用于捕获输出
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &output

// 执行命令
err := cmd.Run()

if err != nil {
return "", err
return output.String(), err
}
return strings.TrimSpace(string(output)), nil

return strings.TrimSpace(output.String()), nil
}

// 数据库配置信息
Expand All @@ -115,11 +130,19 @@ func DatabaseConfig() string {
// 获取全系统计算分区信息
func GetPatitionInfo() ([]string, error) {
shellCmd := "scontrol show partition| grep PartitionName=| awk -F'=' '{print $2}'| tr '\n' ','"
output, err := RunCommand(shellCmd)
cmd := exec.Command("bash", "-c", shellCmd)

// 创建一个 bytes.Buffer 用于捕获输出
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &output

// 执行命令
err := cmd.Run()
if err != nil {
return nil, err
}
resOutput := strings.Split(output, ",")
resOutput := strings.Split(strings.TrimSpace(output.String()), ",")
resOutput = resOutput[:len(resOutput)-1]
return resOutput, nil
}
Expand Down Expand Up @@ -217,41 +240,6 @@ func GetTimeLimit(timeLimit string) int64 {
}
}

func GetElapsedSeconds(cmd string) int64 {
var elapsedSeconds int64
ElapsedSecondsOutput, _ := RunCommand(cmd)
// 先判断作业时长中是否包含-
// 超过一天的作业
if strings.Contains(ElapsedSecondsOutput, "-") {
ElapsedSecondsList := strings.Split(ElapsedSecondsOutput, "-")
day, _ := strconv.Atoi(ElapsedSecondsList[0])
ElapsedSecondsListNew := strings.Split(ElapsedSecondsList[1], ":")
hours, _ := strconv.Atoi(ElapsedSecondsListNew[0])
minutes, _ := strconv.Atoi(ElapsedSecondsListNew[1])
seconds, _ := strconv.Atoi(ElapsedSecondsListNew[2])
return int64(seconds) + int64(minutes)*60 + int64(hours)*3600 + int64(day)*24*3600
} else {
// 没有超过一天的作业
ElapsedSecondsList := strings.Split(ElapsedSecondsOutput, ":")
log.Println(ElapsedSecondsList, 111)
if len(ElapsedSecondsList) == 2 {
minutes, _ := strconv.Atoi(ElapsedSecondsList[0])
seconds, _ := strconv.Atoi(ElapsedSecondsList[1])
elapsedSeconds = int64(seconds) + int64(minutes)*60
} else {
hours, _ := strconv.Atoi(ElapsedSecondsList[0])
minutes, _ := strconv.Atoi(ElapsedSecondsList[1])
seconds, _ := strconv.Atoi(ElapsedSecondsList[2])
elapsedSeconds = int64(seconds) + int64(minutes)*60 + int64(hours)*3600
}
// hours, _ := strconv.Atoi(ElapsedSecondsList[0])
// minutes, _ := strconv.Atoi(ElapsedSecondsList[1])
// seconds, _ := strconv.Atoi(ElapsedSecondsList[2])
// elapsedSeconds := int64(seconds) + int64(minutes)*60 + int64(hours)*3600
return elapsedSeconds
}
}

func GetGpuAllocsFromGpuId(matchCmd string, gpuId int, tresAlloc string) int32 {
var (
gpusAlloc int32
Expand Down Expand Up @@ -560,3 +548,12 @@ func SortJobInfo(sortKey string, sortOrder string, jobInfo []*pb.JobInfo) []*pb.
sortByKey(jobInfo, sortKey, sortOrder)
return jobInfo
}

func CheckSlurmStatus(result string) bool {
subStr := "Unable to contact slurm controller"
if strings.Contains(result, subStr) {
return true
} else {
return false
}
}

0 comments on commit e30d8af

Please sign in to comment.