Spaces:
Runtime error
Runtime error
nyk Doan Bac Tam commited on
fix: cherry-pick improvements from PR #57 (#71)
Browse filesCherry-picks three valuable fixes from @doanbactam 's WebSocket refactor PR:
1. Feed item ID collision fix — prefix log IDs with 'log-' to avoid
React key collisions with activity IDs in the combined feed
2. Jittered reconnect backoff — add random jitter (0-50% of base) to
WebSocket exponential backoff to prevent thundering-herd reconnects
when multiple tabs reconnect after a server restart
3. Cron job deduplication + async I/O — deduplicate jobs.json entries
by name (keeps latest), prevent duplicates on add, and convert
sync file reads/writes to async to avoid blocking the event loop
Co-authored-by: Doan Bac Tam <24356000+doanbactam@users.noreply.github.com>
- src/app/api/cron/route.ts +30 -15
- src/components/layout/live-feed.tsx +1 -1
- src/lib/websocket.ts +2 -1
src/app/api/cron/route.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
|
| 2 |
import { requireRole } from '@/lib/auth'
|
| 3 |
import { config } from '@/lib/config'
|
| 4 |
import { logger } from '@/lib/logger'
|
| 5 |
-
import
|
| 6 |
import path from 'node:path'
|
| 7 |
|
| 8 |
interface CronJob {
|
|
@@ -72,22 +72,22 @@ function getCronFilePath(): string {
|
|
| 72 |
return path.join(openclawHome, 'cron', 'jobs.json')
|
| 73 |
}
|
| 74 |
|
| 75 |
-
function loadCronFile(): OpenClawCronFile | null {
|
| 76 |
const filePath = getCronFilePath()
|
| 77 |
if (!filePath) return null
|
| 78 |
try {
|
| 79 |
-
const raw =
|
| 80 |
return JSON.parse(raw)
|
| 81 |
} catch {
|
| 82 |
return null
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
-
function saveCronFile(data: OpenClawCronFile): boolean {
|
| 87 |
const filePath = getCronFilePath()
|
| 88 |
if (!filePath) return false
|
| 89 |
try {
|
| 90 |
-
|
| 91 |
return true
|
| 92 |
} catch (err) {
|
| 93 |
logger.error({ err }, 'Failed to write cron file')
|
|
@@ -95,6 +95,18 @@ function saveCronFile(data: OpenClawCronFile): boolean {
|
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
function mapLastStatus(status?: string): 'success' | 'error' | 'running' | undefined {
|
| 99 |
if (!status) return undefined
|
| 100 |
const s = status.toLowerCase()
|
|
@@ -140,12 +152,12 @@ export async function GET(request: NextRequest) {
|
|
| 140 |
const action = searchParams.get('action')
|
| 141 |
|
| 142 |
if (action === 'list') {
|
| 143 |
-
const cronFile = loadCronFile()
|
| 144 |
if (!cronFile || !cronFile.jobs) {
|
| 145 |
return NextResponse.json({ jobs: [] })
|
| 146 |
}
|
| 147 |
|
| 148 |
-
const jobs = cronFile.jobs.map(mapOpenClawJob)
|
| 149 |
return NextResponse.json({ jobs })
|
| 150 |
}
|
| 151 |
|
|
@@ -156,7 +168,7 @@ export async function GET(request: NextRequest) {
|
|
| 156 |
}
|
| 157 |
|
| 158 |
// Find the job to get its state info
|
| 159 |
-
const cronFile = loadCronFile()
|
| 160 |
const job = cronFile?.jobs.find(j => j.id === jobId || j.name === jobId)
|
| 161 |
|
| 162 |
const logs: Array<{ timestamp: number; message: string; level: string }> = []
|
|
@@ -209,7 +221,7 @@ export async function POST(request: NextRequest) {
|
|
| 209 |
return NextResponse.json({ error: 'Job ID or name required' }, { status: 400 })
|
| 210 |
}
|
| 211 |
|
| 212 |
-
const cronFile = loadCronFile()
|
| 213 |
if (!cronFile) {
|
| 214 |
return NextResponse.json({ error: 'Cron file not found' }, { status: 404 })
|
| 215 |
}
|
|
@@ -222,7 +234,7 @@ export async function POST(request: NextRequest) {
|
|
| 222 |
job.enabled = !job.enabled
|
| 223 |
job.updatedAtMs = Date.now()
|
| 224 |
|
| 225 |
-
if (!saveCronFile(cronFile)) {
|
| 226 |
return NextResponse.json({ error: 'Failed to save cron file' }, { status: 500 })
|
| 227 |
}
|
| 228 |
|
|
@@ -242,7 +254,7 @@ export async function POST(request: NextRequest) {
|
|
| 242 |
)
|
| 243 |
}
|
| 244 |
|
| 245 |
-
const cronFile = loadCronFile()
|
| 246 |
const job = cronFile?.jobs.find(j => j.id === id || j.name === id)
|
| 247 |
if (!job) {
|
| 248 |
return NextResponse.json({ error: 'Job not found' }, { status: 404 })
|
|
@@ -276,7 +288,7 @@ export async function POST(request: NextRequest) {
|
|
| 276 |
return NextResponse.json({ error: 'Job ID or name required' }, { status: 400 })
|
| 277 |
}
|
| 278 |
|
| 279 |
-
const cronFile = loadCronFile()
|
| 280 |
if (!cronFile) {
|
| 281 |
return NextResponse.json({ error: 'Cron file not found' }, { status: 404 })
|
| 282 |
}
|
|
@@ -288,7 +300,7 @@ export async function POST(request: NextRequest) {
|
|
| 288 |
|
| 289 |
cronFile.jobs.splice(idx, 1)
|
| 290 |
|
| 291 |
-
if (!saveCronFile(cronFile)) {
|
| 292 |
return NextResponse.json({ error: 'Failed to save cron file' }, { status: 500 })
|
| 293 |
}
|
| 294 |
|
|
@@ -305,7 +317,10 @@ export async function POST(request: NextRequest) {
|
|
| 305 |
)
|
| 306 |
}
|
| 307 |
|
| 308 |
-
const cronFile = loadCronFile() || { version: 1, jobs: [] }
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
const newJob: OpenClawCronJob = {
|
| 311 |
id: `mc-${Date.now().toString(36)}`,
|
|
@@ -330,7 +345,7 @@ export async function POST(request: NextRequest) {
|
|
| 330 |
|
| 331 |
cronFile.jobs.push(newJob)
|
| 332 |
|
| 333 |
-
if (!saveCronFile(cronFile)) {
|
| 334 |
return NextResponse.json({ error: 'Failed to save cron file' }, { status: 500 })
|
| 335 |
}
|
| 336 |
|
|
|
|
| 2 |
import { requireRole } from '@/lib/auth'
|
| 3 |
import { config } from '@/lib/config'
|
| 4 |
import { logger } from '@/lib/logger'
|
| 5 |
+
import { readFile, writeFile } from 'node:fs/promises'
|
| 6 |
import path from 'node:path'
|
| 7 |
|
| 8 |
interface CronJob {
|
|
|
|
| 72 |
return path.join(openclawHome, 'cron', 'jobs.json')
|
| 73 |
}
|
| 74 |
|
| 75 |
+
async function loadCronFile(): Promise<OpenClawCronFile | null> {
|
| 76 |
const filePath = getCronFilePath()
|
| 77 |
if (!filePath) return null
|
| 78 |
try {
|
| 79 |
+
const raw = await readFile(filePath, 'utf-8')
|
| 80 |
return JSON.parse(raw)
|
| 81 |
} catch {
|
| 82 |
return null
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
+
async function saveCronFile(data: OpenClawCronFile): Promise<boolean> {
|
| 87 |
const filePath = getCronFilePath()
|
| 88 |
if (!filePath) return false
|
| 89 |
try {
|
| 90 |
+
await writeFile(filePath, JSON.stringify(data, null, 2))
|
| 91 |
return true
|
| 92 |
} catch (err) {
|
| 93 |
logger.error({ err }, 'Failed to write cron file')
|
|
|
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
+
/** Deduplicate jobs by name — keep the latest (by createdAtMs) per unique name */
|
| 99 |
+
function deduplicateJobs(jobs: OpenClawCronJob[]): OpenClawCronJob[] {
|
| 100 |
+
const latest = new Map<string, OpenClawCronJob>()
|
| 101 |
+
for (const job of jobs) {
|
| 102 |
+
const existing = latest.get(job.name)
|
| 103 |
+
if (!existing || (job.createdAtMs ?? 0) > (existing.createdAtMs ?? 0)) {
|
| 104 |
+
latest.set(job.name, job)
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
return [...latest.values()]
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
function mapLastStatus(status?: string): 'success' | 'error' | 'running' | undefined {
|
| 111 |
if (!status) return undefined
|
| 112 |
const s = status.toLowerCase()
|
|
|
|
| 152 |
const action = searchParams.get('action')
|
| 153 |
|
| 154 |
if (action === 'list') {
|
| 155 |
+
const cronFile = await loadCronFile()
|
| 156 |
if (!cronFile || !cronFile.jobs) {
|
| 157 |
return NextResponse.json({ jobs: [] })
|
| 158 |
}
|
| 159 |
|
| 160 |
+
const jobs = deduplicateJobs(cronFile.jobs).map(mapOpenClawJob)
|
| 161 |
return NextResponse.json({ jobs })
|
| 162 |
}
|
| 163 |
|
|
|
|
| 168 |
}
|
| 169 |
|
| 170 |
// Find the job to get its state info
|
| 171 |
+
const cronFile = await loadCronFile()
|
| 172 |
const job = cronFile?.jobs.find(j => j.id === jobId || j.name === jobId)
|
| 173 |
|
| 174 |
const logs: Array<{ timestamp: number; message: string; level: string }> = []
|
|
|
|
| 221 |
return NextResponse.json({ error: 'Job ID or name required' }, { status: 400 })
|
| 222 |
}
|
| 223 |
|
| 224 |
+
const cronFile = await loadCronFile()
|
| 225 |
if (!cronFile) {
|
| 226 |
return NextResponse.json({ error: 'Cron file not found' }, { status: 404 })
|
| 227 |
}
|
|
|
|
| 234 |
job.enabled = !job.enabled
|
| 235 |
job.updatedAtMs = Date.now()
|
| 236 |
|
| 237 |
+
if (!(await saveCronFile(cronFile))) {
|
| 238 |
return NextResponse.json({ error: 'Failed to save cron file' }, { status: 500 })
|
| 239 |
}
|
| 240 |
|
|
|
|
| 254 |
)
|
| 255 |
}
|
| 256 |
|
| 257 |
+
const cronFile = await loadCronFile()
|
| 258 |
const job = cronFile?.jobs.find(j => j.id === id || j.name === id)
|
| 259 |
if (!job) {
|
| 260 |
return NextResponse.json({ error: 'Job not found' }, { status: 404 })
|
|
|
|
| 288 |
return NextResponse.json({ error: 'Job ID or name required' }, { status: 400 })
|
| 289 |
}
|
| 290 |
|
| 291 |
+
const cronFile = await loadCronFile()
|
| 292 |
if (!cronFile) {
|
| 293 |
return NextResponse.json({ error: 'Cron file not found' }, { status: 404 })
|
| 294 |
}
|
|
|
|
| 300 |
|
| 301 |
cronFile.jobs.splice(idx, 1)
|
| 302 |
|
| 303 |
+
if (!(await saveCronFile(cronFile))) {
|
| 304 |
return NextResponse.json({ error: 'Failed to save cron file' }, { status: 500 })
|
| 305 |
}
|
| 306 |
|
|
|
|
| 317 |
)
|
| 318 |
}
|
| 319 |
|
| 320 |
+
const cronFile = (await loadCronFile()) || { version: 1, jobs: [] }
|
| 321 |
+
|
| 322 |
+
// Prevent duplicates: remove existing jobs with the same name
|
| 323 |
+
cronFile.jobs = cronFile.jobs.filter(j => j.name !== name)
|
| 324 |
|
| 325 |
const newJob: OpenClawCronJob = {
|
| 326 |
id: `mc-${Date.now().toString(36)}`,
|
|
|
|
| 345 |
|
| 346 |
cronFile.jobs.push(newJob)
|
| 347 |
|
| 348 |
+
if (!(await saveCronFile(cronFile))) {
|
| 349 |
return NextResponse.json({ error: 'Failed to save cron file' }, { status: 500 })
|
| 350 |
}
|
| 351 |
|
src/components/layout/live-feed.tsx
CHANGED
|
@@ -10,7 +10,7 @@ export function LiveFeed() {
|
|
| 10 |
// Combine logs and activities into a unified feed
|
| 11 |
const feedItems = [
|
| 12 |
...logs.slice(0, 30).map(log => ({
|
| 13 |
-
id: log.id,
|
| 14 |
type: 'log' as const,
|
| 15 |
level: log.level,
|
| 16 |
message: log.message,
|
|
|
|
| 10 |
// Combine logs and activities into a unified feed
|
| 11 |
const feedItems = [
|
| 12 |
...logs.slice(0, 30).map(log => ({
|
| 13 |
+
id: `log-${log.id}`,
|
| 14 |
type: 'log' as const,
|
| 15 |
level: log.level,
|
| 16 |
message: log.message,
|
src/lib/websocket.ts
CHANGED
|
@@ -415,7 +415,8 @@ export function useWebSocket() {
|
|
| 415 |
// Auto-reconnect logic with exponential backoff (uses ref to avoid stale closure)
|
| 416 |
const attempts = reconnectAttemptsRef.current
|
| 417 |
if (attempts < maxReconnectAttempts) {
|
| 418 |
-
const
|
|
|
|
| 419 |
console.log(`Reconnecting in ${timeout}ms... (attempt ${attempts + 1}/${maxReconnectAttempts})`)
|
| 420 |
|
| 421 |
reconnectAttemptsRef.current = attempts + 1
|
|
|
|
| 415 |
// Auto-reconnect logic with exponential backoff (uses ref to avoid stale closure)
|
| 416 |
const attempts = reconnectAttemptsRef.current
|
| 417 |
if (attempts < maxReconnectAttempts) {
|
| 418 |
+
const base = Math.min(Math.pow(2, attempts) * 1000, 30000)
|
| 419 |
+
const timeout = Math.round(base + Math.random() * base * 0.5)
|
| 420 |
console.log(`Reconnecting in ${timeout}ms... (attempt ${attempts + 1}/${maxReconnectAttempts})`)
|
| 421 |
|
| 422 |
reconnectAttemptsRef.current = attempts + 1
|