Add per-group container locking with global concurrency limit to prevent concurrent containers for the same group (#89) and cap total containers. Fix message batching bug where lastAgentTimestamp advanced to trigger message instead of latest in batch, causing redundant re-processing. Move router state, sessions, and registered groups from JSON files to SQLite with automatic one-time migration. Add SIGTERM/SIGINT handlers with graceful shutdown (SIGTERM -> grace period -> SIGKILL). Add startup recovery for messages missed during crash. Remove dead code: utils.ts, Session type, isScheduledTask flag, ContainerConfig.env, getTaskRunLogs, GroupQueue.isActive. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
189 lines
4.8 KiB
TypeScript
189 lines
4.8 KiB
TypeScript
import { ChildProcess } from 'child_process';
|
|
import { CronExpressionParser } from 'cron-parser';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import {
|
|
GROUPS_DIR,
|
|
MAIN_GROUP_FOLDER,
|
|
SCHEDULER_POLL_INTERVAL,
|
|
TIMEZONE,
|
|
} from './config.js';
|
|
import { runContainerAgent, writeTasksSnapshot } from './container-runner.js';
|
|
import {
|
|
getAllTasks,
|
|
getDueTasks,
|
|
getTaskById,
|
|
logTaskRun,
|
|
updateTaskAfterRun,
|
|
} from './db.js';
|
|
import { GroupQueue } from './group-queue.js';
|
|
import { logger } from './logger.js';
|
|
import { RegisteredGroup, ScheduledTask } from './types.js';
|
|
|
|
export interface SchedulerDependencies {
|
|
sendMessage: (jid: string, text: string) => Promise<void>;
|
|
registeredGroups: () => Record<string, RegisteredGroup>;
|
|
getSessions: () => Record<string, string>;
|
|
queue: GroupQueue;
|
|
onProcess: (groupJid: string, proc: ChildProcess) => void;
|
|
}
|
|
|
|
async function runTask(
|
|
task: ScheduledTask,
|
|
deps: SchedulerDependencies,
|
|
): Promise<void> {
|
|
const startTime = Date.now();
|
|
const groupDir = path.join(GROUPS_DIR, task.group_folder);
|
|
fs.mkdirSync(groupDir, { recursive: true });
|
|
|
|
logger.info(
|
|
{ taskId: task.id, group: task.group_folder },
|
|
'Running scheduled task',
|
|
);
|
|
|
|
const groups = deps.registeredGroups();
|
|
const group = Object.values(groups).find(
|
|
(g) => g.folder === task.group_folder,
|
|
);
|
|
|
|
if (!group) {
|
|
logger.error(
|
|
{ taskId: task.id, groupFolder: task.group_folder },
|
|
'Group not found for task',
|
|
);
|
|
logTaskRun({
|
|
task_id: task.id,
|
|
run_at: new Date().toISOString(),
|
|
duration_ms: Date.now() - startTime,
|
|
status: 'error',
|
|
result: null,
|
|
error: `Group not found: ${task.group_folder}`,
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Update tasks snapshot for container to read (filtered by group)
|
|
const isMain = task.group_folder === MAIN_GROUP_FOLDER;
|
|
const tasks = getAllTasks();
|
|
writeTasksSnapshot(
|
|
task.group_folder,
|
|
isMain,
|
|
tasks.map((t) => ({
|
|
id: t.id,
|
|
groupFolder: t.group_folder,
|
|
prompt: t.prompt,
|
|
schedule_type: t.schedule_type,
|
|
schedule_value: t.schedule_value,
|
|
status: t.status,
|
|
next_run: t.next_run,
|
|
})),
|
|
);
|
|
|
|
let result: string | null = null;
|
|
let error: string | null = null;
|
|
|
|
// For group context mode, use the group's current session
|
|
const sessions = deps.getSessions();
|
|
const sessionId =
|
|
task.context_mode === 'group' ? sessions[task.group_folder] : undefined;
|
|
|
|
try {
|
|
const output = await runContainerAgent(
|
|
group,
|
|
{
|
|
prompt: task.prompt,
|
|
sessionId,
|
|
groupFolder: task.group_folder,
|
|
chatJid: task.chat_jid,
|
|
isMain,
|
|
},
|
|
(proc) => deps.onProcess(task.chat_jid, proc),
|
|
);
|
|
|
|
if (output.status === 'error') {
|
|
error = output.error || 'Unknown error';
|
|
} else {
|
|
result = output.result;
|
|
}
|
|
|
|
logger.info(
|
|
{ taskId: task.id, durationMs: Date.now() - startTime },
|
|
'Task completed',
|
|
);
|
|
} catch (err) {
|
|
error = err instanceof Error ? err.message : String(err);
|
|
logger.error({ taskId: task.id, error }, 'Task failed');
|
|
}
|
|
|
|
const durationMs = Date.now() - startTime;
|
|
|
|
logTaskRun({
|
|
task_id: task.id,
|
|
run_at: new Date().toISOString(),
|
|
duration_ms: durationMs,
|
|
status: error ? 'error' : 'success',
|
|
result,
|
|
error,
|
|
});
|
|
|
|
let nextRun: string | null = null;
|
|
if (task.schedule_type === 'cron') {
|
|
const interval = CronExpressionParser.parse(task.schedule_value, {
|
|
tz: TIMEZONE,
|
|
});
|
|
nextRun = interval.next().toISOString();
|
|
} else if (task.schedule_type === 'interval') {
|
|
const ms = parseInt(task.schedule_value, 10);
|
|
nextRun = new Date(Date.now() + ms).toISOString();
|
|
}
|
|
// 'once' tasks have no next run
|
|
|
|
const resultSummary = error
|
|
? `Error: ${error}`
|
|
: result
|
|
? result.slice(0, 200)
|
|
: 'Completed';
|
|
updateTaskAfterRun(task.id, nextRun, resultSummary);
|
|
}
|
|
|
|
let schedulerRunning = false;
|
|
|
|
export function startSchedulerLoop(deps: SchedulerDependencies): void {
|
|
if (schedulerRunning) {
|
|
logger.debug('Scheduler loop already running, skipping duplicate start');
|
|
return;
|
|
}
|
|
schedulerRunning = true;
|
|
logger.info('Scheduler loop started');
|
|
|
|
const loop = async () => {
|
|
try {
|
|
const dueTasks = getDueTasks();
|
|
if (dueTasks.length > 0) {
|
|
logger.info({ count: dueTasks.length }, 'Found due tasks');
|
|
}
|
|
|
|
for (const task of dueTasks) {
|
|
// Re-check task status in case it was paused/cancelled
|
|
const currentTask = getTaskById(task.id);
|
|
if (!currentTask || currentTask.status !== 'active') {
|
|
continue;
|
|
}
|
|
|
|
deps.queue.enqueueTask(
|
|
currentTask.chat_jid,
|
|
currentTask.id,
|
|
() => runTask(currentTask, deps),
|
|
);
|
|
}
|
|
} catch (err) {
|
|
logger.error({ err }, 'Error in scheduler loop');
|
|
}
|
|
|
|
setTimeout(loop, SCHEDULER_POLL_INTERVAL);
|
|
};
|
|
|
|
loop();
|
|
}
|