fix: ensure worker queue advances on error
This commit is contained in:
@@ -23,6 +23,7 @@ type Querier interface {
|
|||||||
GetUserWatchList(ctx context.Context, userID string) ([]GetUserWatchListRow, error)
|
GetUserWatchList(ctx context.Context, userID string) ([]GetUserWatchListRow, error)
|
||||||
GetWatchListEntry(ctx context.Context, arg GetWatchListEntryParams) (WatchListEntry, error)
|
GetWatchListEntry(ctx context.Context, arg GetWatchListEntryParams) (WatchListEntry, error)
|
||||||
GetWatchingAnime(ctx context.Context, userID string) ([]GetWatchingAnimeRow, error)
|
GetWatchingAnime(ctx context.Context, userID string) ([]GetWatchingAnimeRow, error)
|
||||||
|
MarkRelationsSynced(ctx context.Context, id int64) error
|
||||||
UpdateAnimeStatus(ctx context.Context, arg UpdateAnimeStatusParams) error
|
UpdateAnimeStatus(ctx context.Context, arg UpdateAnimeStatusParams) error
|
||||||
UpsertAnime(ctx context.Context, arg UpsertAnimeParams) (Anime, error)
|
UpsertAnime(ctx context.Context, arg UpsertAnimeParams) (Anime, error)
|
||||||
UpsertAnimeRelation(ctx context.Context, arg UpsertAnimeRelationParams) error
|
UpsertAnimeRelation(ctx context.Context, arg UpsertAnimeRelationParams) error
|
||||||
|
|||||||
@@ -86,7 +86,10 @@ ON CONFLICT (anime_id, related_anime_id) DO UPDATE SET
|
|||||||
relation_type = excluded.relation_type;
|
relation_type = excluded.relation_type;
|
||||||
|
|
||||||
-- name: UpdateAnimeStatus :exec
|
-- name: UpdateAnimeStatus :exec
|
||||||
UPDATE anime SET status = ?, relations_synced_at = CURRENT_TIMESTAMP WHERE id = ?;
|
UPDATE anime SET status = ? WHERE id = ?;
|
||||||
|
|
||||||
|
-- name: MarkRelationsSynced :exec
|
||||||
|
UPDATE anime SET relations_synced_at = CURRENT_TIMESTAMP WHERE id = ?;
|
||||||
|
|
||||||
-- name: GetAnimeNeedingRelationSync :many
|
-- name: GetAnimeNeedingRelationSync :many
|
||||||
WITH RECURSIVE sequel_chain AS (
|
WITH RECURSIVE sequel_chain AS (
|
||||||
|
|||||||
@@ -459,8 +459,17 @@ func (q *Queries) GetWatchingAnime(ctx context.Context, userID string) ([]GetWat
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const markRelationsSynced = `-- name: MarkRelationsSynced :exec
|
||||||
|
UPDATE anime SET relations_synced_at = CURRENT_TIMESTAMP WHERE id = ?
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) MarkRelationsSynced(ctx context.Context, id int64) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, markRelationsSynced, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const updateAnimeStatus = `-- name: UpdateAnimeStatus :exec
|
const updateAnimeStatus = `-- name: UpdateAnimeStatus :exec
|
||||||
UPDATE anime SET status = ?, relations_synced_at = CURRENT_TIMESTAMP WHERE id = ?
|
UPDATE anime SET status = ? WHERE id = ?
|
||||||
`
|
`
|
||||||
|
|
||||||
type UpdateAnimeStatusParams struct {
|
type UpdateAnimeStatusParams struct {
|
||||||
|
|||||||
@@ -51,10 +51,20 @@ func (w *Worker) syncRelations(ctx context.Context) {
|
|||||||
for _, a := range animes {
|
for _, a := range animes {
|
||||||
log.Printf("worker: syncing relations for anime %d (%s)", a.ID, a.TitleOriginal)
|
log.Printf("worker: syncing relations for anime %d (%s)", a.ID, a.TitleOriginal)
|
||||||
|
|
||||||
|
func() {
|
||||||
|
// Always mark as synced and sleep so the queue advances even on error.
|
||||||
|
defer func() {
|
||||||
|
err := w.db.MarkRelationsSynced(ctx, a.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("worker: failed to mark relations synced for %d: %v", a.ID, err)
|
||||||
|
}
|
||||||
|
time.Sleep(400 * time.Millisecond)
|
||||||
|
}()
|
||||||
|
|
||||||
relations, err := w.client.GetRelationsData(int(a.ID))
|
relations, err := w.client.GetRelationsData(int(a.ID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("worker: failed to fetch relations for %d: %v", a.ID, err)
|
log.Printf("worker: failed to fetch relations for %d: %v", a.ID, err)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rel := range relations.Data {
|
for _, rel := range relations.Data {
|
||||||
@@ -89,9 +99,7 @@ func (w *Worker) syncRelations(ctx context.Context) {
|
|||||||
log.Printf("worker: failed to update status for %d: %v", a.ID, err)
|
log.Printf("worker: failed to update status for %d: %v", a.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
// Sleep briefly to respect Jikan's 3 req/sec rate limit
|
|
||||||
time.Sleep(400 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user