-
Notifications
You must be signed in to change notification settings - Fork 28
Fix(form-cleanup): improve token cleanup with batching and absolute time handling #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
se348
wants to merge
3
commits into
vula-africa:main
Choose a base branch
from
se348:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,84 +1,86 @@ | ||
| /* Context: | ||
| This is a scheduled job that runs every day at midnight to clean up forms that users started filling in but didn't submit which are older than 7 days. | ||
| When a user visits a public form, a token is generated and stored in the database. | ||
| This token is used to identify the user and link the answers to the entity. | ||
| An entity is the owner of data in the database, separated as it could be a business or an individual but has been decoupled from a login/user. | ||
| If the user does not submit the form, the token and the entity should be deleted after 7 days. | ||
| This is to prevent the database from being cluttered with unused tokens and entities. | ||
| */ | ||
|
|
||
| /* Task Instructions: | ||
| * 1. Read and understand the code below | ||
| * 2. Identify ALL issues in the code (there are multiple) | ||
| * 3. Fix the issues and create a working solution | ||
| * 4. Create a PR with clear commit messages | ||
| * 5. Record a 3-5 minute Loom video explaining: | ||
| * - What issues you found | ||
| * - How you fixed them | ||
| * - Any trade-offs you considered | ||
| * | ||
| * Focus on: correctness, performance, error handling, and code clarity | ||
| * Expected time: 45-60 minutes | ||
| */ | ||
|
|
||
| // For the purpose of this test you can ignore that the imports are not working. | ||
| import type { JobScheduleQueue } from "@prisma/client"; | ||
| import { prisma } from "../endpoints/middleware/prisma"; | ||
| import { update_job_status } from "./generic_scheduler"; | ||
| import { update_job_status } from "./generic_scheduler"; | ||
|
|
||
| type RelationshipStatus = "new" | "submitted"; | ||
| type JobStatus = "completed" | "failed"; | ||
|
|
||
| // Number of records to process in one batch. | ||
| // This value can be fine-tuned depending on: | ||
| // - database size | ||
| // - indexes available | ||
| // - workload characteristics | ||
| // Larger batches = fewer transactions but more memory/lock contention. | ||
| // Smaller batches = safer but potentially slower overall. | ||
| // Reasoning behind it is in my pr description. | ||
| const BATCH_SIZE = 20 | ||
| const EXPIRATION_DAYS = 7 | ||
|
|
||
| /** | ||
| * Cleans up expired unsubmitted public form tokens and all associated data. | ||
| * | ||
| * This scheduled job runs in batches and performs deletions: | ||
| * 1. Deletes all corpus items linked to the entities | ||
| * 2. Deletes all relationships linked to the batch of tokens | ||
| * 3. Deletes all expired tokens in the batch | ||
| * 4. Deletes all associated entities | ||
| * | ||
| * The function uses absolute date calculation (midnight 7 days ago) to avoid missing tokens | ||
| * if the job is delayed or re-queued. Batches reduce database load by minimizing repeated | ||
| * opening and closing of connections, while still ensuring correctness. | ||
| * | ||
| * @param {JobScheduleQueue} job - The scheduled job object containing job metadata. | ||
| * @throws Will throw an error if any database operation fails, updating the job status to 'failed'. | ||
| */ | ||
| export const cleanup_unsubmitted_forms = async (job: JobScheduleQueue) => { | ||
| try { | ||
| //Find forms that were created 7 days ago and have not been submitted | ||
| const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60); | ||
| const sevenDaysAgoPlusOneDay = new Date( | ||
| sevenDaysAgo.getTime() + 24 * 60 * 60 * 1000 | ||
| ); | ||
| // Calculate absolute EXPIRATION_DAYS day-old date (midnight EXPIRATION_DAYS days ago) | ||
| const today = new Date(); | ||
| today.setHours(0, 0, 0, 0); | ||
| const sevenDaysAgo = new Date(today.getTime() - EXPIRATION_DAYS * 24 * 60 * 60 * 1000); | ||
|
|
||
|
Comment on lines
+37
to
41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UTC cutoff to avoid local-time skew; rename to cutoff.
Apply: - const today = new Date();
- today.setHours(0, 0, 0, 0);
- const sevenDaysAgo = new Date(today.getTime() - EXPIRATION_DAYS * 24 * 60 * 60 * 1000);
+ const cutoff = new Date();
+ cutoff.setUTCHours(0, 0, 0, 0);
+ cutoff.setUTCDate(cutoff.getUTCDate() - EXPIRATION_DAYS);And use it in the query: - where: { createdAt: { lt: sevenDaysAgo } },
+ where: { createdAt: { lt: cutoff } },Also applies to: 44-44 |
||
| const expiredTokens = await prisma.publicFormsTokens.findMany({ | ||
| where: { | ||
| createdAt: { | ||
| gte: sevenDaysAgo, // greater than or equal to 7 days ago | ||
| lt: sevenDaysAgoPlusOneDay, // but less than 7 days ago + 1 day | ||
| while (true) { | ||
| const expiredTokens = await prisma.publicFormsTokens.findMany({ | ||
| where: { createdAt: { lt: sevenDaysAgo } }, | ||
| select: { | ||
| entityId: true, | ||
| token: true, | ||
| productId: true, | ||
| }, | ||
| }, | ||
| }); | ||
| take: BATCH_SIZE, | ||
| }); | ||
|
|
||
| for (const token of expiredTokens) { | ||
| const relationship = await prisma.relationship.findFirst({ | ||
| if (expiredTokens.length === 0) break; // exit when no more expired tokens | ||
|
|
||
| const entityIds = expiredTokens | ||
| .map((t) => t.entityId) | ||
| .filter(Boolean) as string[]; | ||
| const tokenValues = expiredTokens.map((t) => t.token); | ||
|
|
||
| const relationships = await prisma.relationship.findMany({ | ||
| where: { | ||
| product_id: token.productId, | ||
| status: "new", | ||
| product_id: { in: expiredTokens.map((t) => t.productId) }, | ||
| status: "new" as RelationshipStatus, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (relationship) { | ||
| await prisma.$transaction([ | ||
| // Delete relationship | ||
| prisma.relationship.delete({ | ||
| where: { id: relationship.id }, | ||
| }), | ||
| // // Delete the token | ||
| prisma.publicFormsTokens.delete({ | ||
| where: { token: token.token }, | ||
| }), | ||
| // Delete all corpus items associated with the entity | ||
| prisma.new_corpus.deleteMany({ | ||
| where: { | ||
| entity_id: token.entityId || "", | ||
| }, | ||
| }), | ||
| // Delete the entity (company) | ||
| prisma.entity.delete({ | ||
| where: { id: token.entityId || "" }, | ||
| }), | ||
| ]); | ||
| } | ||
| await prisma.$transaction([ | ||
| prisma.new_corpus.deleteMany({ where: { entity_id: { in: entityIds } } }), | ||
| prisma.relationship.deleteMany({ where: { id: { in: relationships.map((r) => r.id) } } }), | ||
| prisma.publicFormsTokens.deleteMany({ where: { token: { in: tokenValues } } }), | ||
| prisma.entity.deleteMany({ where: { id: { in: entityIds } } }), | ||
| ]); | ||
| } | ||
|
|
||
| await update_job_status(job.id, "completed"); | ||
| await update_job_status(job.id, "completed" as JobStatus); | ||
| } catch (error) { | ||
| // TODO: This is not a good way to loggging. | ||
| // We should use loggers like winston. Check my pr description for more. | ||
| console.error("Error cleaning up unsubmitted forms:", error); | ||
| await update_job_status(job.id, "failed"); | ||
| await update_job_status(job.id, "failed" as JobStatus); | ||
| throw error; | ||
| } | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify import paths resolve from test location.
From tests/, check that
../endpoints/middleware/prismaand./generic_schedulerexist and export the expected symbols.Run:
🏁 Script executed:
Length of output: 70
🏁 Script executed:
Length of output: 2022
Fix broken imports in tests/unsubmitted_forms/cleanup_unsubmitted_forms.ts
Lines 2–3: the paths
../endpoints/middleware/prismaand./generic_schedulerdon’t exist—update these to the correct relative locations of the modules that exportprismaandupdate_job_statusso the imports resolve.🤖 Prompt for AI Agents