Files
fgueryandGitHub 599477b709 fix(EN-549): remove config and connector dependencies (#622)
* chore: rename RunNextTasks workflow

* feat: added a trimmed down type for connector only containing the id

* feat: make runNextTask use connectorIdOnly

* feat: added isScheduledForDeletion to plugin interface

* feat: add scheduledForDeletion at plugin level

* feat: refactor manager.Load to take a connector instead of parts of it

* feat: saves the connector's scheduledFOrDeletion into the plugin

* feat: use plugin.IsScheduledForDeletion instead of loading connector

* fix: make e2e tests less flaky

* fix: remove config from most request types

* feat: (WIP) remove config from runNextTask

* fix: remove unneeded method definitions in each plugin

* fix: simplify RunNextFlow as anyway we need to change the parameters

* fix: add a workflow compatible with previous version for runNextTasks

* fix(EN-577): added back RunSendEvents as a deprecated workflow

* fix(EN-577): added back RunUpdatePaymentInitiationFromPayment as a deprecated workflow

* fix: fix linting directives

* fix: replace old usage with refactored one

* fix: added back missing deprecated activities

* fix: ensure fetch_next_balance works with v3.0 code

* fix: simplify a bit the versionning etc

* fix: quick refactor before applying solution elsewhere

* fix: add back RunSendEvents and RunNextTask child workflows where necessary for backward compatibility

* fix: add back missing activity, seems like it's mandatory?

* fix: run payment initiation update as child workflow when necessary

* fix: missing activy call in test, refine some todos

* fix: lint

* fix: ensure we always reload plugins into the manager -- so exposing isScheduledForDeletion

* fix: ensure we stop storing clear config in temporal

* fix: make store config support plain text config

* fix: some comments

* fix: change runSendEvent workflow to be match what we had in 3.0

* feat: enable worker versionning to avoid workflow from new version to get executed on old code

* fix: lint

* fix: revert worker versionning

* fix: minor code review comments

* fix: add context to Encrypt & Decrypt methods

* fix: improve lint, add couple tests

* fix: use atomic to handle isScheduledForDeletion

* fix: make create_events work on migration

* fix: avoid returning channel; just send what is needed in runSendEventAsChildWorkflow and runNextTaskAsChildWorkflow

* fix: remove extra call to installWorkflow on start plugin

* fix: typo in error log line

* fix: simplify errChan initialization

* fix: clarify feature flag names
2026-01-05 11:09:45 +00:00

303 lines
7.1 KiB
Go

package workflow
import (
"fmt"
"time"
"github.com/formancehq/payments/internal/connectors/engine/activities"
"github.com/formancehq/payments/internal/models"
"github.com/go-faster/errors"
"github.com/google/uuid"
"go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/workflow"
)
type sendEventActivityFunction func(ctx workflow.Context) error
type SendEvents struct {
Account *models.Account
Balance *models.Balance
BankAccount *models.BankAccount
Payment *models.Payment
PaymentDeleted *models.PaymentID
ConnectorReset *models.ConnectorID
PoolsCreation *models.Pool
PoolsDeletion *uuid.UUID
PaymentInitiation *models.PaymentInitiation
PaymentInitiationAdjustment *models.PaymentInitiationAdjustment
PaymentInitiationRelatedPayment *models.PaymentInitiationRelatedPayments
UserPendingDisconnect *models.UserConnectionPendingDisconnect
UserDisconnected *models.UserDisconnected
UserConnectionDisconnected *models.UserConnectionDisconnected
UserConnectionReconnected *models.UserConnectionReconnected
UserLinkStatus *models.UserLinkSessionFinished
UserConnectionDataSynced *models.UserConnectionDataSynced
Task *models.Task
}
func (w Workflow) runSendEvents(
ctx workflow.Context,
sendEvents SendEvents,
) error {
if sendEvents.Account != nil {
err := sendEvent(
ctx,
sendEvents.Account.IdempotencyKey(),
&sendEvents.Account.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendAccount(
infiniteRetryContext(ctx),
*sendEvents.Account,
)
},
)
if err != nil {
return err
}
}
if sendEvents.Balance != nil {
err := sendEvent(
ctx,
sendEvents.Balance.IdempotencyKey(),
&sendEvents.Balance.AccountID.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendBalance(
infiniteRetryContext(ctx),
*sendEvents.Balance,
)
},
)
if err != nil {
return err
}
}
if sendEvents.BankAccount != nil {
err := sendEvent(
ctx,
sendEvents.BankAccount.IdempotencyKey(),
nil,
func(ctx workflow.Context) error {
return activities.EventsSendBankAccount(
infiniteRetryContext(ctx),
*sendEvents.BankAccount,
)
},
)
if err != nil {
return err
}
}
if sendEvents.Payment != nil {
for _, adjustment := range sendEvents.Payment.Adjustments {
err := sendEvent(
ctx,
adjustment.IdempotencyKey(),
&sendEvents.Payment.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendPayment(
infiniteRetryContext(ctx),
*sendEvents.Payment,
adjustment,
)
},
)
if err != nil {
return err
}
}
}
if sendEvents.ConnectorReset != nil {
now := workflow.Now(ctx).UTC()
err := sendEvent(
ctx,
fmt.Sprintf("%s-%s", sendEvents.ConnectorReset.String(), now.Format(time.RFC3339Nano)),
nil,
func(ctx workflow.Context) error {
return activities.EventsSendConnectorReset(
infiniteRetryContext(ctx),
*sendEvents.ConnectorReset,
now,
)
},
)
if err != nil {
return err
}
}
if sendEvents.PoolsCreation != nil {
err := sendEvent(
ctx,
sendEvents.PoolsCreation.IdempotencyKey(),
nil,
func(ctx workflow.Context) error {
return activities.EventsSendPoolCreation(
infiniteRetryContext(ctx),
*sendEvents.PoolsCreation,
)
},
)
if err != nil {
return err
}
}
if sendEvents.PoolsDeletion != nil {
err := sendEvent(
ctx,
sendEvents.PoolsDeletion.String(),
nil,
func(ctx workflow.Context) error {
return activities.EventsSendPoolDeletion(
infiniteRetryContext(ctx),
*sendEvents.PoolsDeletion,
)
},
)
if err != nil {
return err
}
}
if sendEvents.PaymentInitiation != nil {
err := sendEvent(
ctx,
sendEvents.PaymentInitiation.IdempotencyKey(),
&sendEvents.PaymentInitiation.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendPaymentInitiation(
infiniteRetryContext(ctx),
*sendEvents.PaymentInitiation,
)
},
)
if err != nil {
return err
}
}
if sendEvents.PaymentInitiationAdjustment != nil {
err := sendEvent(
ctx,
sendEvents.PaymentInitiationAdjustment.IdempotencyKey(),
&sendEvents.PaymentInitiationAdjustment.ID.PaymentInitiationID.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendPaymentInitiationAdjustment(
infiniteRetryContext(ctx),
*sendEvents.PaymentInitiationAdjustment,
)
},
)
if err != nil {
return err
}
}
if sendEvents.PaymentInitiationRelatedPayment != nil {
err := sendEvent(
ctx,
sendEvents.PaymentInitiationRelatedPayment.IdempotencyKey(),
&sendEvents.PaymentInitiationRelatedPayment.PaymentInitiationID.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendPaymentInitiationRelatedPayment(
infiniteRetryContext(ctx),
*sendEvents.PaymentInitiationRelatedPayment,
)
},
)
if err != nil {
return err
}
}
if sendEvents.Task != nil {
err := sendEvent(
ctx,
sendEvents.Task.IdempotencyKey(),
sendEvents.Task.ConnectorID,
func(ctx workflow.Context) error {
return activities.EventsSendTaskUpdated(
infiniteRetryContext(ctx),
*sendEvents.Task,
)
},
)
if err != nil {
return err
}
}
return nil
}
func sendEvent(
ctx workflow.Context,
idempotencyKey string,
connectorID *models.ConnectorID,
fn sendEventActivityFunction,
) error {
isExisting, err := activities.StorageEventsSentExists(
infiniteRetryContext(ctx),
idempotencyKey,
connectorID,
)
if err != nil {
return err
}
if !isExisting {
// event was not sent yet
if err := fn(ctx); err != nil {
return err
}
if err := activities.StorageEventsSentStore(
infiniteRetryContext(ctx),
models.EventSent{
ID: models.EventID{
EventIdempotencyKey: idempotencyKey,
ConnectorID: connectorID,
},
ConnectorID: connectorID,
SentAt: workflow.Now(ctx).UTC(),
},
); err != nil {
return err
}
}
return nil
}
func (w Workflow) runSendEventAsChildWorkflow(ctx workflow.Context, wg workflow.WaitGroup, evts SendEvents, errChan chan<- error) {
wg.Add(1)
workflow.Go(ctx, func(ctx workflow.Context) {
defer wg.Done()
if err := workflow.ExecuteChildWorkflow(
workflow.WithChildOptions(
ctx,
workflow.ChildWorkflowOptions{
TaskQueue: w.getDefaultTaskQueue(),
ParentClosePolicy: enums.PARENT_CLOSE_POLICY_ABANDON,
SearchAttributes: map[string]interface{}{
SearchAttributeStack: w.stack,
},
},
),
RunSendEvents, // nolint: staticcheck // ignore deprecated
evts,
).Get(ctx, nil); err != nil {
errChan <- errors.Wrap(err, "sending events")
}
})
}
// RunSendEvents
// Deprecated: should not be used after version 3.0; we keep it in 3.1 for ongoing workflows.
const RunSendEvents = "RunSendEvents" //nolint:staticcheck // ignore deprecation