Skip to content

Commit

Permalink
#1879: Allow stopping all versions of a multi-step journey
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Dec 13, 2024
1 parent cebc720 commit 43927ef
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
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.

89 changes: 71 additions & 18 deletions lib/metadataTypes/Journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,8 @@ class Journey extends MetadataType {
const apiLimit = pLimit(20);
const journeyCache = await this.retrieveForCache();

const stoppableJourneyStatus = ['Paused', 'Published', 'Finishing'];

await Promise.allSettled(
keyArr.map((key) =>
apiLimit(async () => {
Expand All @@ -2353,29 +2355,80 @@ class Journey extends MetadataType {
break;
}
case 'Multistep': {
version ||= journeyCache.metadata[key].version;
const toBeStoppedVersions = [];
if (version === '*') {
Util.logger.warn(
` ☇ skipping ${this.definition.type} ${key} / ${journeyCache.metadata[key].name}: Cannot stop all versions at once. The highest version is ${journeyCache.metadata[key].version}`
);
return;
}
try {
await this.client.rest.post(
endpoint +
const responseAllVersions = await this.client.rest.getBulk(
'/interaction/v1/interactions/?id=' +
journeyCache.metadata[key].id +
`?versionNumber=${version}`,
{}
);
Util.logger.info(
` - Stopped ${this.definition.type} ${key}/${version} / ${journeyCache.metadata[key].name}`
'&mostRecentVersionOnly=false',
this.definition.restPageSize || 500
);
if (responseAllVersions?.items?.length) {
// find all active versions
const allActiveVersions = responseAllVersions.items
.filter((item) =>
stoppableJourneyStatus.includes(item.status)
)
.map((item) => item.version);
if (allActiveVersions.length) {
toBeStoppedVersions.push(...allActiveVersions);
}
}
if (!toBeStoppedVersions.length) {
Util.logger.warn(
` ☇ skipping stop of ${this.definition.type} ${key}: no active versions found`
);
}
} else {
if (!version) {
version = journeyCache.metadata[key].version;
Util.logger.info(
Util.getGrayMsg(
` - ${this.definition.type} ${key}: no version provided. Will try to stop latest version: Version ${version}. To stop all versions, append /* after the key.`
)
);
if (
!stoppableJourneyStatus.includes(
journeyCache.metadata[key].status
)
) {
Util.logger.warn(
` ☇ skipping stop of ${this.definition.type} ${key}: version ${version} has status "${journeyCache.metadata[key].status}" which cannot be stopped. To stop all versions, append /* after the key.`
);
}
}
toBeStoppedVersions.push(version);
}
const rateLimitActivities = pLimit(2);
const stoppedVersions = (
await Promise.all(
toBeStoppedVersions.map((version) =>
rateLimitActivities(async () => {
try {
await this.client.rest.post(
endpoint +
journeyCache.metadata[key].id +
`?versionNumber=${version}`,
{}
);
Util.logger.info(
` - Stopped ${this.definition.type} ${key} / ${journeyCache.metadata[key].name} - Version ${version}`
);
return version;
} catch (ex) {
Util.logger.error(
` - Stopping ${this.definition.type} ${key} / ${journeyCache.metadata[key].name} - Version ${version} failed: ${ex.message}`
);
return;
}
})
)
)
).filter(Boolean);
if (stoppedVersions.length === toBeStoppedVersions.length) {
stoppedKeyArr.push(key);
} catch (ex) {
Util.logger.error(
` - Stopping ${this.definition.type} ${key} / ${journeyCache.metadata[key].name} failed: ${ex.message}`
);
}

break;
}
default: {
Expand Down

0 comments on commit 43927ef

Please sign in to comment.