Files
TREK/server/src/nest/storage/storage-reclaim.controller.ts
T
Maurice 1c6a175ebc fix(storage): queue what a delete left unnamed past every undo, and tell a copied database apart
A delete or a replace no longer deletes inside the request. The owner's call
queues the keys in storage_release_queue, due after RELEASE_DELAY_MS (26 h:
the assistant's 24 h approval window plus a margin), and StorageReleaseJob
takes the due rows every ten minutes, at most 200 a tick and 50 keys a
question, asks every reference source again and deletes what none names.
It refuses what the nightly pass refuses (StorageReclaimGuardService: the
ledger, the required sources, a shared location, another installation's
owner marker, cached per category), and waits while a restore or a backup
runs or another storage job holds the slot. An assistant undo or a late
write that names the old value again keeps its object.

The install id lives in data/.trek-install-id, outside any backup and never
written by a restore. At boot and after every reopen the database is
reconciled with it: a database naming another installation (a boot restore,
a restart after a failed restore, a copied travel.db) loses its switch,
confirmations, reports, marks and queue before anything reads them.

A confirmation now stores the count the admin was shown and when; it covers
up to one and a half times that count (at least 100 more) for 14 days, is
used up once the share is back under a fifth, and a count the last report
no longer holds is refused as reclaim_confirm_stale. The admin state folds
a live confirmation in, so the prompt goes at once.

A pass reads each source once for every category and drops each category's
keys once judged; right before a batch it reads the category's sources
again, whole and in memory, instead of a LIKE per key. A narrowed read holds
at most 50 LIKE terms. A pass held off by a backup reports backup_running,
and a category the ledger never reclaims reports not_reclaimed even after a
stop.
2026-11-01 18:11:43 +01:00

93 lines
3.5 KiB
TypeScript

import type { User } from '../../types';
import { AuditService } from '../audit/audit.service';
import { getClientIp } from '../audit/client-ip';
import { AdminGuard } from '../auth-core/admin.guard';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { ManagedForbidden } from '../common/managed';
import { ResponseContract } from '../common/response-contract';
import { StorageReclaimConfirmDto, StorageReclaimSettingsDto } from './storage-admin.dto';
import { StorageReclaimService } from './storage-reclaim.service';
import { Body, Controller, HttpCode, Post, Put, Req, UseGuards } from '@nestjs/common';
import { storageReclaimRunStartedSchema, storageReclaimStateSchema, type StorageReclaimState } from '@trek/shared';
import type { Request } from 'express';
/**
* /api/admin/storage/reclaim: the admin's switch for the reclaim pass, a check
* on demand and the confirmation a category with a large unreferenced share
* waits for. The state itself rides on GET /api/admin/storage. Deleting stays
* off until an admin turns it on here; a pass then still deletes only what has
* been unreferenced for a week (StorageReclaimService).
*/
@Controller('api/admin/storage/reclaim')
@UseGuards(JwtAuthGuard, AdminGuard)
@ManagedForbidden('storage backends and their contents are hoster-level configuration')
export class StorageReclaimController {
constructor(
private readonly reclaim: StorageReclaimService,
private readonly audit: AuditService,
) {}
@Put()
@ResponseContract(storageReclaimStateSchema)
async update(
@CurrentUser() user: User,
@Body() body: StorageReclaimSettingsDto,
@Req() req: Request,
): Promise<StorageReclaimState> {
const state = await this.reclaim.setDeleteEnabled(body.deleteEnabled);
await this.audit.writeAudit({
userId: user.id,
action: 'admin.storage_reclaim_update',
ip: getClientIp(req),
details: { deleteEnabled: body.deleteEnabled },
});
return state;
}
/**
* Starts a report-only pass in the background and answers 202 at once; the
* admin state says `running` until its report lands. 409 on a demo or while
* another storage job, a reclaim pass included, runs (a release tick, which
* takes seconds, is waited for).
*/
@Post('run')
@HttpCode(202)
@ResponseContract(storageReclaimRunStartedSchema)
async run(@CurrentUser() user: User, @Req() req: Request): Promise<{ started: true }> {
await this.reclaim.start();
await this.audit.writeAudit({
userId: user.id,
action: 'admin.storage_reclaim_run',
ip: getClientIp(req),
details: { mode: 'report' },
});
return { started: true };
}
/**
* Lets the pass delete in a category whose unreferenced share is over a
* fifth, for the count the admin was shown (`unreferenced`). 409
* `reclaim_nothing_to_confirm` when nothing waits, `reclaim_confirm_stale`
* when the last report's count is no longer the one shown.
*/
@Post('confirm')
@HttpCode(200)
@ResponseContract(storageReclaimStateSchema)
async confirm(
@CurrentUser() user: User,
@Body() body: StorageReclaimConfirmDto,
@Req() req: Request,
): Promise<StorageReclaimState> {
const state = await this.reclaim.confirm(body.category, body.unreferenced);
await this.audit.writeAudit({
userId: user.id,
action: 'admin.storage_reclaim_confirm',
ip: getClientIp(req),
details: { category: body.category, unreferenced: body.unreferenced },
});
return state;
}
}