fix: clean up orphaned instance records on enqueue failure (#686)#687
fix: clean up orphaned instance records on enqueue failure (#686)#687poyrazK wants to merge 3 commits into
Conversation
…revent orphaned records
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR adds compensating cleanup to the instance service: when task enqueue fails after an instance is created in the database, the service now attempts to delete that record and logs any delete errors. Both LaunchInstance and LaunchInstanceWithOptions paths are fixed and covered by unit and integration tests. ChangesInstance Enqueue Failure Cleanup
🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
internal/core/services/instance_unit_test.go (1)
224-261: ⚡ Quick winAdd one subtest for rollback delete failure.
These new cases validate rollback success, but not the branch where
repo.Deletefails after enqueue failure. Add a case asserting the returned error remains the enqueue error (and delete failure is only logged) to prevent regression in compensating behavior.Also applies to: 285-309
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/core/services/instance_unit_test.go` around lines 224 - 261, Add a new subtest mirroring "EnqueueFailure" that simulates taskQueue.Enqueue returning an error and repo.Delete also failing; call svc.LaunchInstance with the same LaunchParams, set taskQueue.On("Enqueue", ...).Return(errors.New("enqueue error")) and repo.On("Delete", ...).Return(errors.New("delete error")), then assert the returned error contains the original enqueue error (e.g., "failed to enqueue provisioning task") and not the delete error, and verify repo.Delete was called but its error is not propagated (only logged). Apply the same additional subtest pattern where the other test block exists (the similar block around lines 285-309) to cover both places.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/core/services/instance_test.go`:
- Around line 729-730: The test is silently ignoring errors from seeding the
instance type; replace the blank identifier on the call to itRepo.Create with
error handling: capture the returned error from itRepo.Create(ctx, defaultType)
and fail the test immediately if non-nil (e.g., use t.Fatalf or require.NoError)
so that failures to create the fixture (defaultType / testInstanceType) are
reported clearly and stop the test.
---
Nitpick comments:
In `@internal/core/services/instance_unit_test.go`:
- Around line 224-261: Add a new subtest mirroring "EnqueueFailure" that
simulates taskQueue.Enqueue returning an error and repo.Delete also failing;
call svc.LaunchInstance with the same LaunchParams, set taskQueue.On("Enqueue",
...).Return(errors.New("enqueue error")) and repo.On("Delete",
...).Return(errors.New("delete error")), then assert the returned error contains
the original enqueue error (e.g., "failed to enqueue provisioning task") and not
the delete error, and verify repo.Delete was called but its error is not
propagated (only logged). Apply the same additional subtest pattern where the
other test block exists (the similar block around lines 285-309) to cover both
places.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: f6ebaf28-91d8-4501-b0bb-103879876f40
📒 Files selected for processing (3)
internal/core/services/instance.gointernal/core/services/instance_test.gointernal/core/services/instance_unit_test.go
Summary
Fixes database instance records being orphaned in a transient/starting state if the task queue enqueuing fails during instance launch.
Changes
s.repo.Delete) inLaunchInstancewhen enqueuing the provisioning task returns an error.LaunchInstanceWithOptionswhen enqueuing fails.instance_test.gowhererepo.Listcalls were missing thetagFilterparameter.instance_unit_test.goand integration tests ininstance_test.goto cover and verify database cleanup on enqueuing failure.Test Plan
Unit tests added to verify that
repo.Deleteis invoked when task enqueuing fails.Integration test added to verify that the instance is indeed removed from PostgreSQL database repository when the task queue fails.
Unit tests added/updated
Integration tests pass
Manual testing performed
Breaking Changes
None.
Related Issues
Fixes #686
Checklist
make testfor unit tests)Summary by CodeRabbit