mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix(schedules): clean up orphaned schedule records when repo is deleted (#255)
* fix(schedules): clean up orphaned schedule records and add schedule URL state management * refactor(schedules): consolidate schedule tab state into useScheduleUrlState * fix(schedules): cascade delete schedule runs/jobs and preserve assistant schedules in orphan cleanup * refactor(schedules): replace deleteJobsByRepo with prepareRepoDelete, add lightweight ID query, remove dead mock * refactor(schedules): consolidate PromptTemplateDialog form state into single object
This commit is contained in:
1 parent
70cb62b77b
commit
cc89a524c4
24 files changed
+1938
-327
No files matched your search
@@ -209,7 +209,8 @@ export function updateRepoBranch(db: Database, id: number, branch: string): void
|
||||
}
|
||||
|
||||
export function deleteRepo(db: Database, id: number): void {
|
||||
db.prepare('DELETE FROM schedule_runs WHERE repo_id = ?').run(id)
|
||||
db.prepare('DELETE FROM schedule_jobs WHERE repo_id = ?').run(id)
|
||||
const stmt = db.prepare('DELETE FROM repos WHERE id = ?')
|
||||
stmt.run(id)
|
||||
}
|
||||
|
||||
@@ -116,6 +116,12 @@ export function listScheduleJobsByRepo(db: Database, repoId: number): ScheduleJo
|
||||
return rows.map(rowToScheduleJob)
|
||||
}
|
||||
|
||||
export function listScheduleJobIdsByRepo(db: Database, repoId: number): number[] {
|
||||
const stmt = db.prepare('SELECT id FROM schedule_jobs WHERE repo_id = ? ORDER BY created_at DESC')
|
||||
const rows = stmt.all(repoId) as Array<{ id: number }>
|
||||
return rows.map((row) => row.id)
|
||||
}
|
||||
|
||||
export function listEnabledScheduleJobs(db: Database): ScheduleJob[] {
|
||||
const stmt = db.prepare('SELECT * FROM schedule_jobs WHERE enabled = 1 ORDER BY id ASC')
|
||||
const rows = stmt.all() as ScheduleJobRow[]
|
||||
@@ -200,11 +206,29 @@ export function updateScheduleJob(db: Database, repoId: number, jobId: number, i
|
||||
}
|
||||
|
||||
export function deleteScheduleJob(db: Database, repoId: number, jobId: number): boolean {
|
||||
db.prepare('DELETE FROM schedule_runs WHERE repo_id = ? AND job_id = ?').run(repoId, jobId)
|
||||
const stmt = db.prepare('DELETE FROM schedule_jobs WHERE repo_id = ? AND id = ?')
|
||||
const result = stmt.run(repoId, jobId)
|
||||
return result.changes > 0
|
||||
}
|
||||
|
||||
export function cleanupOrphanedSchedules(db: Database): { orphanedJobs: number; orphanedRuns: number } {
|
||||
const runStmt = db.prepare(`
|
||||
DELETE FROM schedule_runs
|
||||
WHERE (repo_id != ? AND repo_id NOT IN (SELECT id FROM repos))
|
||||
OR job_id NOT IN (SELECT id FROM schedule_jobs)
|
||||
`)
|
||||
const orphanedRuns = runStmt.run(ASSISTANT_REPO_ID).changes
|
||||
|
||||
const jobStmt = db.prepare(`
|
||||
DELETE FROM schedule_jobs
|
||||
WHERE repo_id != ? AND repo_id NOT IN (SELECT id FROM repos)
|
||||
`)
|
||||
const orphanedJobs = jobStmt.run(ASSISTANT_REPO_ID).changes
|
||||
|
||||
return { orphanedJobs, orphanedRuns }
|
||||
}
|
||||
|
||||
export function updateScheduleJobRunState(db: Database, repoId: number, jobId: number, values: { lastRunAt: number; nextRunAt?: number | null }): void {
|
||||
const stmt = db.prepare('UPDATE schedule_jobs SET last_run_at = ?, next_run_at = ?, updated_at = ? WHERE repo_id = ? AND id = ?')
|
||||
stmt.run(values.lastRunAt, values.nextRunAt ?? null, Date.now(), repoId, jobId)
|
||||
|
||||
@@ -37,6 +37,7 @@ function createTestApp(db: Database, openCodeClient: OpenCodeClient = createStub
|
||||
listSchedules: () => [],
|
||||
updateSchedule: () => {},
|
||||
deleteSchedule: () => {},
|
||||
prepareRepoDelete: () => {},
|
||||
} as any
|
||||
app.route('/repos', createRepoRoutes(db, stubGitAuthService, scheduleService, openCodeClient))
|
||||
return app
|
||||
|
||||
@@ -273,6 +273,8 @@ app.get('/', async (c) => {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
}
|
||||
|
||||
scheduleService.prepareRepoDelete(id)
|
||||
|
||||
await repoService.deleteRepoFiles(database, id)
|
||||
|
||||
return c.json({ success: true })
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
import { getRepoById } from '../db/queries'
|
||||
import type { ScheduleJobWithRepo } from '../db/schedules'
|
||||
import {
|
||||
cleanupOrphanedSchedules,
|
||||
createScheduleJob,
|
||||
createScheduleRun,
|
||||
deleteScheduleJob,
|
||||
@@ -19,6 +20,7 @@ import {
|
||||
listAllScheduleJobsWithRepos,
|
||||
listAllScheduleRuns,
|
||||
listEnabledScheduleJobs,
|
||||
listScheduleJobIdsByRepo,
|
||||
listScheduleJobsByRepo,
|
||||
listRunningScheduleRuns,
|
||||
listScheduleRunsByJob,
|
||||
@@ -444,6 +446,26 @@ export class ScheduleService {
|
||||
this.onJobChange?.(null, jobId)
|
||||
}
|
||||
|
||||
prepareRepoDelete(repoId: number): void {
|
||||
const jobIds = listScheduleJobIdsByRepo(this.db, repoId)
|
||||
for (const jobId of jobIds) {
|
||||
this.onJobChange?.(null, jobId)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes any schedule_jobs and schedule_runs whose repo_id (or job_id for
|
||||
* runs) no longer exists in the repos / schedule_jobs table. Safe to call
|
||||
* on every startup — no-op when there are no orphans.
|
||||
*/
|
||||
cleanupOrphanedSchedules(): { orphanedJobs: number; orphanedRuns: number } {
|
||||
const result = cleanupOrphanedSchedules(this.db)
|
||||
if (result.orphanedJobs > 0 || result.orphanedRuns > 0) {
|
||||
logger.info(`Cleaned up ${result.orphanedJobs} orphaned schedule job(s) and ${result.orphanedRuns} run(s)`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
listRuns(repoId: number, jobId: number, limit: number = 20): ScheduleRun[] {
|
||||
this.assertJob(repoId, jobId)
|
||||
return listScheduleRunsByJob(this.db, repoId, jobId, limit)
|
||||
@@ -1120,6 +1142,11 @@ export class ScheduleRunner {
|
||||
}
|
||||
})
|
||||
|
||||
// Clean up any schedule records whose repo no longer exists. This handles
|
||||
// leftovers from before foreign-key enforcement was enabled, and guards
|
||||
// against edge cases where a repo row was removed outside the normal flow.
|
||||
this.scheduleService.cleanupOrphanedSchedules()
|
||||
|
||||
await this.scheduleService.recoverRunningRuns()
|
||||
this.registerAllEnabledJobs()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user