Skip to content

Commit 0746213

Browse files
authored
feat: QoL improvements to write alerts script (hyperlane-xyz#5998)
### Description - properly handle port forward process on exit - prompt user to confirm threshold changes have been reviewed ### Testing Manual
1 parent f021f86 commit 0746213

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

typescript/infra/scripts/funding/write-alert.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ interface RegressionError {
4848
async function main() {
4949
const { dryRun } = await withDryRun(yargs(process.argv.slice(2))).argv;
5050

51+
const confirmed = await confirm({
52+
message:
53+
'Before proceeding, please ensure that any threshold changes have been committed and reviewed in a PR, have all changes been reviewed?',
54+
});
55+
56+
if (!confirmed) {
57+
rootLogger.info('Exiting without updating any alerts');
58+
process.exit(0);
59+
}
60+
5161
// runs a validation check to ensure the threshold configs are valid relative to each other
5262
await validateBalanceThresholdConfigs();
5363

@@ -56,25 +66,29 @@ async function main() {
5666
PROMETHEUS_LOCAL_PORT,
5767
);
5868

59-
const alertsToUpdate = Object.values(AlertType);
60-
const alertUpdateInfo: AlertUpdateInfo[] = [];
61-
const missingChainErrors: RegressionError[] = [];
69+
process.on('SIGINT', () => {
70+
cleanUp(portForwardProcess);
71+
process.exit(1);
72+
});
73+
74+
process.on('beforeExit', () => {
75+
cleanUp(portForwardProcess);
76+
});
6277

6378
try {
79+
const alertsToUpdate = Object.values(AlertType);
80+
const alertUpdateInfo: AlertUpdateInfo[] = [];
81+
const missingChainErrors: RegressionError[] = [];
82+
6483
for (const alert of alertsToUpdate) {
6584
// fetch alertRule config from Grafana via the Grafana API
6685
const alertRule = await fetchGrafanaAlert(alert, saToken);
6786

6887
// read the proposed thresholds from the config file
6988
let proposedThresholds: ChainMap<number> = {};
70-
try {
71-
proposedThresholds = readJSONAtPath(
72-
`${THRESHOLD_CONFIG_PATH}/${alertConfigMapping[alert].configFileName}`,
73-
);
74-
} catch (e) {
75-
rootLogger.error(`Error reading ${alert} config: ${e}`);
76-
process.exit(1);
77-
}
89+
proposedThresholds = readJSONAtPath(
90+
`${THRESHOLD_CONFIG_PATH}/${alertConfigMapping[alert].configFileName}`,
91+
);
7892

7993
// parse the current thresholds from the existing query
8094
const existingQuery = alertRule.queries[0];
@@ -112,6 +126,7 @@ async function main() {
112126
rootLogger.info(
113127
`Exiting without updating any alerts, this is to avoid thresholds from being out of sync`,
114128
);
129+
cleanUp(portForwardProcess);
115130
process.exit(0);
116131
}
117132
} else {
@@ -128,6 +143,7 @@ async function main() {
128143
query,
129144
currentThresholds,
130145
proposedThresholds,
146+
portForwardProcess,
131147
);
132148

133149
alertUpdateInfo.push({
@@ -148,7 +164,7 @@ async function main() {
148164
rootLogger.info('Dry run, not updating alerts');
149165
}
150166
} finally {
151-
portForwardProcess.kill();
167+
cleanUp(portForwardProcess);
152168
}
153169
}
154170

@@ -220,7 +236,7 @@ async function updateAlerts(
220236
`Error updating ${alertInfo.alertType} alert, aborting updating the rest of the alerts: ${e}`,
221237
);
222238
// exiting here so we don't continue updating alerts with lower writePriority
223-
portForwardProcess.kill();
239+
cleanUp(portForwardProcess);
224240
process.exit(1);
225241
}
226242
}
@@ -294,6 +310,7 @@ async function confirmFiringAlerts(
294310
query: string,
295311
currentThresholds: ChainMap<number>,
296312
proposedThresholds: ChainMap<number>,
313+
portForwardProcess: ChildProcess,
297314
) {
298315
const alertingChains = await fetchFiringThresholdAlert(query);
299316
if (alertingChains.length === 0) return;
@@ -316,10 +333,18 @@ async function confirmFiringAlerts(
316333
rootLogger.info(
317334
`Exiting without updating any alerts, this is to avoid thresholds from being out of sync as we do not want to update the ${alert} alert`,
318335
);
336+
cleanUp(portForwardProcess);
319337
process.exit(0);
320338
}
321339
}
322340

341+
function cleanUp(portForwardProcess: ChildProcess) {
342+
if (!portForwardProcess.killed) {
343+
rootLogger.info('Cleaning up port forward process');
344+
portForwardProcess.kill();
345+
}
346+
}
347+
323348
main().catch((err) => {
324349
console.error(err);
325350
process.exit(1);

0 commit comments

Comments
 (0)