Make persist interval for remote state backend configurable (#1591)

Signed-off-by: Alex Ott <alexott@gmail.com>
Co-authored-by: Siddhartha Sonker <158144589+siddharthasonker95@users.noreply.github.com>
This commit is contained in:
Alex Ott
2024-05-28 19:47:16 +02:00
committed by GitHub
parent 8361438c52
commit 7e1a02cbb8
4 changed files with 37 additions and 1 deletions

View File

@@ -10,6 +10,8 @@ import (
"errors"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/opentofu/opentofu/internal/addrs"
@@ -27,6 +29,22 @@ import (
// test hook called between plan+apply during opApply
var testHookStopPlanApply func()
const (
defaultPersistInterval = 20 // arbitrary interval that's hopefully a sweet spot
persistIntervalEnvironmentVariableName = "TF_STATE_PERSIST_INTERVAL"
)
func getEnvAsInt(envName string, defaultValue int) int {
if val, exists := os.LookupEnv(envName); exists {
parsedVal, err := strconv.Atoi(val)
if err == nil {
return parsedVal
}
panic(fmt.Sprintf("Can't parse value '%s' of environment variable '%s'", val, envName))
}
return defaultValue
}
func (b *Local) opApply(
stopCtx context.Context,
cancelCtx context.Context,
@@ -84,7 +102,12 @@ func (b *Local) opApply(
// stateHook uses schemas for when it periodically persists state to the
// persistent storage backend.
stateHook.Schemas = schemas
stateHook.PersistInterval = 20 * time.Second // arbitrary interval that's hopefully a sweet spot
persistInterval := getEnvAsInt(persistIntervalEnvironmentVariableName, defaultPersistInterval)
if persistInterval < defaultPersistInterval {
panic(fmt.Sprintf("Can't use value lower than %d for env variable %s, got %d",
defaultPersistInterval, persistIntervalEnvironmentVariableName, persistInterval))
}
stateHook.PersistInterval = time.Duration(persistInterval) * time.Second
var plan *plans.Plan
// If we weren't given a plan, then we refresh/plan