From f784983d71b333fcc95828b8da8f74a32dfc74f7 Mon Sep 17 00:00:00 2001 From: Vivek Lingaiah Date: Fri, 29 Aug 2025 18:52:22 +0000 Subject: [PATCH] Introduce ForceRerun parameter for Linux RCv2 --- internal/cmds/cmds.go | 17 ++++++++++---- internal/cmds/cmds_test.go | 37 ++++++++++++++++++++++++++----- internal/handlersettings/types.go | 1 + 3 files changed, 45 insertions(+), 10 deletions(-) mode change 100644 => 100755 internal/handlersettings/types.go diff --git a/internal/cmds/cmds.go b/internal/cmds/cmds.go index 864c001..a1c810f 100755 --- a/internal/cmds/cmds.go +++ b/internal/cmds/cmds.go @@ -165,9 +165,18 @@ func uninstall(ctx *log.Context, h types.HandlerEnvironment, report *types.RunCo } func enablePre(ctx *log.Context, h types.HandlerEnvironment, metadata types.RCMetadata, c types.Cmd) error { + extensionEvents := createExtensionEventManager(ctx, h) + + // parse the extension handler settings + cfg, err1 := handlersettings.GetHandlerSettings(h.HandlerEnvironment.ConfigFolder, metadata.ExtName, metadata.SeqNum, ctx) + if err1 != nil { + errMessage := fmt.Sprintf("Failed to get configuration in enablePre: %v", err1) + extensionEvents.LogErrorEvent("enablePre", errMessage) + return errors.Wrap(err1, "failed to get configuration") + } // exit if this sequence number (a snapshot of the configuration) is already // processed. if not, save this sequence number before proceeding. - if shouldExit, err := checkAndSaveSeqNum(ctx, metadata.SeqNum, metadata.MostRecentSequence); err != nil { + if shouldExit, err := checkAndSaveSeqNum(ctx, metadata.SeqNum, metadata.MostRecentSequence, cfg.ForceRerun); err != nil { return errors.Wrap(err, "failed to process sequence number") } else if shouldExit { ctx.Log("event", "exit", "message", "the script configuration has already been processed, will not run again") @@ -388,14 +397,14 @@ func getOutput(ctx *log.Context, stdoutFileName string, stderrFileName string) ( // checkAndSaveSeqNum checks if the given seqNum is already processed // according to the specified seqNumFile and if so, returns true, // otherwise saves the given seqNum into seqNumFile returns false. -func checkAndSaveSeqNum(ctx log.Logger, seq int, mrseqPath string) (shouldExit bool, _ error) { +func checkAndSaveSeqNum(ctx log.Logger, seq int, mrseqPath string, forceRerun bool) (shouldExit bool, _ error) { ctx.Log("event", "comparing seqnum", "path", mrseqPath) - smaller, err := seqnum.IsSmallerThan(mrseqPath, seq) + isSavedSeqNumSmallerThanNewSeqNum, err := seqnum.IsSmallerThan(mrseqPath, seq) if err != nil { return false, errors.Wrap(err, "failed to check sequence number") } - if !smaller { + if !forceRerun && !isSavedSeqNumSmallerThanNewSeqNum { // stored sequence number is equals or greater than the current // sequence number. return true, nil diff --git a/internal/cmds/cmds_test.go b/internal/cmds/cmds_test.go index 205c5ef..e009074 100755 --- a/internal/cmds/cmds_test.go +++ b/internal/cmds/cmds_test.go @@ -148,7 +148,7 @@ func Test_commands_shouldReportStatus(t *testing.T) { func Test_checkAndSaveSeqNum_fails(t *testing.T) { // pass in invalid seqnum format - _, err := checkAndSaveSeqNum(log.NewNopLogger(), 0, "/non/existing/dir") + _, err := checkAndSaveSeqNum(log.NewNopLogger(), 0, "/non/existing/dir", false) require.NotNil(t, err) require.Contains(t, err.Error(), `failed to save sequence number`) } @@ -162,27 +162,52 @@ func Test_checkAndSaveSeqNum(t *testing.T) { nop := log.NewNopLogger() // no sequence number, 0 comes in. - shouldExit, err := checkAndSaveSeqNum(nop, 0, fp) + shouldExit, err := checkAndSaveSeqNum(nop, 0, fp, false) require.Nil(t, err) require.False(t, shouldExit) // file=0, seq=0 comes in. (should exit) - shouldExit, err = checkAndSaveSeqNum(nop, 0, fp) + shouldExit, err = checkAndSaveSeqNum(nop, 0, fp, false) require.Nil(t, err) require.True(t, shouldExit) // file=0, seq=1 comes in. - shouldExit, err = checkAndSaveSeqNum(nop, 1, fp) + shouldExit, err = checkAndSaveSeqNum(nop, 1, fp, false) require.Nil(t, err) require.False(t, shouldExit) // file=1, seq=1 comes in. (should exit) - shouldExit, err = checkAndSaveSeqNum(nop, 1, fp) + shouldExit, err = checkAndSaveSeqNum(nop, 1, fp, false) require.Nil(t, err) require.True(t, shouldExit) // file=1, seq=0 comes in. (should exit) - shouldExit, err = checkAndSaveSeqNum(nop, 1, fp) + shouldExit, err = checkAndSaveSeqNum(nop, 0, fp, false) + require.Nil(t, err) + require.True(t, shouldExit) + + // file=1, seq=0 comes in. ForceRerun=true, should not exit + shouldExit, err = checkAndSaveSeqNum(nop, 0, fp, true) + require.Nil(t, err) + require.False(t, shouldExit) + + // file=0, seq=0 comes in. ForceRerun=true, should not exit + shouldExit, err = checkAndSaveSeqNum(nop, 0, fp, true) + require.Nil(t, err) + require.False(t, shouldExit) + + // file=0, seq=1 comes in. ForceRerun=true, should not exit + shouldExit, err = checkAndSaveSeqNum(nop, 1, fp, true) + require.Nil(t, err) + require.False(t, shouldExit) + + // file=1, seq=5 comes in. ForceRerun=true, should not exit + shouldExit, err = checkAndSaveSeqNum(nop, 1, fp, true) + require.Nil(t, err) + require.False(t, shouldExit) + + // file=5, seq=0 comes in. ForceRerun=false, should exit + shouldExit, err = checkAndSaveSeqNum(nop, 0, fp, false) require.Nil(t, err) require.True(t, shouldExit) } diff --git a/internal/handlersettings/types.go b/internal/handlersettings/types.go old mode 100644 new mode 100755 index 49447a6..c10992d --- a/internal/handlersettings/types.go +++ b/internal/handlersettings/types.go @@ -87,6 +87,7 @@ type PublicSettings struct { TimeoutInSeconds int `json:"timeoutInSeconds,int"` AsyncExecution bool `json:"asyncExecution,bool"` TreatFailureAsDeploymentFailure bool `json:"treatFailureAsDeploymentFailure,bool"` + ForceRerun bool `json:"forceRerun,bool"` // List of artifacts to download before running the script Artifacts []PublicArtifactSource `json:"artifacts"`