@@ -21,11 +21,10 @@ import (
2121 "time"
2222
2323 "github.com/aws/aws-sdk-go/aws"
24- "github.com/aws/aws-sdk-go/aws/awserr"
2524 "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
2625 "github.com/gorilla/websocket"
2726
28- "github.com/cortexlabs/cortex/pkg/lib/errors "
27+ awslib "github.com/cortexlabs/cortex/pkg/lib/aws "
2928 "github.com/cortexlabs/cortex/pkg/lib/sets/strset"
3029 "github.com/cortexlabs/cortex/pkg/operator/api/context"
3130 "github.com/cortexlabs/cortex/pkg/operator/config"
@@ -75,7 +74,7 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, apiName
7574 "apiName" : apiName ,
7675 }
7776
78- var latestContextID string
77+ var currentContextID string
7978 var podTemplateHash string
8079 var ctx * context.Context
8180
@@ -91,21 +90,20 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, apiName
9190 if ctx == nil {
9291 writeString (socket , "\n deployment " + appName + "not found" )
9392 closeSocket (socket )
94- return
93+ continue
9594 }
9695
9796 if _ , ok := ctx .APIs [apiName ]; ! ok {
9897 writeString (socket , "\n api " + apiName + " was not found in latest deployment" )
9998 closeSocket (socket )
100- return
101- }
102-
103- if len (latestContextID ) != 0 && ctx .ID != latestContextID {
104- writeString (socket , "\n a new deployment was detected, streaming logs from the latest deployment" )
99+ continue
105100 }
106101
107- if ctx .ID != latestContextID {
108- latestContextID = ctx .ID
102+ if ctx .ID != currentContextID {
103+ if len (currentContextID ) != 0 {
104+ writeString (socket , "\n a new deployment was detected, streaming logs from the latest deployment" )
105+ }
106+ currentContextID = ctx .ID
109107 podSearchLabels ["workloadID" ] = ctx .APIs [apiName ].WorkloadID
110108 podTemplateHash = ""
111109 wrotePending = false
@@ -121,7 +119,7 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, apiName
121119 if err != nil {
122120 writeString (socket , err .Error ())
123121 closeSocket (socket )
124- return
122+ continue
125123 }
126124 }
127125
@@ -141,8 +139,7 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, apiName
141139 })
142140
143141 if err != nil {
144- awsErr := errors .Cause (err ).(awserr.Error )
145- if awsErr .Code () == "ResourceNotFoundException" {
142+ if awslib .CheckErrCode (err , "ResourceNotFoundException" ) {
146143 if ! wrotePending {
147144 writeString (socket , "pending..." )
148145 wrotePending = true
@@ -193,10 +190,12 @@ func getPodTemplateHash(labels map[string]string) (string, error) {
193190}
194191
195192func writeString (socket * websocket.Conn , message string ) {
193+ socket .SetWriteDeadline (time .Now ().Add (socketWriteDeadlineWait ))
196194 socket .WriteMessage (websocket .TextMessage , []byte (message ))
197195}
198196
199197func closeSocket (socket * websocket.Conn ) {
198+ socket .SetWriteDeadline (time .Now ().Add (socketWriteDeadlineWait ))
200199 socket .WriteMessage (websocket .CloseMessage , websocket .FormatCloseMessage (websocket .CloseNormalClosure , "" ))
201200 time .Sleep (socketCloseGracePeriod )
202201}
0 commit comments