feat: auto-trigger scheduled agents + fix stale nextRun display
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 41s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 41s
- Add background scheduler in entrypoint: calls /api/cron/agents every 5 minutes to trigger due agents automatically - Recalculate stale nextRun (past dates) in getAgents() so the display always shows a future time - Agent card shows "En attente de déclenchement" instead of "il y a X" when nextRun is in the past Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -206,6 +206,26 @@ export async function getAgents() {
|
|||||||
orderBy: { createdAt: 'desc' }
|
orderBy: { createdAt: 'desc' }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Fix stale nextRun: recalculate if in the past for scheduled agents
|
||||||
|
const now = new Date()
|
||||||
|
for (const agent of agents) {
|
||||||
|
if (agent.frequency !== 'manual' && agent.nextRun && new Date(agent.nextRun) < now) {
|
||||||
|
const nextRun = calculateNextRun({
|
||||||
|
frequency: agent.frequency,
|
||||||
|
scheduledTime: agent.scheduledTime,
|
||||||
|
scheduledDay: agent.scheduledDay,
|
||||||
|
timezone: agent.timezone,
|
||||||
|
})
|
||||||
|
if (nextRun) {
|
||||||
|
await prisma.agent.update({
|
||||||
|
where: { id: agent.id },
|
||||||
|
data: { nextRun },
|
||||||
|
})
|
||||||
|
agent.nextRun = nextRun
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return agents
|
return agents
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching agents:', error)
|
console.error('Error fetching agents:', error)
|
||||||
|
|||||||
@@ -202,7 +202,9 @@ export function AgentCard({ agent, onEdit, onRefresh, onToggle }: AgentCardProps
|
|||||||
<Clock className="w-3 h-3" />
|
<Clock className="w-3 h-3" />
|
||||||
{t('agents.schedule.nextRun')}{' '}
|
{t('agents.schedule.nextRun')}{' '}
|
||||||
{mounted
|
{mounted
|
||||||
? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale })
|
? new Date(agent.nextRun) > new Date()
|
||||||
|
? 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>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -4,5 +4,14 @@ set -e
|
|||||||
echo "Running Prisma migrations..."
|
echo "Running Prisma migrations..."
|
||||||
node ./node_modules/prisma/build/index.js migrate deploy
|
node ./node_modules/prisma/build/index.js migrate deploy
|
||||||
|
|
||||||
|
# Background scheduler: call /api/cron/agents every 5 minutes
|
||||||
|
(
|
||||||
|
sleep 60
|
||||||
|
while true; do
|
||||||
|
wget -q -O /dev/null --post-data='' "http://localhost:3000/api/cron/agents" 2>/dev/null || true
|
||||||
|
sleep 300
|
||||||
|
done
|
||||||
|
) &
|
||||||
|
|
||||||
echo "Starting server..."
|
echo "Starting server..."
|
||||||
exec node server.js
|
exec node server.js
|
||||||
|
|||||||
@@ -1256,6 +1256,7 @@
|
|||||||
},
|
},
|
||||||
"schedule": {
|
"schedule": {
|
||||||
"nextRun": "Next run",
|
"nextRun": "Next run",
|
||||||
|
"pending": "Pending trigger",
|
||||||
"time": "Time",
|
"time": "Time",
|
||||||
"dayOfWeek": "Day of week",
|
"dayOfWeek": "Day of week",
|
||||||
"dayOfMonth": "Day of month",
|
"dayOfMonth": "Day of month",
|
||||||
|
|||||||
@@ -1252,6 +1252,7 @@
|
|||||||
},
|
},
|
||||||
"schedule": {
|
"schedule": {
|
||||||
"nextRun": "Prochaine exécution",
|
"nextRun": "Prochaine exécution",
|
||||||
|
"pending": "En attente de déclenchement",
|
||||||
"time": "Heure",
|
"time": "Heure",
|
||||||
"dayOfWeek": "Jour de la semaine",
|
"dayOfWeek": "Jour de la semaine",
|
||||||
"dayOfMonth": "Jour du mois",
|
"dayOfMonth": "Jour du mois",
|
||||||
|
|||||||
Reference in New Issue
Block a user