mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +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
@@ -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