-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Fixed expired complimentary members not triggering webhooks #28444
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
9larsons
wants to merge
1
commit into
main
Choose a base branch
from
codex/model-event-bridge-worker-jobs
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
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
135 changes: 135 additions & 0 deletions
135
ghost/core/core/server/services/jobs/worker-model-event-bridge.js
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,135 @@ | ||
| const moment = require('moment'); | ||
|
|
||
| const MODEL_EVENT_TYPE = 'model-event'; | ||
| const SUPPORTED_MODEL_EVENTS = { | ||
| 'member.edited': { | ||
| model: 'Member' | ||
| } | ||
| }; | ||
|
|
||
| class WorkerModelEventBridge { | ||
| constructor({models, events, logging, sentry}) { | ||
| this.models = models; | ||
| this.events = events; | ||
| this.logging = logging; | ||
| this.sentry = sentry; | ||
| } | ||
|
|
||
| isModelEventMessage(message) { | ||
| return isObject(message) && message.type === MODEL_EVENT_TYPE; | ||
| } | ||
|
|
||
| async handle(message, meta = {}) { | ||
| const validationError = this.validate(message); | ||
|
|
||
| if (validationError) { | ||
| this.logging.warn(`Ignoring invalid worker model event from job ${meta.jobName || 'unknown'}: ${validationError}`); | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| return await this.emitModelEvent(message); | ||
| } catch (err) { | ||
| this.logging.error(err); | ||
| this.sentry.captureException(err); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| validate(message) { | ||
| if (!this.isModelEventMessage(message)) { | ||
| return 'Unexpected message type'; | ||
| } | ||
|
|
||
| const supportedEvent = SUPPORTED_MODEL_EVENTS[message.event]; | ||
|
|
||
| if (!supportedEvent || supportedEvent.model !== message.model) { | ||
| return `Unsupported model event ${message.event || 'unknown'} for model ${message.model || 'unknown'}`; | ||
| } | ||
|
|
||
| if (!message.id || typeof message.id !== 'string') { | ||
| return 'Missing model id'; | ||
| } | ||
|
|
||
| if (!isObject(message.previous)) { | ||
| return 'Missing previous attributes'; | ||
| } | ||
|
|
||
| if (!isObject(message.changed) || !Object.keys(message.changed).length) { | ||
| return 'Missing changed attributes'; | ||
| } | ||
|
|
||
| if (message.options && !isObject(message.options)) { | ||
| return 'Invalid options'; | ||
| } | ||
| } | ||
|
|
||
| async emitModelEvent(message) { | ||
| const Model = this.models[message.model]; | ||
| let model; | ||
|
|
||
| try { | ||
| model = await Model.findOne({ | ||
| id: message.id | ||
| }, { | ||
| require: true, | ||
| context: {internal: true} | ||
| }); | ||
| } catch (err) { | ||
| if (isNotFoundError(err)) { | ||
| this.logging.warn(`Could not emit worker model event ${message.event}: ${message.model} ${message.id} was not found`); | ||
| return false; | ||
| } | ||
|
|
||
| throw err; | ||
| } | ||
|
|
||
| model._previousAttributes = normalizeDates({ | ||
| ...model.attributes, | ||
| ...message.previous | ||
| }); | ||
| model._changed = normalizeDates(message.changed); | ||
|
|
||
| const options = normalizeOptions(message.options); | ||
| this.events.emit(message.event, model, options); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| function normalizeOptions(options = {}) { | ||
| return { | ||
| ...options, | ||
| context: { | ||
| ...options.context, | ||
| internal: true | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function normalizeDates(attributes) { | ||
| const normalized = {...attributes}; | ||
|
|
||
| for (const [key, value] of Object.entries(normalized)) { | ||
| if (key.endsWith('_at') && value && !(value instanceof Date)) { | ||
| normalized[key] = moment.utc(value).toDate(); | ||
| } | ||
| } | ||
|
|
||
| return normalized; | ||
| } | ||
|
|
||
| function isObject(value) { | ||
| return !!value && typeof value === 'object' && !Array.isArray(value); | ||
| } | ||
|
|
||
| function isNotFoundError(err) { | ||
| return err && ( | ||
| err.errorType === 'NotFoundError' || | ||
| err.name === 'NotFoundError' || | ||
| err.message === 'NotFound' || | ||
| err.message === 'EmptyResponse' | ||
| ); | ||
| } | ||
|
|
||
| module.exports = WorkerModelEventBridge; | ||
| module.exports.MODEL_EVENT_TYPE = MODEL_EVENT_TYPE; | ||
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
101 changes: 101 additions & 0 deletions
101
ghost/core/test/unit/server/services/jobs/job-service.test.js
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,101 @@ | ||
| const assert = require('node:assert/strict'); | ||
| const Module = require('module'); | ||
| const sinon = require('sinon'); | ||
|
|
||
| describe('JobService', function () { | ||
| const jobServicePath = '../../../../../core/server/services/jobs/job-service'; | ||
| let originalLoad; | ||
| let workerMessageHandler; | ||
| let handleModelEvent; | ||
|
|
||
| beforeEach(function () { | ||
| originalLoad = Module._load; | ||
| handleModelEvent = sinon.stub().resolves(true); | ||
|
|
||
| Module._load = function (request, parent, isMain) { | ||
| if (request === '@tryghost/job-manager') { | ||
| return class JobManager { | ||
| constructor(options) { | ||
| workerMessageHandler = options.workerMessageHandler; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| if (request === './worker-model-event-bridge') { | ||
| return class WorkerModelEventBridge { | ||
| isModelEventMessage(message) { | ||
| return message && message.type === 'model-event'; | ||
| } | ||
|
|
||
| handle(message, meta) { | ||
| return handleModelEvent(message, meta); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| if (request === '@tryghost/logging') { | ||
| return { | ||
| info: sinon.stub(), | ||
| warn: sinon.stub(), | ||
| error: sinon.stub() | ||
| }; | ||
| } | ||
|
|
||
| if (request === '../../models') { | ||
| return {Job: {}}; | ||
| } | ||
|
|
||
| if (request === '../../../shared/sentry') { | ||
| return {captureException: sinon.stub()}; | ||
| } | ||
|
|
||
| if (request === '@tryghost/domain-events') { | ||
| return {}; | ||
| } | ||
|
|
||
| if (request === '../../../shared/config') { | ||
| return {}; | ||
| } | ||
|
|
||
| if (request === '../../lib/common/events') { | ||
| return {emit: sinon.stub()}; | ||
| } | ||
|
|
||
| return originalLoad.call(this, request, parent, isMain); | ||
| }; | ||
|
|
||
| delete require.cache[require.resolve(jobServicePath)]; | ||
| require(jobServicePath); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| Module._load = originalLoad; | ||
| delete require.cache[require.resolve(jobServicePath)]; | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('routes model-event worker messages without leaving them as raw domain events', function () { | ||
| const message = { | ||
| type: 'model-event', | ||
| event: 'member.edited', | ||
| model: 'Member', | ||
| id: 'member-id', | ||
| previous: {status: 'comped'}, | ||
| changed: {status: 'free'} | ||
| }; | ||
|
|
||
| workerMessageHandler({name: 'clean-expired-comped', message}); | ||
|
|
||
| sinon.assert.calledOnceWithExactly(handleModelEvent, { | ||
| type: 'model-event', | ||
| event: 'member.edited', | ||
| model: 'Member', | ||
| id: 'member-id', | ||
| previous: {status: 'comped'}, | ||
| changed: {status: 'free'} | ||
| }, { | ||
| jobName: 'clean-expired-comped' | ||
| }); | ||
| assert.equal(message.event, undefined); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
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.
Reject
options: nullduring validation to avoid false handler-error reportingLine 62 uses a truthy guard, so
options: nullbypasses validation and later throws at Line 103 when readingoptions.context, which gets reported as an internal handler error instead of an invalid payload.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents