@@ -1567,8 +1567,13 @@ const DefaultVersion Version = -1
15671567// CadenceChangeVersion is used as search attributes key to find workflows with specific change version.
15681568const CadenceChangeVersion = "CadenceChangeVersion"
15691569
1570- // GetVersionOptions contains options for GetVersion
1571- type GetVersionOptions struct {
1570+ // GetVersionOption is an interface for functional options that can be applied to GetVersion
1571+ type GetVersionOption interface {
1572+ apply (* getVersionConfig )
1573+ }
1574+
1575+ // getVersionConfig holds the configuration for GetVersion
1576+ type getVersionConfig struct {
15721577 // CustomVersion is used to force GetVersion to return a specific version
15731578 // instead of maxSupported version. Set up via ExecuteWithVersion option.
15741579 CustomVersion * Version
@@ -1578,75 +1583,39 @@ type GetVersionOptions struct {
15781583 UseMinVersion bool
15791584}
15801585
1586+ // customVersionOption forces GetVersion to return a specific version
1587+ type customVersionOption Version
1588+
1589+ func (c customVersionOption ) apply (config * getVersionConfig ) {
1590+ version := Version (c )
1591+ config .CustomVersion = & version
1592+ }
1593+
15811594// ExecuteWithVersion forces a specific version to be returned when GetVersion is executed for the first time,
1582- // instead of returning maxSupported version. This option can be used when you want to separate the versioning of the workflow code and
1583- // activation of the new logic in the workflow code, to ensure that your changes can be safely rolled back, if needed.
1584- //
1585- // For example, initially a workflow has the following code:
1586- //
1587- // err = workflow.ExecuteActivity(ctx, foo).Get(ctx, nil)
1588- //
1589- // It should be updated to:
1590- //
1591- // err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1592- //
1593- // Following the steps below, your changes will be forward and backward compatible, keeping possible a safe rollback
1594- // to the previous version of the workflow code:
1595- //
1596- // 1. Keep execution of foo activity, add a support of bar activity
1597- //
1598- // v := GetVersion(ctx, "fooChange", DefaultVersion, 1, ExecuteWithVersion(DefaultVersion))
1599- // if v == DefaultVersion {
1600- // err = workflow.ExecuteActivity(ctx, foo).Get(ctx, nil)
1601- // } else {
1602- // err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1603- // }
1604- //
1605- // The code above supports replaying of workflow execution with both versions DefaultVersion and 1.
1606- // All new workflow executions will execute foo activity, because GetVersion with ExecuteWithMinVersion option
1607- // returns DefaultVersion, that will be recorded into the workflow history.
1608- //
1609- // 2. Enable execution of bar activity
1610- //
1611- // v := GetVersion(ctx, "fooChange", DefaultVersion, 1)
1612- // if v == DefaultVersion {
1613- // err = workflow.ExecuteActivity(ctx, foo).Get(ctx, nil)
1614- // } else {
1615- // err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1616- // }
1617- //
1618- // The code above supports replaying of workflow execution with both versions DefaultVersion and 1.
1619- // All new workflow executions will execute bar activity, because
1620- // GetVersion returns the maximum supported version - 1, that will be recorded into the workflow history.
1621- //
1622- // 3. Remove a support of foo activity:
1623- //
1624- // GetVersion(ctx, "fooChange", 1, 1)
1625- // err = workflow.ExecuteActivity(ctx, bar).Get(ctx, nil)
1626- //
1627- // When there are no workflow executions running DefaultVersion the support of foo activity can be removed.
1628- //
1629- // ExecuteWithVersion option is useful when you want to ensure that your changes can be safely rolled back if needed.
1630- func ExecuteWithVersion (version Version ) GetVersionOptions {
1631- return GetVersionOptions {
1632- CustomVersion : & version ,
1633- }
1595+ // instead of returning maxSupported version. Check public documentation of workflow.ExecuteWithVersion
1596+ func ExecuteWithVersion (version Version ) GetVersionOption {
1597+ return customVersionOption (version )
1598+ }
1599+
1600+ // useMinVersionOption forces GetVersion to return minSupported version
1601+ type useMinVersionOption struct {}
1602+
1603+ func (useMinVersionOption ) apply (config * getVersionConfig ) {
1604+ config .UseMinVersion = true
16341605}
16351606
16361607// ExecuteWithMinVersion forces minSupported version to be returned when GetVersion is executed for the first time,
1637- // instead of returning maxSupported version. The option is equivalent to ExecuteWithVersion(minSupportedVersion).
1638- // Check the ExecuteWithVersion documentation for more details.
1639- func ExecuteWithMinVersion () GetVersionOptions {
1640- return GetVersionOptions {
1641- UseMinVersion : true ,
1642- }
1608+ // instead of returning maxSupported version. Check public documentation of workflow.ExecuteWithMinVersion
1609+ func ExecuteWithMinVersion () GetVersionOption {
1610+ return useMinVersionOption {}
16431611}
16441612
16451613// GetVersion is used to safely perform backwards incompatible changes to workflow definitions.
16461614// It is not allowed to update workflow code while there are workflows running as it is going to break
16471615// determinism. The solution is to have both old code that is used to replay existing workflows
16481616// as well as the new one that is used when it is executed for the first time.
1649- // GetVersion returns maxSupported version (to return another version, check GetVersionOption),
1617+ // GetVersion returns maxSupported version
1618+ // (to return another version for a potential safe rollback, check documentation for ExecuteWithVersion and ExecuteWithMinVersion),
16501619// when is executed for the first time. This version is recorded into the
16511620// workflow history as a marker event. Even if maxSupported version is changed the version that was recorded is
16521621// returned on replay. DefaultVersion constant contains version of code that wasn't versioned before.
@@ -1708,12 +1677,12 @@ func ExecuteWithMinVersion() GetVersionOptions {
17081677// } else {
17091678// err = workflow.ExecuteActivity(ctx, qux, data).Get(ctx, nil)
17101679// }
1711- func GetVersion (ctx Context , changeID string , minSupported , maxSupported Version , opts ... GetVersionOptions ) Version {
1680+ func GetVersion (ctx Context , changeID string , minSupported , maxSupported Version , opts ... GetVersionOption ) Version {
17121681 i := getWorkflowInterceptor (ctx )
17131682 return i .GetVersion (ctx , changeID , minSupported , maxSupported , opts ... )
17141683}
17151684
1716- func (wc * workflowEnvironmentInterceptor ) GetVersion (ctx Context , changeID string , minSupported , maxSupported Version , opts ... GetVersionOptions ) Version {
1685+ func (wc * workflowEnvironmentInterceptor ) GetVersion (ctx Context , changeID string , minSupported , maxSupported Version , opts ... GetVersionOption ) Version {
17171686 return wc .env .GetVersion (changeID , minSupported , maxSupported , opts ... )
17181687}
17191688
0 commit comments