-
Notifications
You must be signed in to change notification settings - Fork 1
feat(release-worker): assign sequence to new releases #586
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
base: master
Are you sure you want to change the base?
Changes from all commits
9d6ede7
0b35f82
cb1538d
7fe5034
d72ac69
17dccbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,11 +89,21 @@ export default class ReleaseWorker extends Worker { | |
| this.logger.info(`saveRelease: save release for project: ${projectId}, release: ${payload.release}`); | ||
| try { | ||
| const commits = payload.commits; | ||
| const validCommits = !!commits && this.areCommitsValid(commits); | ||
| const hasFiles = Array.isArray(payload.files) && payload.files.length > 0; | ||
|
|
||
| if (!validCommits && !hasFiles) { | ||
| this.logger.debug(`Skipping release ${payload.release} for project ${projectId}: no valid commits or source maps`); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| await this.createReleaseIfMissing(projectId, payload.release); | ||
|
|
||
| /** | ||
| * Save commits | ||
| */ | ||
| if (commits && this.areCommitsValid(commits)) { | ||
| if (validCommits) { | ||
| const commitsWithParsedDate: CommitData[] = commits.map(commit => ({ | ||
| ...commit, | ||
| date: new Date(commit.date), | ||
|
|
@@ -106,13 +116,11 @@ export default class ReleaseWorker extends Worker { | |
| $set: { | ||
| commits: commitsWithParsedDate, | ||
| }, | ||
| }, { | ||
| upsert: true, | ||
| }); | ||
| } | ||
|
|
||
| // save source maps | ||
| if (payload.files) { | ||
| if (hasFiles) { | ||
| await this.saveSourceMap(projectId, payload); | ||
| } | ||
| } catch (err) { | ||
|
|
@@ -122,6 +130,54 @@ export default class ReleaseWorker extends Worker { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a release once and assign its first-registration sequence. | ||
| * | ||
| * The sequence starts at 1 for each project and is based on the order in | ||
| * which new releases are received by this sequential worker. Repeated | ||
| * commits or source-map uploads keep the existing sequence. | ||
| * | ||
| * @param projectId - project id to bind the corresponding release. | ||
| * @param release - release name | ||
| */ | ||
| private async createReleaseIfMissing(projectId: string, release: string): Promise<void> { | ||
| const existingRelease = await this.releasesCollection.findOne({ | ||
| projectId, | ||
| release, | ||
| }); | ||
|
|
||
| if (existingRelease) { | ||
| return; | ||
| } | ||
|
|
||
| const lastRelease = await this.releasesCollection.findOne({ | ||
| projectId, | ||
| releaseSequence: { $exists: true }, | ||
| }, { | ||
| sort: { releaseSequence: -1 }, | ||
| projection: { releaseSequence: 1 }, | ||
| }); | ||
|
|
||
| const releaseSequence = (lastRelease?.releaseSequence || 0) + 1; | ||
|
Member
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. I'm not sure it will work properly in case of several workers running in parallel.
Author
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. You are right. This code is safe only with the current deployment setup: one release worker with SIMULTANEOUS_TASKS=1. With multiple workers, two new releases could read the same last sequence and get the same number. I kept this approach to avoid adding another MongoDB collection. Should we keep this limitation, or should we add a small releaseSequenceCounters collection and use an atomic |
||
|
|
||
| try { | ||
| await this.releasesCollection.insertOne({ | ||
| projectId, | ||
| release, | ||
| releaseSequence, | ||
| commits: [], | ||
| } as unknown as ReleaseDBScheme); | ||
| } catch (error) { | ||
| if ((error as MongoError).code?.toString() === DB_DUPLICATE_KEY_ERROR) { | ||
| this.logger.debug(`Release ${release} for project ${projectId} was created by another worker`); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check commtis for the content of all required data | ||
| * | ||
|
|
@@ -208,31 +264,8 @@ export default class ReleaseWorker extends Worker { | |
|
|
||
| try { | ||
| /** | ||
| * - insert new record with saved maps | ||
| * or | ||
| * - update previous record with adding new saved maps | ||
| * Add new source maps to the release created by saveRelease. | ||
| */ | ||
| if (!existedRelease) { | ||
|
Member
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. why this code has been removed?
Author
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. This code was moved to createReleaseIfMissing, which runs before saveSourceMap. So saveSourceMap only updates an existing release now. |
||
| this.logger.info('trying insert new release'); | ||
|
|
||
| try { | ||
| await this.releasesCollection.insertOne({ | ||
| projectId: projectId, | ||
| release: payload.release, | ||
| files: savedFilesWithoutContent, | ||
| } as ReleaseDBScheme); | ||
| this.logger.info('inserted new release'); | ||
| } catch (err) { | ||
| if ((err as MongoError).code.toString() === DB_DUPLICATE_KEY_ERROR) { | ||
| this.logger.warn(`Duplicate key on insert, retrying update after small delay`); | ||
| /* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
| await new Promise(resolve => setTimeout(resolve, 200)); | ||
| } else { | ||
| throw err; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| await this.releasesCollection.findOneAndUpdate({ | ||
| projectId: projectId, | ||
| release: payload.release, | ||
|
|
||
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.
add debug log about release skipping with project id