-
-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Moved members-migrations out of the background jobs system #30516
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
vershwal
wants to merge
2
commits into
main
Choose a base branch
from
princi-hkg-1983-move-members-migrations-out-of-the-background-jobs-system
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.
+198
−34
Open
Changes from all commits
Commits
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
136 changes: 136 additions & 0 deletions
136
ghost/core/test/e2e-server/services/members-migrations.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| const logging = require('@tryghost/logging'); | ||
| const { agentProvider } = require('../../utils/e2e-framework'); | ||
| const db = require('../../../core/server/data/db'); | ||
| const models = require('../../../core/server/models'); | ||
| const jobsService = require('../../../core/server/services/jobs'); | ||
| const membersService = require('../../../core/server/services/members'); | ||
| const stripeService = require('../../../core/server/services/stripe'); | ||
|
|
||
| const JOB_NAME = 'members-migrations'; | ||
| const STALE = new Date('2020-01-01T00:00:00Z'); | ||
|
|
||
| describe('Members migrations on boot', function () { | ||
| beforeAll(async function () { | ||
| await agentProvider.getAdminAPIAgent(); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('records a finished job row on a fresh boot', async function () { | ||
| const job = await models.Job.findOne({ name: JOB_NAME }); | ||
|
|
||
| assert.ok(job, 'expected a members-migrations row to be written during boot'); | ||
| assert.equal(job.get('status'), 'finished'); | ||
| assert.ok(job.get('started_at') instanceof Date); | ||
| assert.ok(job.get('finished_at') instanceof Date); | ||
| }); | ||
|
|
||
| describe('when the members service initialises again', function () { | ||
| let jobId: string; | ||
|
|
||
| beforeEach(async function () { | ||
| const attrs = { status: 'finished', started_at: STALE, finished_at: STALE }; | ||
| const job = await models.Job.findOne({ name: JOB_NAME }); | ||
| jobId = job | ||
| ? (await models.Job.edit(attrs, { id: job.id })).id | ||
| : (await models.Job.add({ name: JOB_NAME, ...attrs })).id; | ||
| }); | ||
|
|
||
| it('skips the migrations and writes nothing when the row already exists', async function () { | ||
| const execute = sinon.stub(stripeService.migrations, 'execute').resolves(); | ||
| const add = sinon.spy(models.Job, 'add'); | ||
| const edit = sinon.spy(models.Job, 'edit'); | ||
|
|
||
| await membersService.init(); | ||
|
|
||
| sinon.assert.notCalled(execute); | ||
| sinon.assert.notCalled(add); | ||
| sinon.assert.notCalled(edit); | ||
| }); | ||
|
|
||
| it('re-runs the migrations and updates the row in place when the previous run failed', async function () { | ||
| await models.Job.edit({ status: 'failed' }, { id: jobId }); | ||
| const execute = sinon.stub(stripeService.migrations, 'execute').resolves(); | ||
| const add = sinon.spy(models.Job, 'add'); | ||
| const addOneOffJob = sinon.spy(jobsService, 'addOneOffJob'); | ||
| const awaitOneOffCompletion = sinon.spy(jobsService, 'awaitOneOffCompletion'); | ||
|
|
||
| await membersService.init(); | ||
|
|
||
| sinon.assert.calledOnce(execute); | ||
| sinon.assert.notCalled(add); | ||
| sinon.assert.notCalled(addOneOffJob); | ||
| sinon.assert.notCalled(awaitOneOffCompletion); | ||
|
|
||
| const after = await models.Job.findOne({ name: JOB_NAME }); | ||
| assert.equal(after.id, jobId); | ||
| assert.equal(after.get('status'), 'finished'); | ||
| assert.ok(after.get('started_at') > STALE, 'expected started_at to be rewritten'); | ||
| assert.ok(after.get('finished_at') >= after.get('started_at')); | ||
| }); | ||
|
|
||
| it('marks the row failed and keeps booting when the migrations throw', async function () { | ||
| await models.Job.edit({ status: 'failed' }, { id: jobId }); | ||
| sinon.stub(stripeService.migrations, 'execute').rejects(new Error('stripe exploded')); | ||
| const loggingError = sinon.stub(logging, 'error'); | ||
|
|
||
| await membersService.init(); | ||
|
|
||
| sinon.assert.calledWithMatch( | ||
| loggingError, | ||
| sinon.match.has('message', 'stripe exploded'), | ||
| /members-migrations failed after \d+ms/, | ||
| ); | ||
|
|
||
| const after = await models.Job.findOne({ name: JOB_NAME }); | ||
| assert.equal(after.id, jobId); | ||
| assert.equal(after.get('status'), 'failed'); | ||
| assert.ok(after.get('started_at') > STALE, 'expected the failed run to rewrite the row'); | ||
| assert.ok(after.get('finished_at') >= after.get('started_at')); | ||
| }); | ||
|
|
||
| it('keeps booting when another process writes the row first', async function () { | ||
| await db.knex('jobs').where({ name: JOB_NAME }).del(); | ||
| sinon.stub(stripeService.migrations, 'execute').resolves(); | ||
| const add: sinon.SinonStub = sinon.stub(models.Job, 'add').callsFake(async function ( | ||
| this: unknown, | ||
| ...args: unknown[] | ||
| ) { | ||
| // The other process inserts the row first, then our own insert hits the | ||
| // unique index on jobs.name. The runner never inspects the error itself: | ||
| // any failed insert with a row present takes the warn path. | ||
| await add.wrappedMethod.apply(this, args); | ||
| return add.wrappedMethod.apply(this, args); | ||
| }); | ||
| const loggingWarn = sinon.stub(logging, 'warn'); | ||
|
|
||
| await membersService.init(); | ||
|
|
||
| sinon.assert.calledOnce(add); | ||
| sinon.assert.calledWithMatch(loggingWarn, /row was already written by another process/); | ||
|
|
||
| const rows = await db.knex('jobs').where({ name: JOB_NAME }); | ||
| assert.equal(rows.length, 1); | ||
| assert.equal(rows[0].status, 'finished'); | ||
| }); | ||
|
|
||
| it('fails boot when the insert fails and no other process wrote the row', async function () { | ||
| await db.knex('jobs').where({ name: JOB_NAME }).del(); | ||
| const execute = sinon.stub(stripeService.migrations, 'execute').resolves(); | ||
| sinon.stub(models.Job, 'add').rejects(new Error('insert exploded')); | ||
| const loggingWarn = sinon.stub(logging, 'warn'); | ||
|
|
||
| await assert.rejects(membersService.init(), { message: 'insert exploded' }); | ||
|
|
||
| sinon.assert.calledOnce(execute); | ||
| sinon.assert.notCalled(loggingWarn); | ||
| const rows = await db.knex('jobs').where({ name: JOB_NAME }); | ||
| assert.equal(rows.length, 0); | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.