Skip to content

Commit d75cc4e

Browse files
[O2B-1195] Don't transition runs & envs created less than 30s ago (#1465)
* [O2B-1195] Don't transition runs & envs created less than 30s ago * Remove console.log
1 parent ea82633 commit d75cc4e

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

lib/server/services/environment/transitionToErrorLostEnvironments.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@
1414
const { sequelize } = require('../../../database/index.js');
1515
const { utilities: { TransactionHelper } } = require('../../../database');
1616
const { EnvironmentHistoryItemRepository } = require('../../../database/repositories/index.js');
17+
const { timestampToMysql } = require('../../utilities/timestampToMysql.js');
1718

1819
/**
1920
* Consider all the environments that are currently active but not in the given list of ids to be lost, and transition them to error
2021
*
2122
* @param {string[]} activeEnvironmentIds the ids of active environments (those environments will not be transitioned to error)
22-
* @param {number} modificationTimeWindow the time we have (in MINUTES), after the creation of an environment, to purge them. If an environment
23-
* has been created more than this amount of minutes before NOW, it is NOT purged
23+
* @param {Period} modificationTimePeriod environments created outside this time period will not be updated
2424
* @return {Promise<number[]>} resolve once all inactive environments has been transitioned to error with their ids
2525
* @deprecated
2626
*/
27-
exports.transitionToErrorLostEnvironments = async (activeEnvironmentIds, modificationTimeWindow) => {
27+
exports.transitionToErrorLostEnvironments = async (activeEnvironmentIds, modificationTimePeriod) => {
2828
// The select query is adapted for the insert
2929
let selectQuery = `
3030
SELECT e.id
3131
FROM environments_history_items ehi
3232
INNER JOIN environments e ON ehi.environment_id = e.id
33-
GROUP BY e.id, e.updated_at
34-
HAVING e.updated_at > date_sub(now(), INTERVAL ${modificationTimeWindow} MINUTE)
33+
GROUP BY e.id, e.updated_at, e.created_at
34+
HAVING e.updated_at >= '${timestampToMysql(modificationTimePeriod.from)}'
35+
AND e.created_at < '${timestampToMysql(modificationTimePeriod.to)}'
3536
AND group_concat(ehi.status) NOT LIKE '%ERROR%'
3637
AND group_concat(ehi.status) NOT LIKE '%DESTROYED%'
3738
`;

lib/server/services/housekeeping/handleLostRunsAndEnvironments.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const { transitionToErrorLostEnvironments } = require('../environment/transition
33
const { setO2StopOfLostRuns } = require('../run/setO2StopOfLostRuns.js');
44
const { ServicesConfig } = require('../../../config/index.js');
55

6+
const MILLISECONDS_IN_ONE_DAY = 24 * 60 * 60 * 1000;
7+
68
/**
79
* Handle lost environments and runs
810
*
@@ -33,11 +35,16 @@ exports.handleLostRunsAndEnvironments = async () => {
3335
}
3436
}
3537

36-
// Maximum amount of minutes after run start/environment creation to purge them
37-
const modificationTime = 2880;
38+
// Environments and runs created outside this time window (timestamps in ms) will not be updated
39+
const modificationPeriod = {
40+
// Don't update runs and environments lost more than 48 hours ago
41+
from: Date.now() - MILLISECONDS_IN_ONE_DAY * 2,
42+
// Don't update runs and environments created less than 30 seconds ago
43+
to: Date.now() - 1000 * 30,
44+
};
3845

39-
const transitionedEnvironments = await transitionToErrorLostEnvironments(environmentIdsToKeep, modificationTime);
40-
const endedRuns = await setO2StopOfLostRuns(runNumbersToKeep, modificationTime);
46+
const transitionedEnvironments = await transitionToErrorLostEnvironments(environmentIdsToKeep, modificationPeriod);
47+
const endedRuns = await setO2StopOfLostRuns(runNumbersToKeep, modificationPeriod);
4148

4249
return { transitionedEnvironments, endedRuns };
4350
} else {

lib/server/services/run/setO2StopOfLostRuns.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,30 @@
1212
*/
1313

1414
const { sequelize } = require('../../../database/index.js');
15+
const { timestampToMysql } = require('../../utilities/timestampToMysql.js');
1516

1617
/**
1718
* Consider all the runs that are still running but not in the given list of run numbers to be lost, and set their stop time to now
1819
*
1920
* @param {number[]} runNumbersOfRunningRuns the list of run numbers corresponding to runs that are actually running
20-
* @param {number} modificationTimeWindow the time we have (in MINUTES), after the start of a run, to purge them. If a run started more than this
21-
* amount of minutes before NOW, it is NOT purged
21+
* @param {Period} modificationTimePeriod runs started outside this period will not be updated
2222
* @return {Promise<number[]>} resolve once the lost runs has been marked as ended with the list of ended runs numbers
2323
* @deprecated
2424
*/
25-
exports.setO2StopOfLostRuns = async (runNumbersOfRunningRuns, modificationTimeWindow) => {
25+
exports.setO2StopOfLostRuns = async (runNumbersOfRunningRuns, modificationTimePeriod) => {
2626
let fetchQuery = `
2727
SELECT run_number
2828
FROM runs
2929
WHERE time_o2_end IS NULL
3030
AND time_trg_end IS NULL
3131
AND COALESCE(time_trg_start, time_o2_start) IS NOT NULL
32-
AND COALESCE(time_trg_start, time_o2_start) > date_sub(now(), INTERVAL ${modificationTimeWindow} MINUTE)
32+
AND COALESCE(time_trg_start, time_o2_start) >= '${timestampToMysql(modificationTimePeriod.from)}'
33+
AND COALESCE(time_trg_start, time_o2_start) < '${timestampToMysql(modificationTimePeriod.to)}'
3334
`;
3435
if (runNumbersOfRunningRuns.length > 0) {
3536
fetchQuery += ` AND run_number NOT IN (${runNumbersOfRunningRuns.join(',')})`;
3637
}
38+
3739
const [runs] = await sequelize.query(fetchQuery, { raw: true });
3840
const runNumbers = runs.map(({ run_number }) => run_number);
3941

0 commit comments

Comments
 (0)