Add queue management API and database queries
- Add 7 new database queries for queue management - GetStuckSyncQueueItems - Detect stuck items - GetSyncQueueStats - Queue statistics - GetNextRetryTime - Exponential backoff calc - ListAllSyncQueueItems - Admin view - IncrementSyncQueueAttempts - Retry counter - Add queue handler with 7 REST endpoints - GET /api/queue/devices/:id/stats - Queue statistics - GET /api/queue/devices/:id/items - List device queue - POST /api/queue/items/:id/retry - Retry failed item - DELETE /api/queue/items/:id - Delete queue item - DELETE /api/queue/devices/:id/clear - Clear device queue - GET /api/queue/items - List all items (admin) - Add full user/admin access control
This commit is contained in:
@@ -305,6 +305,12 @@ UPDATE users SET email = $2, updated_at = NOW() WHERE id = $1;
|
||||
-- name: UpdatePassword :exec
|
||||
UPDATE users SET password_hash = $2, updated_at = NOW() WHERE id = $1;
|
||||
|
||||
-- name: UpdateUserMaxDevices :exec
|
||||
UPDATE users SET max_devices = $2, updated_at = NOW() WHERE id = $1;
|
||||
|
||||
-- name: CountUserDevices :one
|
||||
SELECT COUNT(*) FROM devices WHERE user_id = $1;
|
||||
|
||||
-- name: DeleteUser :exec
|
||||
DELETE FROM users WHERE id = $1;
|
||||
|
||||
@@ -720,6 +726,64 @@ DELETE FROM sync_queue WHERE id = $1;
|
||||
-- name: ClearDeviceSyncQueue :exec
|
||||
DELETE FROM sync_queue WHERE device_id = $1;
|
||||
|
||||
-- name: GetFailedSyncQueueItems :many
|
||||
SELECT * FROM sync_queue
|
||||
WHERE status = 'failed' AND attempts < max_attempts
|
||||
ORDER BY priority ASC, created_at ASC
|
||||
LIMIT $1;
|
||||
|
||||
-- name: GetStuckSyncQueueItems :many
|
||||
SELECT * FROM sync_queue
|
||||
WHERE status = 'processing' AND created_at < NOW() - INTERVAL '1 hour'
|
||||
ORDER BY priority ASC, created_at ASC;
|
||||
|
||||
-- name: IncrementSyncQueueAttempts :one
|
||||
UPDATE sync_queue
|
||||
SET
|
||||
attempts = attempts + 1,
|
||||
status = CASE
|
||||
WHEN attempts + 1 >= max_attempts THEN 'failed'
|
||||
ELSE 'pending'
|
||||
END,
|
||||
error_message = $2
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetSyncQueueStats :one
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE status = 'pending') as pending_count,
|
||||
COUNT(*) FILTER (WHERE status = 'processing') as processing_count,
|
||||
COUNT(*) FILTER (WHERE status = 'failed') as failed_count,
|
||||
COUNT(*) FILTER (WHERE status = 'completed') as completed_count,
|
||||
COUNT(*) as total_count
|
||||
FROM sync_queue
|
||||
WHERE device_id = $1;
|
||||
|
||||
-- name: ListAllSyncQueueItems :many
|
||||
SELECT
|
||||
sq.*,
|
||||
d.device_name,
|
||||
d.device_type,
|
||||
u.email as user_email,
|
||||
mi.title as media_title
|
||||
FROM sync_queue sq
|
||||
JOIN devices d ON sq.device_id = d.id
|
||||
JOIN users u ON d.user_id = u.id
|
||||
LEFT JOIN media_items mi ON sq.media_item_id = mi.id
|
||||
ORDER BY sq.priority ASC, sq.created_at DESC
|
||||
LIMIT $1 OFFSET $2;
|
||||
|
||||
-- name: GetNextRetryTime :one
|
||||
SELECT
|
||||
CASE
|
||||
WHEN attempts = 0 THEN NOW()
|
||||
WHEN attempts = 1 THEN NOW() + INTERVAL '1 minute'
|
||||
WHEN attempts = 2 THEN NOW() + INTERVAL '5 minutes'
|
||||
WHEN attempts = 3 THEN NOW() + INTERVAL '15 minutes'
|
||||
WHEN attempts = 4 THEN NOW() + INTERVAL '1 hour'
|
||||
ELSE NOW() + INTERVAL '24 hours'
|
||||
END as next_retry_time;
|
||||
|
||||
-- Conflict Resolution
|
||||
-- name: CreateSyncConflict :one
|
||||
INSERT INTO sync_conflicts (media_item_id, user_id, conflict_type, conflict_data)
|
||||
|
||||
Reference in New Issue
Block a user