-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds a debounce for RA deployments
- Loading branch information
Showing
11 changed files
with
541 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { knex } from '../../db/knex-database-connection.js'; | ||
|
||
const TABLE_NAME = 'review-apps-deployments'; | ||
|
||
/** | ||
* @typedef {{ | ||
* appName: string | ||
* scmRef: string | ||
* after: Date | ||
* }} Deployment | ||
* | ||
* @typedef {import('knex').Knex} Knex | ||
*/ | ||
|
||
/** | ||
* @param {Deployment} deployment | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function save({ appName, scmRef, after }) { | ||
await knex.insert({ appName, scmRef, after }).into(TABLE_NAME).onConflict('appName').merge(); | ||
} | ||
|
||
/** | ||
* @param {string} appName | ||
* @param {Knex} knexConn | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function remove(appName, knexConn = knex) { | ||
await knexConn.delete().from(TABLE_NAME).where('appName', appName); | ||
} | ||
|
||
/** | ||
* @param {Knex} knexConn | ||
* @returns {Promise<Deployment[]>} | ||
*/ | ||
export async function listForDeployment(knexConn = knex) { | ||
const deployments = await knexConn | ||
.select() | ||
.from(TABLE_NAME) | ||
.where('after', '<', knexConn.raw('NOW()')) | ||
.orderBy('after') | ||
.forUpdate(); | ||
return deployments; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
import * as reviewAppDeploymentRepository from '../repositories/review-app-deployment-repository.js'; | ||
import ScalingoClient from '../../common/services/scalingo-client.js'; | ||
import { logger } from '../../common/services/logger.js'; | ||
import { config } from '../../config.js'; | ||
import { knex } from '../../db/knex-database-connection.js'; | ||
|
||
let started = false; | ||
let stopped; | ||
|
||
export async function start() { | ||
let scalingoClient; | ||
|
||
try { | ||
scalingoClient = await ScalingoClient.getInstance('reviewApps'); | ||
} catch (error) { | ||
throw new Error(`Scalingo auth APIError: ${error.message}`); | ||
} | ||
|
||
const delay = config.scalingo.reviewApps.deployDebounce / 5; | ||
|
||
started = true; | ||
stopped = Promise.withResolvers(); | ||
|
||
(async () => { | ||
while (started) { | ||
const [result] = await Promise.allSettled([ | ||
deploy({ reviewAppDeploymentRepository, scalingoClient }), | ||
Promise.race(setTimeout(delay), stopped.promise), | ||
]); | ||
|
||
if (result.status === 'rejected') { | ||
logger.error({ | ||
message: 'an error occured while deploying review apps', | ||
data: result.reason, | ||
}); | ||
} | ||
} | ||
})(); | ||
} | ||
|
||
export function stop() { | ||
started = false; | ||
stopped.resolve(); | ||
} | ||
|
||
export async function deploy({ reviewAppDeploymentRepository, scalingoClient }) { | ||
await knex.transaction(async (transaction) => { | ||
const deployments = await reviewAppDeploymentRepository.listForDeployment(transaction); | ||
|
||
for (const deployment of deployments) { | ||
if (!started) break; | ||
|
||
try { | ||
const reviewAppStillExists = await scalingoClient.reviewAppExists(deployment.appName); | ||
|
||
if (reviewAppStillExists) { | ||
await scalingoClient.deployUsingSCM(deployment.appName, deployment.scmRef); | ||
} | ||
|
||
await reviewAppDeploymentRepository.remove(deployment.appName, transaction); | ||
} catch (err) { | ||
logger.error({ | ||
event: 'scalingo', | ||
message: 'error while trying to deploy review app', | ||
data: err, | ||
}); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
db/migrations/20250114140337_create-table-review-apps-deployments.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const TABLE_NAME = 'review-apps-deployments'; | ||
|
||
/** | ||
* @param {import("knex").Knex} knex | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function up(knex) { | ||
await knex.schema.createTable(TABLE_NAME, (table) => { | ||
table.string('appName', 255).notNullable().primary(); | ||
table.string('scmRef', 255).notNullable(); | ||
table.datetime('after').notNullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {import("knex").Knex} knex | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function down(knex) { | ||
await knex.schema.dropTable(TABLE_NAME); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.