Skip to content

chore: workflow internals improvementss #9455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
daaefe4
chore: AddToCart workflow improvements
adrien2p Oct 3, 2024
2590005
WIP
adrien2p Oct 3, 2024
2db5a67
update when
adrien2p Oct 3, 2024
f950fe3
WIP
adrien2p Oct 3, 2024
dee0b8f
fix
adrien2p Oct 3, 2024
a9def72
fixes
adrien2p Oct 3, 2024
f135919
WIP
adrien2p Oct 3, 2024
5dc4564
WIP
adrien2p Oct 3, 2024
8eddd5b
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 3, 2024
7c8abce
improve value resolution
adrien2p Oct 3, 2024
6794bef
update fields usage
adrien2p Oct 4, 2024
b011b3b
proxies
adrien2p Oct 4, 2024
7a29a82
resolve conflicts
adrien2p Oct 4, 2024
09bf1fc
resolve conflicts
adrien2p Oct 4, 2024
e8938a2
fix integration
adrien2p Oct 4, 2024
e440613
WIP
adrien2p Oct 4, 2024
6404d4e
revert workflows changes
adrien2p Oct 4, 2024
1832981
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 4, 2024
c97be5d
ignore event subscription when worker mode is server
carlos-r-l-rodrigues Oct 4, 2024
a2b92b7
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
carlos-r-l-rodrigues Oct 4, 2024
b792075
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 7, 2024
0d39ceb
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 7, 2024
089a154
sligth proxy improvement
adrien2p Oct 7, 2024
f0aa129
fix when/then + delete payment sessions
adrien2p Oct 7, 2024
c529632
fix types
adrien2p Oct 7, 2024
ff2c6e9
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 7, 2024
e5181a4
improvements
adrien2p Oct 7, 2024
f9ba6d6
Merge branch 'chore/add-to-cart-workflow-improvements' of github.com:…
adrien2p Oct 7, 2024
5661414
improvements on deep copy week map
adrien2p Oct 7, 2024
268d5be
remove duplicated copy
adrien2p Oct 7, 2024
38b1943
update compensation resolution
adrien2p Oct 7, 2024
3a40e59
fix types
adrien2p Oct 7, 2024
07c3455
remove copy before saving
adrien2p Oct 7, 2024
e0215ca
remove extra copy
adrien2p Oct 7, 2024
a10221c
parallelize steps in add to cart
adrien2p Oct 7, 2024
5c81063
parallelize
adrien2p Oct 7, 2024
bcc22e2
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 7, 2024
cede478
fix tests
adrien2p Oct 9, 2024
b7dd067
cleanup is worker mode
adrien2p Oct 9, 2024
ae60a18
Merge branch 'develop' into chore/add-to-cart-workflow-improvements
adrien2p Oct 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ medusaIntegrationTestRunner({

await deleteLineItemsWorkflow(appContainer).run({
input: {
cart_id: cart.id,
ids: items.map((i) => i.id),
},
throwOnError: false,
Expand Down Expand Up @@ -1211,6 +1212,7 @@ medusaIntegrationTestRunner({

const { errors } = await workflow.run({
input: {
cart_id: cart.id,
ids: items.map((i) => i.id),
},
throwOnError: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ProductStatus,
PromotionRuleOperator,
PromotionType,
RuleOperator
RuleOperator,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
Expand Down Expand Up @@ -2131,6 +2131,7 @@ medusaIntegrationTestRunner({
)

expect(response.status).toEqual(200)

expect(response.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MedusaError } from "@medusajs/framework/utils"
import {
WorkflowData,
createWorkflow,
parallelize,
transform,
WorkflowData,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
import {
Expand Down Expand Up @@ -102,13 +103,14 @@ export const addShippingMethodToWorkflow = createWorkflow(
return cart.shipping_methods.map((sm) => sm.id)
})

removeShippingMethodFromCartStep({
shipping_method_ids: currentShippingMethods,
})

const shippingMethodsToAdd = addShippingMethodToCartStep({
shipping_methods: shippingMethodInput,
})
const [, shippingMethodsToAdd] = parallelize(
removeShippingMethodFromCartStep({
shipping_method_ids: currentShippingMethods,
}),
addShippingMethodToCartStep({
shipping_methods: shippingMethodInput,
})
)

updateTaxLinesWorkflow.runAsStep({
input: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const completeCartWorkflow = createWorkflow(
name: completeCartWorkflowId,
store: true,
idempotent: true,
// 3 days of retention time
retentionTime: THREE_DAYS,
},
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ export const createCartWorkflow = createWorkflow(
}

// If there is only one country in the region, we prepare a shipping address with that country's code.
if (!data.input.shipping_address && data.region.countries.length === 1) {
if (
!data.input.shipping_address &&
data.region.countries.length === 1
) {
data_.shipping_address = {
country_code: data.region.countries[0].iso_2,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
} from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
import {
WorkflowData,
createStep,
createWorkflow,
parallelize,
transform,
WorkflowData,
} from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep } from "../../common/steps/create-remote-links"
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
Expand Down Expand Up @@ -52,9 +53,10 @@ export const createPaymentCollectionForCartWorkflow = createWorkflow(
list: false,
})

validateCartStep({ cart })

validateExistingPaymentCollectionStep({ cart })
parallelize(
validateCartStep({ cart }),
validateExistingPaymentCollectionStep({ cart })
)

const paymentData = transform({ cart }, ({ cart }) => {
return {
Expand Down
19 changes: 9 additions & 10 deletions packages/core/core-flows/src/cart/workflows/update-cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
} from "@medusajs/framework/types"
import { MedusaError, PromotionActions } from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
createHook,
createWorkflow,
parallelize,
transform,
when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import {
Expand Down Expand Up @@ -138,14 +138,13 @@ export const updateCartWorkflow = createWorkflow(
list: false,
}).config({ name: "refetch–cart" })

parallelize(
refreshCartShippingMethodsStep({ cart }),
updateTaxLinesWorkflow.runAsStep({
input: {
cart_id: carts[0].id,
},
})
)
refreshCartShippingMethodsStep({ cart })

updateTaxLinesWorkflow.runAsStep({
input: {
cart_id: carts[0].id,
},
})

updateCartPromotionsWorkflow.runAsStep({
input: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import {
Logger,
PaymentSessionDTO,
} from "@medusajs/framework/types"
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
import {
ContainerRegistrationKeys,
Modules,
promiseAll,
} from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"

export interface DeletePaymentSessionStepInput {
ids: string[]
Expand All @@ -29,32 +33,42 @@ export const deletePaymentSessionsStep = createStep(
return new StepResponse([], null)
}

for (const id of ids) {
const select = [
"provider_id",
"currency_code",
"amount",
"data",
"context",
"payment_collection.id",
]
const select = [
"provider_id",
"currency_code",
"amount",
"data",
"context",
"payment_collection.id",
]

const sessions = await service.listPaymentSessions({ id: ids }, { select })
const sessionMap = new Map(sessions.map((s) => [s.id, s]))

const [session] = await service.listPaymentSessions({ id }, { select })
const promises: Promise<void>[] = []

for (const id of ids) {
const session = sessionMap.get(id)!

// As this requires an external method call, we will try to delete as many successful calls
// as possible and pass them over to the compensation step to be recreated if any of the
// payment sessions fails to delete.
try {
await service.deletePaymentSession(id)
const promise = service
.deletePaymentSession(id)
.then((res) => {
deleted.push(session)
})
.catch((e) => {
logger.error(
`Encountered an error when trying to delete payment session - ${id} - ${e}`
)
})

deleted.push(session)
} catch (e) {
logger.error(
`Encountered an error when trying to delete payment session - ${id} - ${e}`
)
}
promises.push(promise)
}

await promiseAll(promises)

return new StepResponse(
deleted.map((d) => d.id),
deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class DistributedTransaction extends EventEmitter {
private readonly context: TransactionContext = new TransactionContext()
private static keyValueStore: IDistributedTransactionStorage

/**
* Store data during the life cycle of the current transaction execution.
* Store non persistent data such as transformers results, temporary data, etc.
*
* @private
*/
#temporaryStorage = new Map<string, unknown>()

public static setStorage(storage: IDistributedTransactionStorage) {
this.keyValueStore = storage
}
Expand Down Expand Up @@ -298,6 +306,18 @@ class DistributedTransaction extends EventEmitter {

await DistributedTransaction.keyValueStore.clearStepTimeout(this, step)
}

public setTemporaryData(key: string, value: unknown) {
this.#temporaryStorage.set(key, value)
}

public getTemporaryData(key: string) {
return this.#temporaryStorage.get(key)
}

public hasTemporaryData(key: string) {
return this.#temporaryStorage.has(key)
}
}

DistributedTransaction.setStorage(
Expand Down
49 changes: 33 additions & 16 deletions packages/core/orchestration/src/workflow/local-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
import {
createMedusaContainer,
isDefined,
isString,
MedusaContext,
MedusaContextType,
MedusaError,
MedusaModuleType,
createMedusaContainer,
isDefined,
isString,
} from "@medusajs/utils"
import { asValue } from "awilix"
import {
Expand Down Expand Up @@ -107,9 +107,17 @@ export class LocalWorkflow {
return resolved
}

const wrappableMethods = Object.getOwnPropertyNames(resolved).filter(
(key) => key !== "constructor"
)

return new Proxy(resolved, {
get: function (target, prop) {
if (typeof target[prop] !== "function") {
const shouldWrap =
wrappableMethods.includes(prop as string) &&
typeof target[prop] === "function"

if (!shouldWrap) {
return target[prop]
}

Expand All @@ -131,6 +139,7 @@ export class LocalWorkflow {
},
})
}

return container
}

Expand Down Expand Up @@ -369,9 +378,11 @@ export class LocalWorkflow {

await orchestrator.resume(transaction)

cleanUpEventListeners()

return transaction
try {
return transaction
} finally {
cleanUpEventListeners()
}
}

async getRunningTransaction(uniqueTransactionId: string, context?: Context) {
Expand Down Expand Up @@ -406,9 +417,11 @@ export class LocalWorkflow {

await orchestrator.cancelTransaction(transaction)

cleanUpEventListeners()

return transaction
try {
return transaction
} finally {
cleanUpEventListeners()
}
}

async registerStepSuccess(
Expand All @@ -433,9 +446,11 @@ export class LocalWorkflow {
response
)

cleanUpEventListeners()

return transaction
try {
return transaction
} finally {
cleanUpEventListeners()
}
}

async registerStepFailure(
Expand All @@ -459,9 +474,11 @@ export class LocalWorkflow {
handler(this.container_, context)
)

cleanUpEventListeners()

return transaction
try {
return transaction
} finally {
cleanUpEventListeners()
}
}

setOptions(options: Partial<TransactionModelOptions>) {
Expand Down
Loading
Loading