fix(posting): cap stuck Instagram container polling and guard scheduled post create/update transitions

Open
#53 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript

Research direction

Read apps/workers/src/services/db-job-poller.ts and apps/workers/src/services/instagram-api.service.ts to trace ContainerNotReadyError retries and job state updates, then inspect apps/api/src/instagram/services/instagram-posts.service.ts for create and update flows. Check the existing worker and service tests before changing behavior. Done means polling eventually fails with a clear error, post and job creation is atomic, and edits are rejected during PROCESSING.

Written by the indexing model from the issue text.

Description

backend bug workers
Problem Description
  1. Infinite reschedule loop on stuck Instagram media containers:
    In apps/workers/src/services/instagram-api.service.ts (waitForContainerAndPublish, lines 789-815), an IN_PROGRESS media container throws ContainerNotReadyError(containerId, 5).
    In apps/workers/src/services/db-job-poller.ts (lines 272-295), catching ContainerNotReadyError resets attempts: candidate.attempts with no maximum poll count or elapsed-time cap. If an Instagram container gets stuck in IN_PROGRESS (e.g., corrupt or rejected video), the worker polls Graph API indefinitely every ~6-12s while leaving InstagramScheduledPost.status stuck in PROCESSING.

  2. Non-atomic post/job creation and unsafe edits during PROCESSING:

    • In apps/api/src/instagram/services/instagram-posts.service.ts (createScheduledPost, lines 226-264), instagramScheduledPost.create and backgroundJob.create run outside a prisma.$transaction; if backgroundJob.create fails, an orphaned post remains in SCHEDULED / PROCESSING with backgroundJobId: null.
    • updateScheduledPost (lines 278-356) only rejects PUBLISHED posts, allowing edits while post.status === 'PROCESSING' and resetting the BackgroundJob back to PENDING mid-publish, risking duplicate publishes.

Expected Behavior
  1. Container polling (ContainerNotReadyError) must have a safe maximum cap (e.g., 15-20 minutes or max ~100 polls so large ~100 MB videos have plenty of time to process), after which both BackgroundJob and InstagramScheduledPost transition to FAILED with a clear timeout error.
  2. createScheduledPost must create the InstagramScheduledPost and BackgroundJob atomically inside prisma.$transaction.
  3. updateScheduledPost must continue allowing edits while SCHEDULED, and reject edits while status === 'PROCESSING' (in addition to PUBLISHED).

Where to Look
  • apps/workers/src/services/instagram-api.service.ts (waitForContainerAndPublish, lines 789-815)
  • apps/workers/src/services/db-job-poller.ts (lines 272-295)
  • apps/api/src/instagram/services/instagram-posts.service.ts (createScheduledPost lines 226-264, updateScheduledPost lines 278-356)

Solution / Implementation Guide
  1. Cap container polling (db-job-poller.ts & publish-worker.ts):
    • Track containerPollCount and firstPolledAt in err.updatedPayload when handling ContainerNotReadyError.
    • Once containerPollCount >= MAX_CONTAINER_POLLS (e.g., 100) or Date.now() - firstPolledAt > 20 * 60 * 1000, mark both the BackgroundJob and InstagramScheduledPost as FAILED with a descriptive errorMessage instead of rescheduling.
  2. Atomic create & PROCESSING edit guard (instagram-posts.service.ts):
    • Wrap instagramScheduledPost.create, backgroundJob.create, and linking backgroundJobId inside this.prisma.$transaction(...) in createScheduledPost.
    • In updateScheduledPost, reject requests with 409 Conflict / 400 BadRequest when existing.status === 'PROCESSING'.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.