Skip to content

Commit

Permalink
Refactor Promote Batching
Browse files Browse the repository at this point in the history
  • Loading branch information
arshadparwaiz committed Aug 12, 2024
1 parent e768a6d commit 7c5a606
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 455 deletions.
84 changes: 44 additions & 40 deletions actions/graybox/copy-sched.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ async function main(params) {
const projectStatusJson = await filesWrapper.readFileIntoObject(`graybox_promote${project}/status.json`);
logger.info(`In Copy-sched Projects Json: ${JSON.stringify(projectStatusJson)}`);

// Read the Batch Status in the current project's "batch_status.json" file
const batchStatusJson = await filesWrapper.readFileIntoObject(`graybox_promote${project}/batch_status.json`);

const copyBatchesJson = await filesWrapper.readFileIntoObject(`graybox_promote${project}/copy_batches.json`);
// copy all params from json into the params object
const inputParams = projectStatusJson?.params;
Expand All @@ -54,61 +57,62 @@ async function main(params) {
});

Object.entries(copyBatchesJson).forEach(async ([batchName, copyFilePathsJson]) => {
// Set the Project & Batch Name in params for the Copy Content Worker Action to read and process
params.project = project;
params.batchName = batchName;
if (batchStatusJson[batchName] === 'processed') {
// Set the Project & Batch Name in params for the Copy Content Worker Action to read and process
params.project = project;
params.batchName = batchName;

try {
const appConfig = new AppConfig(params);
const grpIds = appConfig.getConfig().grayboxUserGroups;
const vActData = await validateAction(params, grpIds, params.ignoreUserCheck);
if (vActData && vActData.code !== 200) {
logger.info(`Validation failed: ${JSON.stringify(vActData)}`);
return vActData;
}
try {
const appConfig = new AppConfig(params);
const grpIds = appConfig.getConfig().grayboxUserGroups;
const vActData = await validateAction(params, grpIds, params.ignoreUserCheck);
if (vActData && vActData.code !== 200) {
logger.info(`Validation failed: ${JSON.stringify(vActData)}`);
return vActData;
}

return ow.actions.invoke({
name: 'graybox/copy-content-worker',
blocking: false,
result: false,
params
}).then(async (result) => {
logger.info(result);
return {
code: 200,
payload: responsePayload
};
}).catch(async (err) => {
responsePayload = 'Failed to invoke graybox copy action';
return ow.actions.invoke({
name: 'graybox/copy-worker',
blocking: false,
result: false,
params
}).then(async (result) => {
logger.info(result);
return {
code: 200,
payload: responsePayload
};
}).catch(async (err) => {
responsePayload = 'Failed to invoke graybox copy action';
logger.error(`${responsePayload}: ${err}`);
return {
code: 500,
payload: responsePayload
};
});
} catch (err) {
responsePayload = 'Unknown error occurred while invoking Copy Content Worker Action';
logger.error(`${responsePayload}: ${err}`);
return {
code: 500,
payload: responsePayload
};
});
} catch (err) {
responsePayload = 'Unknown error occurred';
logger.error(`${responsePayload}: ${err}`);
responsePayload = err;
responsePayload = err;
}
}

responsePayload = 'Triggered multiple Copy Content Worker Actions';
return {
code: 500,
code: 200,
payload: responsePayload,
};
});
});

logger.info(`Params length after: ${Object.keys(params).length}`);
} catch (err) {
responsePayload = 'Unknown error occurred';
responsePayload = 'Unknown error occurred while processing the projects for Copy';
logger.error(`${responsePayload}: ${err}`);
responsePayload = err;
}

