Skip to content

Commit e41020b

Browse files
authored
fix: fix instance auto deletion (#35)
1 parent 857e341 commit e41020b

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

docs/en/farm-config-json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Git repository format: `https://<token>@github.com/<repositoryPath>.git`.
2727
### `instanceDeleteTimeout`
2828
- *Number*
2929
- *Optional*
30-
> Ms time after which only **generated** instances will be removed. Default is unset (instances will **not** be removed)
30+
> Ms after creation after which only **generated** instances are removed (same lifecycle status as when stopped: rows stay `generated` in DB). Default is **30 days** when omitted. Instances the provider still reports as **running** are not queued for deletion. Set to `0` or a negative value to turn off automatic deletion.
3131
3232
### `urlTemplate`
3333
- *String*

docs/ru/farm-config-json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
### `instanceDeleteTimeout`
2828
- *Number*
2929
- *Optional*
30-
> Время в мс, по истечении которого только **сгенерированные** инстансы будут удалены. По умолчанию не установлено (инстансы **не будут** удалены)
30+
> Время в мс с момента **создания**, по истечении которого **сгенерированные** инстансы ставятся в очередь на удаление (в БД статус остаётся `generated`, как и у остановленных). Если не задано — по умолчанию **30 суток**. Инстансы, которые провайдер по-прежнему считает **running**, в очередь на удаление не попадают. Значение `0` или отрицательное число отключает автоудаление.
3131
3232
### `urlTemplate`
3333
- *String*

src/server/utils/butler.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Locker = {
1414
};
1515

1616
const globalInstanceStopTimeout = getGlobalFarmConfig().instanceStopTimeout ?? ms('1h');
17-
const globalInstanceDeleteTimeout = getGlobalFarmConfig().instanceDeleteTimeout;
17+
const globalInstanceDeleteTimeout = getGlobalFarmConfig().instanceDeleteTimeout ?? ms('30d');
1818

1919
export const isTimeout = (now: number, time: number, timeout: number) => {
2020
const diff = now - time;
@@ -67,17 +67,22 @@ const stopInstances = async () => {
6767
};
6868

6969
const deleteInstancesByTTL = async () => {
70-
if (!globalInstanceDeleteTimeout || Locker.deleteInstances) {
70+
if (globalInstanceDeleteTimeout <= 0 || Locker.deleteInstances) {
7171
return;
7272
}
7373

7474
try {
7575
Locker.deleteInstances = true;
7676

7777
const instancesToDelete = await db.getInstancesByTTL(globalInstanceDeleteTimeout);
78+
const providerInstances = await getFarmProvider().getInstances();
79+
const runningHashes = new Set(
80+
providerInstances.filter((p) => p.status === 'running').map((p) => p.hash),
81+
);
82+
const instancesSafeToDelete = instancesToDelete.filter((i) => !runningHashes.has(i.hash));
7883

7984
await Promise.all(
80-
instancesToDelete.map((instance) =>
85+
instancesSafeToDelete.map((instance) =>
8186
instanceUtils
8287
.addInstanceToDeleteQueue(instance)
8388
.catch(() => console.warn(`error while deleting instance "${instance.hash}"`)),

src/shared/common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export interface FarmConfigBase {
6767
/**
6868
* Instance life before deletion in ms.
6969
*
70-
* Deletes only `generated` instances, that were created more than `instanceDeleteTimeout` ms ago.
70+
* Deletes only `generated` instances whose `createdAt` is older than this value.
71+
* Instances still reported as `running` by the provider are skipped until they stop.
72+
* When omitted, defaults to 30 days. Set to `0` or a negative value to disable automatic deletion.
7173
*/
7274
instanceDeleteTimeout?: number;
7375
autoStartDelay?: number;

0 commit comments

Comments
 (0)