diff --git a/db-connector.go b/db-connector.go index 119188f..4b02e66 100755 --- a/db-connector.go +++ b/db-connector.go @@ -8193,6 +8193,31 @@ func FixWorkflowPosition(ctx context.Context, workflow Workflow) Workflow { } func SetWorkflow(ctx context.Context, workflow Workflow, id string, optionalEditedSecondsOffset ...int) error { + // FIXME: Due to a possibility of ID reusage on duplication, we re-randomize ID's IF the workflow is new + // Due to caching, this is kind of fine. + foundWorkflow, err := GetWorkflow(ctx, id) + if err != nil || foundWorkflow.ID == "" { + log.Printf("[INFO] Workflow %s doesn't exist, randomizing IDs for Triggers", id) + + // Old ID + Org ID as seed -> generate new uuid + for triggerIndex, trigger := range workflow.Triggers { + uuidSeed := fmt.Sprintf("%s_%s", trigger.ID, workflow.OrgId) + newTriggerId := uuid.NewV5(uuid.NamespaceOID, uuidSeed).String() + for branchIndex, branch := range workflow.Branches { + if branch.SourceID == trigger.ID { + workflow.Branches[branchIndex].SourceID = newTriggerId + } + + if branch.DestinationID == trigger.ID { + workflow.Branches[branchIndex].DestinationID = newTriggerId + } + } + + workflow.Triggers[triggerIndex].ID = newTriggerId + workflow.Triggers[triggerIndex].Status = "stopped" + } + } + // Overwriting to be sure these are matching // No real point in having id + workflow.ID anymore id = workflow.ID diff --git a/shared.go b/shared.go index 4ea7677..9addda2 100755 --- a/shared.go +++ b/shared.go @@ -16651,7 +16651,6 @@ func GetReplacementNodes(ctx context.Context, execution WorkflowExecution, trigg } childNodes := FindChildNodes(workflowExecution.Workflow, workflowAction, []string{}, []string{}) - //log.Printf("Found %d childnodes of %s", len(childNodes), workflowAction) newActions := []Action{} branches := []Branch{} @@ -29142,9 +29141,16 @@ func checkExecutionStatus(ctx context.Context, exec *WorkflowExecution) *Workflo if workflowChanged { workflow.Actions = originalActions - err = SetWorkflow(ctx, *workflow, workflow.ID) + // This causes too many writes and can't be handled at scale. Removing for now. Only setting cache. + cacheKey := fmt.Sprintf("workflow_%s", workflow.ID) + data, err := json.Marshal(workflow) if err != nil { - log.Printf("[ERROR] Failed updating workflow from execution validator. This is NOT critical as we keep cache %s: %s", workflow.ID, err) + log.Printf("[ERROR] Failed marshalling validation of %s: %s", workflow.ID, err) + } else { + err = SetCache(ctx, cacheKey, data, 30) + if err != nil { + log.Printf("[ERROR] Failed setting cache for workflow '%s': %s", cacheKey, err) + } } }