Skip to content

Commit f5d189b

Browse files
committed
fix(aliyun):DescribeInvocationResults need to wait for the results
1 parent e6123f2 commit f5d189b

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

pkg/providers/alibaba/ecs/exec.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ecs
22

33
import (
4+
"time"
5+
46
"github.com/404tk/cloudtoolkit/pkg/schema"
57
"github.com/404tk/cloudtoolkit/utils/logger"
68
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
@@ -33,13 +35,33 @@ func RunCommand(client *ecs.Client, instanceId, region, ostype, cmd string) stri
3335
}
3436

3537
func describeInvocationResults(client *ecs.Client, region, cid string) string {
36-
request := ecs.CreateDescribeInvocationResultsRequest()
37-
request.Scheme = "https"
38-
request.RegionId = region
39-
request.CommandId = cid
40-
response, err := client.DescribeInvocationResults(request)
41-
if err != nil {
42-
return err.Error()
38+
t := 0
39+
for {
40+
time.Sleep(1 * time.Second)
41+
t += 1
42+
request := ecs.CreateDescribeInvocationResultsRequest()
43+
request.Scheme = "https"
44+
request.RegionId = region
45+
request.CommandId = cid
46+
request.ContentEncoding = "PlainText"
47+
response, err := client.DescribeInvocationResults(request)
48+
if err != nil {
49+
logger.Error(err)
50+
return ""
51+
}
52+
status := response.Invocation.InvocationResults.InvocationResult[0].InvokeRecordStatus
53+
switch status {
54+
case "Running":
55+
if t < 5 {
56+
continue
57+
}
58+
logger.Error("Timeout: Wait 5s by default.")
59+
return ""
60+
case "Finished":
61+
return response.Invocation.InvocationResults.InvocationResult[0].Output
62+
default:
63+
logger.Error("Exception status: " + status)
64+
return ""
65+
}
4366
}
44-
return response.Invocation.InvocationResults.InvocationResult[0].Output
4567
}

0 commit comments

Comments
 (0)