Skip to content
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

bug/1627 run publish journey action sequentially for multi-step journeys to fix race condition for sf-journeys #1629

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion @types/lib/metadataTypes/Journey.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion @types/lib/metadataTypes/Journey.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 79 additions & 58 deletions lib/metadataTypes/Journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import File from '../util/file.js';
import ReplaceCbReference from '../util/replaceContentBlockReference.js';
import Retriever from '../Retriever.js';
import pLimit from 'p-limit';
import yoctoSpinner from 'yocto-spinner';

/**
* @typedef {import('../../types/mcdev.d.js').BuObject} BuObject
Expand Down Expand Up @@ -1557,7 +1558,6 @@ class Journey extends MetadataType {
* @returns {Promise.<string[]>} Returns list of updated keys/ids that were published. Success could only be seen with a delay in the UI because the publish-endpoint is async
*/
static async publish(keyArr) {
const resultsJourney = [];
const resultsTransactional = [];
// works only with objectId
const statusUrls = [];
Expand Down Expand Up @@ -1646,7 +1646,7 @@ class Journey extends MetadataType {
Util.logger.info(
` - published ${this.definition.type}: ${
journey[this.definition.nameField]
} (${journey[this.definition.keyField]} by creating the matching transactionalEmail`
} (${journey[this.definition.keyField]}) by creating the matching transactionalEmail`
);
statusUrls.push({ key, statusUrl: response.statusUrl });
}
Expand Down Expand Up @@ -1679,66 +1679,84 @@ class Journey extends MetadataType {
);
break;
}
case 'Multistep':
case 'Quicksend': {
resultsJourney.push(
(async () => {
try {
const response = await this.client.rest.post(
`/interaction/v1/interactions/publishAsync/${journey.id}?versionNumber=${version}`,
{}
); // payload is empty for this request
if (response.statusUrl && response.statusId) {
Util.logger.info(
` - queued for publishing ${this.definition.type}: ${key}`
);
statusUrls.push({ key, statusUrl: response.statusUrl });
} else {
throw new Error(response);
}
return journey.key;
} catch (ex) {
if (
ex.message === 'Cannot publish interaction in Published status.'
) {
Util.logger.info(
` - ${this.definition.type} ${key} is already published`
);
} else if (
ex.message === 'Cannot publish interaction in Stopped status.'
) {
Util.logger.warn(
` - ${this.definition.type} ${key} is already published but stopped. Please resume it manually.`
);
} else {
Util.logger.error(
`Failed to publish ${this.definition.type} ${key}: ${ex.message}`
);
}
case 'Multistep': {
// SF Event, Api Event Journeys
// ! for SF-triggered journeys this cannot be asynchronous or it will cause a race-condition (see #1627 for details); the requests are accepted but then processed sequentually anyways, eliminating potential speed gains.
// It is unknown if the same would happen for API-event journeys but given that it's the same endpoint, lets not risk it and run this sequentially
let statusUrl;
try {
const response = await this.client.rest.post(
`/interaction/v1/interactions/publishAsync/${journey.id}?versionNumber=${version}`,
{}
); // payload is empty for this request
if (response.statusUrl && response.statusId) {
Util.logger.info(
` - ${this.definition.type} queued for publishing : ${key}/${version}`
);
statusUrl = response.statusUrl;
} else {
throw new Error(response);
}
if (!Util.OPTIONS.skipStatusCheck && statusUrl) {
const spinner = yoctoSpinner({
text: `Publishing journey…`,
}).start();

await Util.sleep(1000);
executedKeyArr.push(
await this._checkPublishStatus(statusUrl, key, spinner)
);
} else {
// no guarantees if the journey was actually published
executedKeyArr.push(key);
}
} catch (ex) {
switch (ex.message) {
case 'Cannot publish interaction in Published status.': {
Util.logger.info(
` - ${this.definition.type} ${key}/${version} is already published.`
);

break;
}
})()
case 'Cannot publish interaction in Stopped status.': {
Util.logger.warn(
` - ${this.definition.type} ${key}/${version} is stopped. Please create a new version and publish that.`
);

break;
}
case 'Cannot publish interaction in Paused status.': {
Util.logger.warn(
` - ${this.definition.type} ${key}/${version} is already published but currently paused. Run 'mcdev resume' instead.`
);

break;
}
default: {
Util.logger.error(
`Failed to publish ${this.definition.type} ${key}: ${ex.message}`
);
}
}
}
break;
}
default: {
throw new Error(
`${this.definition.type} type ${journey.definitionType} not supported yet by publish method`
);
}
}
} // for loop
if (resultsJourney.length) {
Util.logger.info(`Found ${resultsJourney.length} journey results`);
executedKeyArr.push(...(await Promise.all(resultsJourney)).filter(Boolean));

if (!Util.OPTIONS.skipStatusCheck && statusUrls.length) {
Util.logger.info(
`Checking status of ${statusUrls.length} published item${statusUrls.length === 1 ? '' : 's'}`
);
executedKeyArr.length = 0;
await Util.sleep(5000);
for (const item of statusUrls) {
executedKeyArr.push(await this._checkPublishStatus(item.statusUrl, item.key));
}
}
}
// Transactional Send Journeys
if (resultsTransactional.length) {
Util.logger.info(`Found ${resultsTransactional.length} journey results`);
const spinner = yoctoSpinner({
text: `Publishing ${resultsTransactional.length} transactional journey${resultsTransactional.length === 1 ? '' : 's'}…`,
}).start();
const transactionalKeyArr = (await Promise.all(resultsTransactional)).filter(Boolean);
spinner.success('done.');
executedKeyArr.push(...transactionalKeyArr);

Util.logger.info('Retrieving relevant journeys');
Expand Down Expand Up @@ -1787,30 +1805,33 @@ class Journey extends MetadataType {
*
* @param {string} statusUrl URL to check the status of the publish request
* @param {string} key key or id for log messages
* @param {any} spinner key or id for log messages
* @param {number} [tries] number of tries used to check the status
* @returns {Promise.<string>} key of the item that was published successfully
*/
static async _checkPublishStatus(statusUrl, key, tries = 1) {
static async _checkPublishStatus(statusUrl, key, spinner, tries = 1) {
try {
const response = await this.client.rest.get(statusUrl);
switch (response.status) {
case 'PublishCompleted': {
spinner.success('done.');
Util.logger.info(` - ${this.definition.type} ${key}: publishing succeeded`);
this._showPublishStatusDetails(response);
return key;
}
case 'PublishInProcess': {
Util.logger.info(
Util.logger.debug(
` - ${this.definition.type} ${key}: publishing still in progress`
);
if (tries < 50) {
await (tries < 10 ? Util.sleep(2000) : Util.sleep(5000));
return await this._checkPublishStatus(statusUrl, key, tries + 1);
return await this._checkPublishStatus(statusUrl, key, spinner, tries + 1);
} else {
return;
}
}
case 'Error': {
spinner.success('failed.');
Util.logger.error(` - ${this.definition.type} ${key}: publishing failed.`);
this._showPublishStatusDetails(response);
return;
Expand Down Expand Up @@ -1907,7 +1928,7 @@ class Journey extends MetadataType {
return stoppedKeyArr;
}
/**
* pauses all journey versions
* pauses selected journey versions
*
* @param {string[]} keyArr customerkey of the metadata
* @returns {Promise.<string[]>} Returns list of keys that were paused
Expand Down
50 changes: 38 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"toposort": "2.0.2",
"update-notifier": "7.2.0",
"winston": "3.14.2",
"yargs": "17.7.2"
"yargs": "17.7.2",
"yocto-spinner": "0.1.0"
},
"devDependencies": {
"@eslint/js": "9.9.0",
Expand Down
Loading
Loading