Skip to content

Commit

Permalink
WIP feat: adds a debounce for RA deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Nov 19, 2024
1 parent 796248f commit 3ce0c33
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 3 additions & 3 deletions build/controllers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async function _handleIssueComment(request, scalingoClient = ScalingoClient, git

const branchName = await githubService.getPullRequestBranchName({ repo, owner, pull_number });

await client.deployUsingSCM(reviewAppName, branchName);
await client.deployUsingSCM(reviewAppName, branchName, config.ecoMode.deployDebounce);

return 'ok';
}
Expand All @@ -214,12 +214,12 @@ async function deployPullRequest(
try {
const reviewAppExists = await client.reviewAppExists(reviewAppName);
if (reviewAppExists) {
await client.deployUsingSCM(reviewAppName, ref);
await client.deployUsingSCM(reviewAppName, ref, config.ecoMode.deployDebounce);
} else {
await reviewAppRepository.create({ name: reviewAppName, repository, prNumber: prId, parentApp: appName });
await client.deployReviewApp(appName, prId);
await client.disableAutoDeploy(reviewAppName);
await client.deployUsingSCM(reviewAppName, ref);
await client.deployUsingSCM(reviewAppName, ref, config.ecoMode.deployDebounce);
}
deployedRA.push({ name: appName, isCreated: !reviewAppExists });
} catch (error) {
Expand Down
23 changes: 22 additions & 1 deletion common/services/scalingo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { logger } from './logger.js';

const DEFAULT_OPTS = { withEnvSuffix: true };

const debouncedDeployments = new Map();

class ScalingoClient {
constructor(client, environment) {
this.client = client;
Expand Down Expand Up @@ -47,7 +49,26 @@ class ScalingoClient {
return `${scalingoApp} ${releaseTag} has been deployed`;
}

async deployUsingSCM(scalingoApp, releaseTag) {
async deployUsingSCM(scalingoApp, releaseTag, debounce) {
if (!debounce) return this.#requestDeploymentUsingSCM(scalingoApp, releaseTag);

if (debouncedDeployments.has(scalingoApp)) {
clearTimeout(debouncedDeployments.get(scalingoApp));
}

debouncedDeployments.set(
scalingoApp,
setTimeout(() => {
this.#requestDeploymentUsingSCM(scalingoApp, releaseTag).catch(() => {});
debouncedDeployments.delete(scalingoApp);
}, debounce),
);

logger.info({ message: `Deployment of ${scalingoApp} ${releaseTag} will be requested after ${debounce}ms` });
return `Deployment of ${scalingoApp} ${releaseTag} will be requested after ${debounce}ms`;
}

async #requestDeploymentUsingSCM(scalingoApp, releaseTag) {
try {
await this.client.SCMRepoLinks.manualDeploy(scalingoApp, releaseTag);
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const configuration = (function () {
ecoMode: {
stopSchedule: process.env.REVIEW_APP_STOP_SCHEDULE,
startSchedule: process.env.REVIEW_APP_START_SCHEDULE,
deployDebounce: _getNumber(process.env.REVIEW_APP_DEPLOY_DEBOUNCE, 30) * 1000,
},

baleen: {
Expand Down
7 changes: 7 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ SCALINGO_TOKEN_REVIEW_APPS=__CHANGE_ME__
# default: "0 0 8 * * 1-5"
#REVIEW_APP_START_SCHEDULE=0 0 8 * * 1-5

# Debounce time for deploying review apps.
#
# presence: optionnal
# type: number
# default: 30
#REVIEW_APP_DEPLOY_DEBOUNCE=30

# List of review apps that must not be managed.
#
# If not present, all the review apps will be stopped and restrated.
Expand Down

0 comments on commit 3ce0c33

Please sign in to comment.