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
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:
@@ -90,44 +90,59 @@ export function AgentsPageClient({
|
||||
}
|
||||
}, [])
|
||||
|
||||
// Track latest action per agent to detect new executions
|
||||
// null = agent tracked with no initial actions, undefined = not tracked yet
|
||||
// Track latest action ID per agent to detect new executions
|
||||
const prevActionsRef = useRef<Record<string, string | null>>({})
|
||||
|
||||
// Poll every 30s to detect agent executions and show toast notifications
|
||||
useEffect(() => {
|
||||
// Initialize tracking from initial data — use null for agents without actions
|
||||
// so we can still detect their FIRST execution
|
||||
for (const agent of initialAgents) {
|
||||
prevActionsRef.current[agent.id] = agent.actions[0]?.id ?? null
|
||||
}
|
||||
// Check for new actions and show toast notifications
|
||||
const checkForNewActions = useCallback((updated: AgentItem[]) => {
|
||||
for (const agent of updated) {
|
||||
const lastAction = agent.actions[0]
|
||||
if (!lastAction) continue
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
const updated = await refreshAgents()
|
||||
if (!updated) return
|
||||
const prevId = prevActionsRef.current[agent.id]
|
||||
if (prevId === undefined) continue // Not tracked
|
||||
|
||||
for (const agent of updated) {
|
||||
const lastAction = agent.actions[0]
|
||||
if (!lastAction) continue
|
||||
|
||||
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 (prevId !== lastAction.id) {
|
||||
// Only toast for recently created actions (within 5 min)
|
||||
const age = Date.now() - new Date(lastAction.createdAt).getTime()
|
||||
if (age < 5 * 60 * 1000) {
|
||||
if (lastAction.status === 'success') {
|
||||
toast.success(t('agents.toasts.autoRunSuccess', { name: agent.name }))
|
||||
} else if (lastAction.status === 'failure') {
|
||||
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)
|
||||
|
||||
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) => {
|
||||
setAgents(prev => prev.map(a => a.id === id ? { ...a, isEnabled } : a))
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -197,14 +197,12 @@ export function AgentCard({ agent, onEdit, onRefresh, onToggle }: AgentCardProps
|
||||
<span>{t('agents.metadata.executions', { count: agent._count.actions })}</span>
|
||||
</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">
|
||||
<Clock className="w-3 h-3" />
|
||||
{t('agents.schedule.nextRun')}{' '}
|
||||
{mounted
|
||||
? new Date(agent.nextRun) > new Date()
|
||||
? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale })
|
||||
: t('agents.schedule.pending')
|
||||
? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale })
|
||||
: new Date(agent.nextRun).toISOString().split('T')[0]}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user