Skip to content

Commit

Permalink
feat: add ondemand pod start
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-singh committed Apr 12, 2022
1 parent ad750c8 commit 2b8ddfa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
55 changes: 52 additions & 3 deletions api/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,65 @@ func RemovePod(id string) (ok bool, err error) {
return
}

func StartOnDemandPod(id string) (pod map[string]interface{}, err error) {
input := Input{
Query: `
mutation podResume($podId: String!) {
podResume(input: {podId: $podId}) {
id
desiredStatus
costPerHr
lastStatusChange
}
}
`,
Variables: map[string]interface{}{"podId": id},
}
res, err := Query(input)
if err != nil {
return
}
if res.StatusCode != 200 {
err = fmt.Errorf("PodBidResume: statuscode %d", res.StatusCode)
return
}
defer res.Body.Close()
rawData, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
data := make(map[string]interface{})
if err = json.Unmarshal(rawData, &data); err != nil {
return
}
gqlErrors, ok := data["errors"].([]interface{})
if ok && len(gqlErrors) > 0 {
firstErr, _ := gqlErrors[0].(map[string]interface{})
err = errors.New(firstErr["message"].(string))
return
}
gqldata, ok := data["data"].(map[string]interface{})
if !ok || gqldata == nil {
err = fmt.Errorf("data is nil: %s", string(rawData))
return
}
pod, ok = gqldata["podResume"].(map[string]interface{})
if !ok || pod == nil {
err = fmt.Errorf("pod is nil: %s", string(rawData))
return
}
return
}

func StartSpotPod(id string, bidPerGpu float32) (podBidResume map[string]interface{}, err error) {
input := Input{
Query: `
mutation Mutation($podId: String!, $bidPerGpu: Float!) {
podBidResume(input: {podId: $podId, bidPerGpu: $bidPerGpu}) {
id
consumerUserId
desiredStatus
machineId
version
costPerHr
lastStatusChange
}
}
`,
Expand Down
4 changes: 3 additions & 1 deletion cmd/pod/startPod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ var StartPodCmd = &cobra.Command{
var pod map[string]interface{}
if bidPerGpu > 0 {
pod, err = api.StartSpotPod(args[0], bidPerGpu)
} else {
pod, err = api.StartOnDemandPod(args[0])
}
cobra.CheckErr(err)

if pod["desiredStatus"] == "RUNNING" {
fmt.Printf(`pod "%s" started`, args[0])
fmt.Printf(`pod "%s" started with $%.3f / hr`, args[0], pod["costPerHr"])
fmt.Println()
} else {
cobra.CheckErr(fmt.Errorf(`pod "%s" start failed; status is %s`, args[0], pod["desiredStatus"]))
Expand Down

0 comments on commit 2b8ddfa

Please sign in to comment.