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

Updated endpoint used for build/deploy which supports polling and added polling #411

Merged
merged 4 commits into from
Jan 13, 2021
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
18 changes: 7 additions & 11 deletions packages/cli-lib/api/functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const http = require('../http');
const { fetchRawAssetByPath } = require('./designManager');

const FUNCTION_API_PATH = 'cms/v3/functions';

async function getFunctionByPath(accountId, functionPath) {
Expand All @@ -15,26 +13,24 @@ async function getRoutes(accountId) {
});
}

async function buildPackage(portalId, path) {
async function buildPackage(portalId, folderPath) {
return http.post(portalId, {
uri: `${FUNCTION_API_PATH}/package`,
uri: `${FUNCTION_API_PATH}/build`,
body: {
path,
folderPath,
},
});
}

async function deletePackage(portalId, path) {
return fetchRawAssetByPath(portalId, path).then(resp => {
return http.delete(portalId, {
uri: `${FUNCTION_API_PATH}/package?portalId=${portalId}&rawAssetId=${resp.id}`,
});
async function getBuildStatus(portalId, buildId) {
return http.get(portalId, {
uri: `${FUNCTION_API_PATH}/build/${buildId}/poll`,
});
}

module.exports = {
buildPackage,
deletePackage,
getBuildStatus,
getFunctionByPath,
getRoutes,
};
55 changes: 29 additions & 26 deletions packages/cli/commands/functions/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
const { logger } = require('@hubspot/cli-lib/logger');
const {
buildPackage,
deletePackage,
getBuildStatus,
} = require('@hubspot/cli-lib/api/functions');
const { validateAccount } = require('../../lib/validation');

Expand All @@ -30,6 +30,23 @@ const makeSpinner = (actionText, functionPath, accountIdentifier) => {
);
};

const pollBuildStatus = (accountId, buildId) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Is 1000 too fast?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set it to a faster poll because the build seems to happen pretty quickly in some cases. Although, this is a good point because the BE only has PROCESSING, ERROR, and SUCCESS states ATM, so we can dial this back. I'll go with 5000

return new Promise((resolve, reject) => {
const pollInterval = setInterval(async () => {
const pollResp = await getBuildStatus(accountId, buildId);
const { status } = pollResp;

if (status === 'SUCCESS') {
clearInterval(pollInterval);
resolve(pollResp);
} else if (status === 'ERROR') {
clearInterval(pollInterval);
reject(pollResp);
}
}, 5000);
});
};

const loadAndValidateOptions = async options => {
setLogLevel(options);
logDebugInfo(options);
Expand All @@ -44,14 +61,12 @@ const loadAndValidateOptions = async options => {

exports.command = 'deploy <path>';
exports.describe = false;
// Uncomment to unhide 'builds a new dependency bundle for the specified .functions folder';

exports.handler = async options => {
loadAndValidateOptions(options);

const { path: functionPath } = options;
const accountId = getAccountId(options);
const { delete: shouldDeletePackage } = options;
const splitFunctionPath = functionPath.split('.');
let spinner;

Expand All @@ -70,20 +85,18 @@ exports.handler = async options => {
);

try {
let successMessage;
if (shouldDeletePackage) {
spinner = makeSpinner('Deleting', functionPath, accountId);
spinner.start();
await deletePackage(accountId, `${functionPath}/package.json`);
successMessage = `Successfully removed build package for ${functionPath} on account ${accountId}.`;
} else {
spinner = makeSpinner('Building and deploying', functionPath, accountId);
spinner.start();
await buildPackage(accountId, `${functionPath}/package.json`);
successMessage = `Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId}.`;
}
spinner = makeSpinner(
'Building and deploying',
functionPath,
accountId
).start();
const buildId = await buildPackage(accountId, functionPath);
const successResp = await pollBuildStatus(accountId, buildId);
const buildTimeSeconds = (successResp.buildTime / 1000).toFixed(2);
spinner.stop();
logger.success(successMessage);
logger.success(
`Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId} in ${buildTimeSeconds}s.`
);
} catch (e) {
spinner && spinner.stop && spinner.stop();
if (e.statusCode === 404) {
Expand All @@ -106,21 +119,11 @@ exports.builder = yargs => {
type: 'string',
});

yargs.option('delete', {
alias: 'D',
describe: 'Remove currently built and deployed package',
type: 'boolean',
});

yargs.example([
[
'$0 functions deploy myFunctionFolder.functions',
'Build and deploy a new bundle for all functions within the myFunctionFolder.functions folder',
],
[
'$0 functions deploy myFunctionFolder.functions --delete',
'Delete the currently built and deployed bundle used by all functions within the myFunctionFolder.functions folder',
],
]);

addConfigOptions(yargs, true);
Expand Down