docs: add status sync migration guide for Studio team
- Explains new automatic trigger-based synchronization - Provides code examples for removing manual cache updates - Documents new status query and sync APIs - Includes migration checklist and testing guide
This commit is contained in:
@@ -0,0 +1,169 @@
|
|||||||
|
# Status Synchronization - Migration Guide for Studio Team
|
||||||
|
|
||||||
|
**Date**: 2026-10-18
|
||||||
|
**From**: Core Team
|
||||||
|
**To**: Studio Team
|
||||||
|
**Priority**: Medium
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The Core team has implemented automatic status synchronization between `monitor_jobs.status` and `videos.status` using a PostgreSQL trigger. This eliminates the need for manual cache updates in the frontend.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What Changed
|
||||||
|
|
||||||
|
### Before (Manual Sync)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// OLD: Manual cache update after pause
|
||||||
|
ctxPause: async (uuid) => {
|
||||||
|
await api.post(`/api/v1/jobs/${uuid}/pause`)
|
||||||
|
// Manual cache update
|
||||||
|
filesCache.value.find(f => f.uuid === uuid).status = 'paused'
|
||||||
|
fileStatusCache.value[uuid] = 'paused'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### After (Automatic Sync via Trigger)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// NEW: Just call API and reload
|
||||||
|
ctxPause: async (uuid) => {
|
||||||
|
await api.post(`/api/v1/jobs/${uuid}/pause`)
|
||||||
|
// Trigger automatically synced videos.status
|
||||||
|
await loadFiles() // or invalidate cache
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## New API Endpoints
|
||||||
|
|
||||||
|
### 1. GET `/api/v1/jobs/:uuid/status`
|
||||||
|
|
||||||
|
**Purpose**: Get consistent status from both tables.
|
||||||
|
|
||||||
|
**Response**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"uuid": "7a1523df54e2161a54e4577d1835acfd",
|
||||||
|
"monitor_status": "paused",
|
||||||
|
"video_status": "paused",
|
||||||
|
"is_consistent": true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Usage**:
|
||||||
|
```typescript
|
||||||
|
const status = await api.get(`/api/v1/jobs/${uuid}/status`)
|
||||||
|
if (status.data.is_consistent) {
|
||||||
|
console.log('Status is consistent')
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. POST `/api/v1/admin/sync-status`
|
||||||
|
|
||||||
|
**Purpose**: Manually fix any historical status inconsistencies.
|
||||||
|
|
||||||
|
**Response**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"mismatches_found": 0,
|
||||||
|
"mismatches_fixed": 0,
|
||||||
|
"message": "Found 0 mismatches, fixed 0"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Status Mapping
|
||||||
|
|
||||||
|
| monitor_jobs.status | videos.status | 說明 |
|
||||||
|
|---------------------|---------------|------|
|
||||||
|
| `pending` | `registered` | 等待處理 |
|
||||||
|
| `running` | `processing` | 處理中 |
|
||||||
|
| `paused` | `paused` | 暫停 |
|
||||||
|
| `completed` | `completed` | 完成 |
|
||||||
|
| `failed` | `failed` | 失敗 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Recommended Changes
|
||||||
|
|
||||||
|
### 1. Remove Manual Cache Updates
|
||||||
|
|
||||||
|
**Remove**:
|
||||||
|
```typescript
|
||||||
|
// ❌ Remove these manual updates
|
||||||
|
file.status = 'paused'
|
||||||
|
fileStatusCache.value[uuid] = 'paused'
|
||||||
|
```
|
||||||
|
|
||||||
|
**Replace with**:
|
||||||
|
```typescript
|
||||||
|
// ✅ Just reload after API call
|
||||||
|
await api.post(`/api/v1/jobs/${uuid}/pause`)
|
||||||
|
await loadFiles() // Reload from backend
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Use Status Query API for Debugging
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Optional: Check status consistency
|
||||||
|
const checkStatus = async (uuid) => {
|
||||||
|
const res = await api.get(`/api/v1/jobs/${uuid}/status`)
|
||||||
|
if (!res.data.is_consistent) {
|
||||||
|
console.warn('Status mismatch detected')
|
||||||
|
// Optionally trigger sync
|
||||||
|
await api.post('/api/v1/admin/sync-status')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Batch Operations
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Batch pause with automatic reload
|
||||||
|
const batchPause = async (uuids) => {
|
||||||
|
for (const uuid of uuids) {
|
||||||
|
await api.post(`/api/v1/jobs/${uuid}/pause`)
|
||||||
|
}
|
||||||
|
await loadFiles() // Single reload after all pauses
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Migration Checklist
|
||||||
|
|
||||||
|
- [ ] Remove manual `file.status = 'paused'` updates
|
||||||
|
- [ ] Remove manual `fileStatusCache` updates
|
||||||
|
- [ ] Use `loadFiles()` after pause/resume API calls
|
||||||
|
- [ ] (Optional) Add status consistency check for debugging
|
||||||
|
- [ ] (Optional) Add "Sync Status" button for admin users
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
1. Pause a job → Verify `videos.status` shows 'paused'
|
||||||
|
2. Resume a job → Verify `videos.status` shows 'registered'
|
||||||
|
3. Check `/api/v1/jobs/:uuid/status` → Verify `is_consistent: true`
|
||||||
|
4. Run `/api/v1/admin/sync-status` → Verify 0 mismatches
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Rollback Plan
|
||||||
|
|
||||||
|
If issues occur:
|
||||||
|
1. The trigger can be disabled: `ALTER TABLE monitor_jobs DISABLE TRIGGER trigger_sync_video_status;`
|
||||||
|
2. Manual cache updates can be restored as temporary workaround
|
||||||
|
3. Contact Core team for assistance
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Contact**: Core Team for questions or issues.
|
||||||
Reference in New Issue
Block a user