fix: refresh agents on tab focus + hide stale nextRun + add cron logging
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 42s

- Add visibilitychange listener: refreshes agent data immediately when
  user returns to the tab (more reliable than interval-only polling)
- Only show toast for actions created within last 5 minutes to avoid
  false positives on page reload
- Hide "Prochaine exécution" line entirely if nextRun is in the past
  instead of showing confusing "En attente de déclenchement"
- Add detailed logging to cron endpoint for debugging

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 13:16:32 +02:00
parent f0999263a0
commit 9779dd7a79
3 changed files with 49 additions and 32 deletions

View File

@@ -46,13 +46,14 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ success: true, executed: 0 })
}
console.log(`[CronAgents] Found ${dueAgents.length} due agent(s)`)
console.log(`[CronAgents] Found ${dueAgents.length} due agent(s): ${dueAgents.map(a => a.id).join(', ')}`)
const results: { id: string; success: boolean; error?: string }[] = []
// Execute agents sequentially (max 3 per cycle) to avoid overwhelming the AI provider
// Execute agents sequentially (max 3 per cycle)
for (const agent of dueAgents.slice(0, 3)) {
try {
console.log(`[CronAgents] Executing agent ${agent.id} (${agent.frequency})`)
const { executeAgent } = await import('@/lib/ai/services/agent-executor.service')
const result = await executeAgent(agent.id, agent.userId)
@@ -64,6 +65,8 @@ export async function POST(request: NextRequest) {
timezone: agent.timezone,
})
console.log(`[CronAgents] Agent ${agent.id} done. success=${result.success}, nextRun=${nextRun?.toISOString() ?? 'null'}`)
await prisma.agent.update({
where: { id: agent.id },
data: { nextRun },
@@ -73,7 +76,6 @@ export async function POST(request: NextRequest) {
} catch (error) {
const msg = error instanceof Error ? error.message : 'Unknown error'
console.error(`[CronAgents] Agent ${agent.id} failed:`, msg)
results.push({ id: agent.id, success: false, error: msg })
// Still schedule next run even on failure
const nextRun = calculateNextRun({
@@ -86,6 +88,8 @@ export async function POST(request: NextRequest) {
where: { id: agent.id },
data: { nextRun },
})
results.push({ id: agent.id, success: false, error: msg })
}
}