Skip to content

Commit

Permalink
Use success state instead of warning for dummy status files during Up…
Browse files Browse the repository at this point in the history
…grade (#38)
  • Loading branch information
vivlingaiah committed Aug 21, 2024
1 parent 71f12d4 commit 827fa13
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
21 changes: 20 additions & 1 deletion internal/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ func createDummyStatusFilesIfNeeded(ctx log.Logger, mrseqFilesNameList *list.Lis
var statusFileName string
var statusFilePath string
var errorMessage string
var instanceViewMessage string
var err error
var content []byte
var allErr error = errors.New("Refer to all error messages above.")
Expand Down Expand Up @@ -515,7 +516,25 @@ func createDummyStatusFilesIfNeeded(ctx log.Logger, mrseqFilesNameList *list.Lis
var rootStatusJson []byte
// If status file path does not exist, create a dummy status file to prevent poll status timeouts for already executed Run Commands after upgrade.
if !handlersettings.DoesFileExist(statusFilePath) {
statusReport := types.NewStatusReport(types.StatusWarning, "Enable", "The script has been executed. However, the execution state, output, error are unknown.")

timeNow := time.Now().UTC().Format(time.RFC3339)
instanceView := types.RunCommandInstanceView{
ExecutionState: types.Unknown, // InstanceView executionState is Unknown because we do not know whether previously executed Run command's state for sure.
ExecutionMessage: "The script has been executed. However, the instanceView's execution state, output, error are unknown.",
ExitCode: 0,
Output: "Unknown",
Error: "Unknown",
StartTime: timeNow,
EndTime: timeNow,
}
instanceViewMessage, err = instanceview.SerializeInstanceView(&instanceView)
if err != nil {
errorMessage = fmt.Sprintf("Failed to serialize unknown instanceView, error is '%s'", err.Error())
allErr = errors.Wrap(allErr, errorMessage)
continue
}

statusReport := types.NewStatusReport(types.StatusSuccess, "Enable", instanceViewMessage)
rootStatusJson, err = status.MarshalStatusReportIntoJson(statusReport, true)
if err != nil {
errorMessage = fmt.Sprintf("failed to marshal status report into json for status file '%s' with error '%s'", statusFilePath, err.Error())
Expand Down
4 changes: 2 additions & 2 deletions internal/instanceview/instanceview.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func ReportInstanceView(ctx *log.Context, hEnv types.HandlerEnvironment, metadat
return nil
}

msg, err := serializeInstanceView(instanceview)
msg, err := SerializeInstanceView(instanceview)
if err != nil {
return err
}

return c.Functions.ReportStatus(ctx, hEnv, metadata, t, c, msg)
}

func serializeInstanceView(instanceview *types.RunCommandInstanceView) (string, error) {
func SerializeInstanceView(instanceview *types.RunCommandInstanceView) (string, error) {
bytes, err := instanceview.Marshal()
if err != nil {
return "", fmt.Errorf("status: failed to marshal into json: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/instanceview/instanceview_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_serializeInstanceView(t *testing.T) {
StartTime: time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC).Format(time.RFC3339),
EndTime: time.Date(2000, 2, 1, 12, 35, 0, 0, time.UTC).Format(time.RFC3339),
}
msg, err := serializeInstanceView(&instanceView)
msg, err := SerializeInstanceView(&instanceView)
require.Nil(t, err)
require.NotNil(t, msg)
expectedMsg := "{\"executionState\":\"Running\",\"executionMessage\":\"Completed\",\"output\":\"Script output stream with \\\\ \\n \\t \\\" \",\"error\":\"Script error stream\",\"exitCode\":0,\"startTime\":\"2000-02-01T12:30:00Z\",\"endTime\":\"2000-02-01T12:35:00Z\"}"
Expand Down Expand Up @@ -67,6 +67,6 @@ func Test_reportInstanceView(t *testing.T) {
require.Equal(t, types.StatusSuccess, r[0].Status.Status)
require.Equal(t, types.CmdEnableTemplate.Name, r[0].Status.Operation)

msg, _ := serializeInstanceView(&instanceView)
msg, _ := SerializeInstanceView(&instanceView)
require.Equal(t, msg, r[0].Status.FormattedMessage.Message)
}
4 changes: 0 additions & 4 deletions internal/types/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ const (

// StatusSuccess indicates the operation succeeded
StatusSuccess StatusType = "success"

// StatusWarning indicates the operation was executed, but with one of the below conditions:
// 1) Status files have been lost. So, exact execution status (error or success), output and error are not known.
StatusWarning StatusType = "warning"
)

// Status is used for serializing status in a manner the server understands
Expand Down

0 comments on commit 827fa13

Please sign in to comment.