Skip to content

Commit

Permalink
fix: global configuration concurrency issue while saving
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Mar 5, 2025
1 parent 634098e commit 7390e33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/support/slack/commands/onboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ function OnboardCommand(context) {
*
* @param {string} baseURLInput - The site URL.
* @param {string} imsOrgID - The IMS Org ID.
* @param {object} configuration - The configuration object.
* @param {string} profileName - The profile name.
* @param {Object} slackContext - Slack context.
* @returns {Promise<Object>} - A report line containing execution details.
*/
const onboardSingleSite = async (baseURLInput, imsOrgID, profileName, slackContext) => {
const onboardSingleSite = async (
baseURLInput,
imsOrgID,
configuration,
profileName,
slackContext,
) => {
const { say } = slackContext;
const { DEFAULT_ORGANIZATION_ID: defaultOrgId } = context.env;

Expand Down Expand Up @@ -176,8 +183,6 @@ function OnboardCommand(context) {
return reportLine;
}

const configuration = await Configuration.findLatest();

const importTypes = Object.keys(profile.imports);
const siteConfig = site.getConfig();
for (const importType of importTypes) {
Expand Down Expand Up @@ -210,8 +215,6 @@ function OnboardCommand(context) {
configuration.enableHandlerForSite(auditType, site);
});

await configuration.save();

reportLine.audits = auditTypes.join(',');
log.info(`Enabled the following audits for site ${site.getId()}: ${reportLine.audits}`);
} catch (error) {
Expand Down Expand Up @@ -270,6 +273,7 @@ function OnboardCommand(context) {

const tempFilePath = path.join(os.tmpdir(), `spacecat_onboard_report_${Date.now()}.csv`);
const fileStream = fs.createWriteStream(tempFilePath);
const configuration = await Configuration.findLatest();

// Write headers to CSV report
fileStream.write(csvStringifier.getHeaderString());
Expand All @@ -278,10 +282,18 @@ function OnboardCommand(context) {
for (const row of csvData) {
/* eslint-disable no-await-in-loop */
const [baseURL, imsOrgID] = row;
const reportLine = await onboardSingleSite(baseURL, imsOrgID, profileName, slackContext);
const reportLine = await onboardSingleSite(
baseURL,
imsOrgID,
configuration,
profileName,
slackContext,
);
fileStream.write(csvStringifier.stringifyRecords([reportLine]));
}

await configuration.save();

log.info('All sites were processed and onboarded.');

fileStream.end();
Expand Down Expand Up @@ -310,13 +322,18 @@ function OnboardCommand(context) {
}

const [baseURLInput, imsOrgID, profileName = 'default'] = args;
const configuration = await Configuration.findLatest();

const reportLine = await onboardSingleSite(
baseURLInput,
imsOrgID,
configuration,
profileName,
slackContext,
);

await configuration.save();

if (reportLine.errors) {
await say(`:warning: ${reportLine.errors}`);
}
Expand Down
1 change: 1 addition & 0 deletions test/support/slack/commands/onboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('OnboardCommand', () => {
beforeEach(async () => {
const configuration = {
enableHandlerForSite: sinon.stub(),
save: sinon.stub().resolves(),
};
baseURL = 'https://example.com';

Expand Down

0 comments on commit 7390e33

Please sign in to comment.