services: lazy start services / on demand services

services are now stopped when no app is using them.

on start up, services are always created and run. they are later
stopped when unused. it's this way to facilitate the upgrade code
path. the database meta files have to be upgraded even if no app is using them.
the other hook to stop unused services is at the end of an app task.

maybe mail container is a candidate for the future where all sending is no-op.
But give this is rare, it's not implemented.
This commit is contained in:
Girish Ramakrishnan
2026-03-07 22:30:43 +05:30
parent 99c55cb22f
commit 4bc0f44789
6 changed files with 77 additions and 9 deletions

View File

@@ -1055,7 +1055,6 @@ async function onTaskFinished(error, appId, installationState, taskId, auditSour
const app = await get(appId);
const task = await tasks.get(taskId);
if (!app || !task) return;
switch (installationState) {
case ISTATE_PENDING_DATA_DIR_MIGRATION:
@@ -1070,6 +1069,10 @@ async function onTaskFinished(error, appId, installationState, taskId, auditSour
break;
}
}
// this can race with an new install task but very unlikely
debug(`onTaskFinished: checking to stop unused services. hasPending: ${appTaskManager.hasPendingTasks()}`)
if (!appTaskManager.hasPendingTasks()) safe(services.stopUnusedServices(), { debug });
}
async function getCount() {