// No errors while initiating all the Copy Content Worker Action for all the projects
return {
code: 500,
payload: responsePayload,
code: 200,
payload: responsePayload
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,48 @@
* from Adobe.
************************************************************************* */

const {
getAioLogger, handleExtension, toUTCStr
} = require('../utils');
const { getAioLogger } = require('../utils');
const AppConfig = require('../appConfig');
const Sharepoint = require('../sharepoint');
const initFilesWrapper = require('./filesWrapper');

const logger = getAioLogger();

async function main(params) {
logger.info('Graybox Promote Content Action triggered');
logger.info('Graybox Copy Content Action triggered');

const appConfig = new AppConfig(params);
const {
spToken, adminPageUri, rootFolder, gbRootFolder, promoteIgnorePaths, experienceName, projectExcelPath, draftsOnly
} = appConfig.getPayload();
const { gbRootFolder, experienceName } = appConfig.getPayload();

const sharepoint = new Sharepoint(appConfig);

// process data in batches
const filesWrapper = await initFilesWrapper(logger);
let responsePayload;
const promotes = [];
let promotes = [];
const failedPromotes = [];

logger.info('In Copy Content Worker, Processing Copy Content');
logger.info('In Copy Worker, Processing Copy Content');

// Read the Batch Status in the current project's "batch_status.json" file
const batchStatusJson = await filesWrapper.readFileIntoObject(`graybox_promote${gbRootFolder}/${experienceName}/batch_status.json`);

const promotedPathsJson = await filesWrapper.readFileIntoObject(`graybox_promote${gbRootFolder}/${experienceName}/promoted_paths.json`) || {};

const project = params.project || '';
const batchName = params.batchName || '';

// Combined existing If any promotes already exist in promoted_paths.json for the current batch either from Copy action or Promote Action
if (promotedPathsJson[batchName]) {
promotes = promotes.concat(promotedPathsJson[batchName]);
}

const copyBatchesJson = await filesWrapper.readFileIntoObject(`graybox_promote${project}/copy_batches.json`);

const copyFilePathsJson = copyBatchesJson[batchName] || {};

logger.info(`In Copy Content Worker, copyFilePaths: ${JSON.stringify(copyFilePathsJson)}`);
// Process the Copy Content
Object.entries(copyFilePathsJson).forEach(async ([copySourceFilePath, copyDestFilePath]) => {
const copyPromises = Object.entries(copyFilePathsJson).map(async ([copySourceFilePath, copyDestFilePath]) => {
// Download the grayboxed file and save it to default content location
const { fileDownloadUrl } = await sharepoint.getFileData(copySourceFilePath, true);
const file = await sharepoint.getFileUsingDownloadUrl(fileDownloadUrl);
Expand All @@ -66,7 +71,28 @@ async function main(params) {
}
});

responsePayload = 'Copy Content Worker finished promoting content';
// Wait for all copy operations to complete
await Promise.all(copyPromises);

logger.info(`In Copy Worker, Promotes: ${JSON.stringify(promotes)}`);
// Update the Promoted Paths in the current project's "promoted_paths.json" file
if (promotes.length > 0) {
promotedPathsJson[batchName] = promotes;
await filesWrapper.writeFile(`graybox_promote${gbRootFolder}/${experienceName}/promoted_paths.json`, promotedPathsJson);
}
// Update the Batch Status in the current project's "batch_status.json" file
if (batchStatusJson && batchStatusJson[batchName] && (promotes.length > 0 || failedPromotes.length > 0)) {
batchStatusJson[batchName] = 'promoted';
}

// Write the updated batch_status.json file
await filesWrapper.writeFile(`graybox_promote${gbRootFolder}/${experienceName}/batch_status.json`, batchStatusJson);

const updatedPromotedPathsJson = await filesWrapper.readFileIntoObject(`graybox_promote${gbRootFolder}/${experienceName}/promoted_paths.json`);

updateProjectStatus(gbRootFolder, experienceName, filesWrapper);

responsePayload = 'Copy Worker finished promoting content';
logger.info(responsePayload);
return exitAction({
body: responsePayload,
Expand All @@ -82,18 +108,16 @@ async function main(params) {
* @returns updated project status
*/
async function updateProjectStatus(gbRootFolder, experienceName, filesWrapper) {
const projects = await filesWrapper.readFileIntoObject('graybox_promote/ongoing_projects.json');
const ongoingProjects = await filesWrapper.readFileIntoObject('graybox_promote/ongoing_projects.json');
const projectStatusJson = await filesWrapper.readFileIntoObject(`graybox_promote${gbRootFolder}/${experienceName}/status.json`);

// Update the Project Status in the current project's "status.json" file
projectStatusJson.status = 'initial_preview_done';
logger.info(`In Promote-content-worker After Processing Promote, Project Status Json: ${JSON.stringify(projectStatusJson)}`);
projectStatusJson.status = 'promoted';
await filesWrapper.writeFile(`graybox_promote${gbRootFolder}/${experienceName}/status.json`, projectStatusJson);

// Update the Project Status in the parent "ongoing_projects.json" file
projects.find((p) => p.project_path === `${gbRootFolder}/${experienceName}`).status = 'initial_preview_done';
logger.info(`In Promote-content-worker After Processing Promote, OnProjects Json: ${JSON.stringify(projects)}`);
await filesWrapper.writeFile('graybox_promote/ongoing_projects.json', projects);
ongoingProjects.find((p) => p.project_path === `${gbRootFolder}/${experienceName}`).status = 'promoted';
await filesWrapper.writeFile('graybox_promote/ongoing_projects.json', ongoingProjects);
}

function exitAction(resp) {
Expand Down
Loading

0 comments on commit 7c5a606

Please sign in to comment.