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

@@ -90,44 +90,59 @@ export function AgentsPageClient({
} }
}, []) }, [])
// Track latest action per agent to detect new executions // Track latest action ID per agent to detect new executions
// null = agent tracked with no initial actions, undefined = not tracked yet
const prevActionsRef = useRef<Record<string, string | null>>({}) const prevActionsRef = useRef<Record<string, string | null>>({})
// Poll every 30s to detect agent executions and show toast notifications // Check for new actions and show toast notifications
useEffect(() => { const checkForNewActions = useCallback((updated: AgentItem[]) => {
// Initialize tracking from initial data — use null for agents without actions for (const agent of updated) {
// so we can still detect their FIRST execution const lastAction = agent.actions[0]
for (const agent of initialAgents) { if (!lastAction) continue
prevActionsRef.current[agent.id] = agent.actions[0]?.id ?? null
}
const interval = setInterval(async () => { const prevId = prevActionsRef.current[agent.id]
const updated = await refreshAgents() if (prevId === undefined) continue // Not tracked
if (!updated) return
for (const agent of updated) { if (prevId !== lastAction.id) {
const lastAction = agent.actions[0] // Only toast for recently created actions (within 5 min)
if (!lastAction) continue const age = Date.now() - new Date(lastAction.createdAt).getTime()
if (age < 5 * 60 * 1000) {
const prevId = prevActionsRef.current[agent.id]
// undefined = agent not in initial list (created by someone else, skip)
if (prevId === undefined) continue
if (prevId !== lastAction.id) {
// New execution detected (first action ever, or new run)
if (lastAction.status === 'success') { if (lastAction.status === 'success') {
toast.success(t('agents.toasts.autoRunSuccess', { name: agent.name })) toast.success(t('agents.toasts.autoRunSuccess', { name: agent.name }))
} else if (lastAction.status === 'failure') { } else if (lastAction.status === 'failure') {
toast.error(t('agents.toasts.autoRunError', { name: agent.name })) toast.error(t('agents.toasts.autoRunError', { name: agent.name }))
} }
} }
prevActionsRef.current[agent.id] = lastAction.id
} }
prevActionsRef.current[agent.id] = lastAction.id
}
}, [t])
useEffect(() => {
// Initialize tracking from initial data
for (const agent of initialAgents) {
prevActionsRef.current[agent.id] = agent.actions[0]?.id ?? null
}
// Interval polling every 30s
const interval = setInterval(async () => {
const updated = await refreshAgents()
if (updated) checkForNewActions(updated)
}, 30000) }, 30000)
return () => clearInterval(interval) // Refresh immediately when user comes back to the tab
}, []) const onVisible = async () => {
if (document.visibilityState === 'visible') {
const updated = await refreshAgents()
if (updated) checkForNewActions(updated)
}
}
document.addEventListener('visibilitychange', onVisible)
return () => {
clearInterval(interval)
document.removeEventListener('visibilitychange', onVisible)
}
}, [refreshAgents, checkForNewActions])
const handleToggle = useCallback((id: string, isEnabled: boolean) => { const handleToggle = useCallback((id: string, isEnabled: boolean) => {
setAgents(prev => prev.map(a => a.id === id ? { ...a, isEnabled } : a)) setAgents(prev => prev.map(a => a.id === id ? { ...a, isEnabled } : a))

View File

@@ -46,13 +46,14 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ success: true, executed: 0 }) 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 }[] = [] 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)) { for (const agent of dueAgents.slice(0, 3)) {
try { try {
console.log(`[CronAgents] Executing agent ${agent.id} (${agent.frequency})`)
const { executeAgent } = await import('@/lib/ai/services/agent-executor.service') const { executeAgent } = await import('@/lib/ai/services/agent-executor.service')
const result = await executeAgent(agent.id, agent.userId) const result = await executeAgent(agent.id, agent.userId)
@@ -64,6 +65,8 @@ export async function POST(request: NextRequest) {
timezone: agent.timezone, timezone: agent.timezone,
}) })
console.log(`[CronAgents] Agent ${agent.id} done. success=${result.success}, nextRun=${nextRun?.toISOString() ?? 'null'}`)
await prisma.agent.update({ await prisma.agent.update({
where: { id: agent.id }, where: { id: agent.id },
data: { nextRun }, data: { nextRun },
@@ -73,7 +76,6 @@ export async function POST(request: NextRequest) {
} catch (error) { } catch (error) {
const msg = error instanceof Error ? error.message : 'Unknown error' const msg = error instanceof Error ? error.message : 'Unknown error'
console.error(`[CronAgents] Agent ${agent.id} failed:`, msg) console.error(`[CronAgents] Agent ${agent.id} failed:`, msg)
results.push({ id: agent.id, success: false, error: msg })
// Still schedule next run even on failure // Still schedule next run even on failure
const nextRun = calculateNextRun({ const nextRun = calculateNextRun({
@@ -86,6 +88,8 @@ export async function POST(request: NextRequest) {
where: { id: agent.id }, where: { id: agent.id },
data: { nextRun }, data: { nextRun },
}) })
results.push({ id: agent.id, success: false, error: msg })
} }
} }

View File

@@ -197,14 +197,12 @@ export function AgentCard({ agent, onEdit, onRefresh, onToggle }: AgentCardProps
<span>{t('agents.metadata.executions', { count: agent._count.actions })}</span> <span>{t('agents.metadata.executions', { count: agent._count.actions })}</span>
</div> </div>
{agent.frequency !== 'manual' && agent.nextRun && ( {agent.frequency !== 'manual' && agent.nextRun && new Date(agent.nextRun) > new Date() && (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground mb-3"> <div className="flex items-center gap-1.5 text-xs text-muted-foreground mb-3">
<Clock className="w-3 h-3" /> <Clock className="w-3 h-3" />
{t('agents.schedule.nextRun')}{' '} {t('agents.schedule.nextRun')}{' '}
{mounted {mounted
? new Date(agent.nextRun) > new Date() ? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale })
? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale })
: t('agents.schedule.pending')
: new Date(agent.nextRun).toISOString().split('T')[0]} : new Date(agent.nextRun).toISOString().split('T')[0]}
</div> </div>
)} )}