fix: ensure OneCLI agents exist for all groups on startup

This commit is contained in:
Guy Ben Aharon
2026-03-23 14:45:41 +02:00
committed by Guy Ben-Aharon
parent 7f6298a1bb
commit 2583af7ead
4 changed files with 34 additions and 21 deletions

View File

@@ -74,6 +74,25 @@ const queue = new GroupQueue();
const onecli = new OneCLI({ url: ONECLI_URL });
function ensureOneCLIAgent(jid: string, group: RegisteredGroup): void {
if (group.isMain) return;
const identifier = group.folder.toLowerCase().replace(/_/g, '-');
onecli.ensureAgent({ name: group.name, identifier }).then(
(res) => {
logger.info(
{ jid, identifier, created: res.created },
'OneCLI agent ensured',
);
},
(err) => {
logger.debug(
{ jid, identifier, err: String(err) },
'OneCLI agent ensure skipped',
);
},
);
}
function loadState(): void {
lastTimestamp = getRouterState('last_timestamp') || '';
const agentTs = getRouterState('last_agent_timestamp');
@@ -114,22 +133,8 @@ function registerGroup(jid: string, group: RegisteredGroup): void {
// Create group folder
fs.mkdirSync(path.join(groupDir, 'logs'), { recursive: true });
// Create a corresponding OneCLI agent (best-effort, non-blocking)
const identifier = group.folder.toLowerCase().replace(/_/g, '-');
onecli.createAgent({ name: group.name, identifier }).then(
(agent) => {
logger.info(
{ jid, agentId: agent.id, identifier },
'OneCLI agent created',
);
},
(err) => {
logger.debug(
{ jid, identifier, err: String(err) },
'OneCLI agent creation skipped',
);
},
);
// Ensure a corresponding OneCLI agent exists (best-effort, non-blocking)
ensureOneCLIAgent(jid, group);
logger.info(
{ jid, name: group.name, folder: group.folder },
@@ -493,6 +498,13 @@ async function main(): Promise<void> {
initDatabase();
logger.info('Database initialized');
loadState();
// Ensure OneCLI agents exist for all registered groups.
// Recovers from missed creates (e.g. OneCLI was down at registration time).
for (const [jid, group] of Object.entries(registeredGroups)) {
ensureOneCLIAgent(jid, group);
}
restoreRemoteControl();
// Graceful shutdown handlers