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

fix(picklist-background): add retry counter to picklist update #8

Merged
merged 3 commits into from
Feb 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DeploySourceResult } from '../../deployers/DeploymentExecutor';
import SFPOrg from '../../org/SFPOrg';
import { Schema } from 'jsforce';
import { DeploymentOptions } from '../../deployers/DeploySourceToOrgImpl';
import Bottleneck from "bottleneck";

const QUERY_BODY = 'SELECT Id FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName = ';

Expand Down Expand Up @@ -68,23 +69,22 @@ export default class PicklistEnabler implements DeploymentCustomizer {

SFPLogger.log(
`Fetching picklist for custom field ${picklistName} on object ${objName}`,
LoggerLevel.INFO,
LoggerLevel.TRACE,
logger
);

let picklistInOrg = await this.getPicklistInOrg(urlId, sfpOrg.getConnection());

//check for empty picklists on org and fix first deployment issue
if (!picklistInOrg?.Metadata?.valueSet?.valueSetDefinition) {
SFPLogger.log(
SFPLogger.log(
`Picklist field ${objName}.${picklistName} not in target Org. Skipping`,
LoggerLevel.TRACE,
logger
);
continue;
continue;
}


let picklistValueInOrg = [];

for (const value of picklistInOrg.Metadata.valueSet.valueSetDefinition.value) {
Expand All @@ -104,12 +104,31 @@ export default class PicklistEnabler implements DeploymentCustomizer {

let isPickListIdentical = this.arePicklistsIdentical(picklistValueInOrg, picklistValueSource);

const limiter = new Bottleneck({maxConcurrent: 1});

limiter.on("failed", async (error, jobInfo) => {

if (jobInfo.retryCount < 5 && error.message.includes('background')) {
return 30000;
} else if (jobInfo.retryCount >= 5 && error.message.includes('background')) {
throw new Error(`Retry limit exceeded (3 minutes). Unable to process Picklist update.`);
} else {
throw new Error(`Unable to update picklist for custom field ${objName}.${picklistName} due to ${error.message}`);
}
});

limiter.on("retry", (error, jobInfo) => SFPLogger.log(
`Background job is beeing executed. Retrying (${jobInfo.retryCount + 1}/5) after 30 seconds...`,
LoggerLevel.WARN,
logger
));

if (!isPickListIdentical) {
this.deployPicklist(picklistInOrg, picklistValueSource, sfpOrg.getConnection(), logger);
await limiter.schedule(() => this.deployPicklist(picklistInOrg, picklistValueSource, sfpOrg.getConnection(), logger));
} else {
SFPLogger.log(
`Picklist for custom field ${objName}.${picklistName} is identical to the source.No deployment`,
LoggerLevel.INFO,
LoggerLevel.TRACE,
logger
);
}
Expand All @@ -122,8 +141,7 @@ export default class PicklistEnabler implements DeploymentCustomizer {
};
}
} catch (error) {
SFPLogger.log(`Unable to process Picklist update due to ${error.message}`, LoggerLevel.WARN, logger);
SFPLogger.log(`Error Details : ${error.stack}`, LoggerLevel.TRACE);
throw new Error(`Unable to process Picklist update due to ${error.message}`);
}
}

Expand Down Expand Up @@ -160,15 +178,23 @@ export default class PicklistEnabler implements DeploymentCustomizer {
if (Array.isArray(values)) {
for (const value of values) {
//ignore inactive values from source
if(!value?.isActive || value?.isActive == 'true'){
picklistValueSet.push({fullName: value['fullName'] ? decodeURI(value['fullName']) : value['fullName'] , default: value.default, label: value['label'] ? decodeURI(value['label']) : value['label']});
if (!value?.isActive || value?.isActive == 'true') {
picklistValueSet.push({
fullName: value['fullName'] ? decodeURI(value['fullName']) : value['fullName'],
default: value.default,
label: value['label'] ? decodeURI(value['label']) : value['label'],
});
}
}
} else if (typeof values === 'object' && 'fullName' in values) {
//ignore inactive values from source
if(!values?.isActive || values?.isActive == 'true'){
picklistValueSet.push({fullName: values['fullName'] ? decodeURI(values['fullName']) : values['fullName'] , default: values.default, label: values['label'] ? decodeURI(values['label']) : values['label']});
}
if (!values?.isActive || values?.isActive == 'true') {
picklistValueSet.push({
fullName: values['fullName'] ? decodeURI(values['fullName']) : values['fullName'],
default: values.default,
label: values['label'] ? decodeURI(values['label']) : values['label'],
});
}
}
return picklistValueSet;
}
Expand Down Expand Up @@ -203,13 +229,8 @@ export default class PicklistEnabler implements DeploymentCustomizer {
FullName: picklistInOrg.FullName,
};
SFPLogger.log(`Update picklist for custom field ${picklistToDeploy.FullName}`, LoggerLevel.INFO, logger);
try {
await conn.tooling.sobject('CustomField').update(picklistToDeploy);
} catch (error) {
throw new Error(
`Unable to update picklist for custom field ${picklistToDeploy.FullName} due to ${error.message}`
);
}
await conn.tooling.sobject('CustomField').update(picklistToDeploy);

}

public getName(): string {
Expand Down
Loading