fix: preserve dakao result subjects on outbox retry
This commit is contained in:
1 parent
e63fba0286
commit
7b636ee793
3 files changed
+145
-1
No files matched your search
@@ -0,0 +1,116 @@
|
|||||||
|
import { ConnectionService } from '../global/database/connection.service';
|
||||||
|
import { NatsService } from '../global/nats/nats.service';
|
||||||
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
||||||
|
import { DakaoResultSubjects } from './dakao.types';
|
||||||
|
|
||||||
|
function execResult<T>(value: T) {
|
||||||
|
return { exec: jest.fn().mockResolvedValue(value) };
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeService(outboxModel: Record<string, unknown>) {
|
||||||
|
const connectionService = {
|
||||||
|
getModel: jest.fn().mockResolvedValue(outboxModel),
|
||||||
|
} as unknown as ConnectionService;
|
||||||
|
const natsService = {
|
||||||
|
publishJetStream: jest.fn().mockResolvedValue(undefined),
|
||||||
|
} as unknown as NatsService;
|
||||||
|
|
||||||
|
return {
|
||||||
|
service: new DakaoOutboxService(connectionService, natsService),
|
||||||
|
connectionService,
|
||||||
|
natsService,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DakaoOutboxService', () => {
|
||||||
|
it('persists result subjects when enqueuing send-message commands', async () => {
|
||||||
|
const outboxModel = {
|
||||||
|
findOneAndUpdate: jest.fn().mockReturnValue(execResult({})),
|
||||||
|
updateOne: jest.fn().mockReturnValue(execResult({})),
|
||||||
|
};
|
||||||
|
const { service, natsService } = makeService(outboxModel);
|
||||||
|
const resultSubjects: DakaoResultSubjects = {
|
||||||
|
succeeded: 'staging.lolympus.dakao.chat.send-message.succeeded.v1',
|
||||||
|
failed: 'staging.lolympus.dakao.chat.send-message.failed.v1',
|
||||||
|
};
|
||||||
|
|
||||||
|
await service.enqueueAndPublish({
|
||||||
|
subject: 'staging.dakao.chat.send-message.v1',
|
||||||
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
||||||
|
envelope: {
|
||||||
|
schema: 'dakao.command.v1',
|
||||||
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
||||||
|
type: 'chat.send-message',
|
||||||
|
eventSource: 'lolympus',
|
||||||
|
correlationId: 'chat-log-1',
|
||||||
|
resultSubjects,
|
||||||
|
payload: {
|
||||||
|
chatroomId: '18474390111480678',
|
||||||
|
segments: [{ kind: 'text', text: 'done' }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(outboxModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||||
|
{ eventId: 'lolympus:chat.send-message:chat-log-1' },
|
||||||
|
expect.objectContaining({
|
||||||
|
$setOnInsert: expect.objectContaining({ resultSubjects }),
|
||||||
|
}),
|
||||||
|
expect.any(Object),
|
||||||
|
);
|
||||||
|
expect(natsService.publishJetStream).toHaveBeenCalledWith(
|
||||||
|
'staging.dakao.chat.send-message.v1',
|
||||||
|
expect.objectContaining({ resultSubjects }),
|
||||||
|
{ messageId: 'lolympus:chat.send-message:chat-log-1' },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('restores send-message result subjects when sweeping legacy stale rows', async () => {
|
||||||
|
const previousNodeEnv = process.env.NODE_ENV;
|
||||||
|
process.env.NODE_ENV = 'staging';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const exec = jest.fn().mockResolvedValue([
|
||||||
|
{
|
||||||
|
eventId: 'lolympus:chat.send-message:chat-log-1',
|
||||||
|
type: 'chat.send-message',
|
||||||
|
eventSource: 'lolympus',
|
||||||
|
subject: 'staging.dakao.chat.send-message.v1',
|
||||||
|
correlationId: 'chat-log-1',
|
||||||
|
payload: {
|
||||||
|
chatroomId: '18474390111480678',
|
||||||
|
segments: [{ kind: 'text', text: 'done' }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
const lean = jest.fn().mockReturnValue({ exec });
|
||||||
|
const limit = jest.fn().mockReturnValue({ lean });
|
||||||
|
const sort = jest.fn().mockReturnValue({ limit });
|
||||||
|
const outboxModel = {
|
||||||
|
find: jest.fn().mockReturnValue({ sort }),
|
||||||
|
};
|
||||||
|
const { service } = makeService(outboxModel);
|
||||||
|
const enqueue = jest
|
||||||
|
.spyOn(service, 'enqueueAndPublish')
|
||||||
|
.mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
await expect(service.sweepStale({ staleMs: 1, limit: 1 })).resolves.toBe(
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(enqueue).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
envelope: expect.objectContaining({
|
||||||
|
resultSubjects: {
|
||||||
|
succeeded:
|
||||||
|
'staging.lolympus.dakao.chat.send-message.succeeded.v1',
|
||||||
|
failed: 'staging.lolympus.dakao.chat.send-message.failed.v1',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
process.env.NODE_ENV = previousNodeEnv;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,13 +1,23 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { ConnectionService } from '../global/database/connection.service';
|
import { ConnectionService } from '../global/database/connection.service';
|
||||||
import { NatsService } from '../global/nats/nats.service';
|
import { NatsService } from '../global/nats/nats.service';
|
||||||
import { DakaoCommandEnvelope, DakaoResultEnvelope } from './dakao.types';
|
import { resolveDakaoNatsNames } from './dakao.nats-names';
|
||||||
|
import {
|
||||||
|
DakaoCommandEnvelope,
|
||||||
|
DakaoResultEnvelope,
|
||||||
|
DakaoResultSubjects,
|
||||||
|
} from './dakao.types';
|
||||||
|
|
||||||
|
const CHAT_SEND_MESSAGE_TYPE = 'chat.send-message';
|
||||||
const TERMINAL_STATES = ['sent', 'failed'] as const;
|
const TERMINAL_STATES = ['sent', 'failed'] as const;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DakaoOutboxService {
|
export class DakaoOutboxService {
|
||||||
private readonly logger = new Logger(DakaoOutboxService.name);
|
private readonly logger = new Logger(DakaoOutboxService.name);
|
||||||
|
private readonly chatSendMessageResultSubjects = {
|
||||||
|
succeeded: resolveDakaoNatsNames().subjects.chatSendMessageSucceeded,
|
||||||
|
failed: resolveDakaoNatsNames().subjects.chatSendMessageFailed,
|
||||||
|
};
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly connectionService: ConnectionService,
|
private readonly connectionService: ConnectionService,
|
||||||
@@ -30,6 +40,7 @@ export class DakaoOutboxService {
|
|||||||
type: params.envelope.type,
|
type: params.envelope.type,
|
||||||
subject: params.subject,
|
subject: params.subject,
|
||||||
correlationId: params.envelope.correlationId,
|
correlationId: params.envelope.correlationId,
|
||||||
|
resultSubjects: params.envelope.resultSubjects,
|
||||||
payload: params.envelope.payload as Record<string, unknown>,
|
payload: params.envelope.payload as Record<string, unknown>,
|
||||||
state: 'pending',
|
state: 'pending',
|
||||||
publishAttempts: 0,
|
publishAttempts: 0,
|
||||||
@@ -161,6 +172,8 @@ export class DakaoOutboxService {
|
|||||||
type: row.type,
|
type: row.type,
|
||||||
eventSource: row.eventSource,
|
eventSource: row.eventSource,
|
||||||
correlationId: row.correlationId,
|
correlationId: row.correlationId,
|
||||||
|
resultSubjects:
|
||||||
|
row.resultSubjects ?? this.resolveDefaultResultSubjects(row.type),
|
||||||
payload: row.payload,
|
payload: row.payload,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -168,4 +181,11 @@ export class DakaoOutboxService {
|
|||||||
|
|
||||||
return rows.length;
|
return rows.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private resolveDefaultResultSubjects(
|
||||||
|
type: string,
|
||||||
|
): DakaoResultSubjects | undefined {
|
||||||
|
if (type !== CHAT_SEND_MESSAGE_TYPE) return undefined;
|
||||||
|
return this.chatSendMessageResultSubjects;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -350,6 +350,10 @@ export interface DakaoOutboxMessage extends MongooseTimestamps {
|
|||||||
type: string;
|
type: string;
|
||||||
subject: string;
|
subject: string;
|
||||||
correlationId?: string;
|
correlationId?: string;
|
||||||
|
resultSubjects?: {
|
||||||
|
succeeded: string;
|
||||||
|
failed: string;
|
||||||
|
};
|
||||||
payload: Record<string, unknown>;
|
payload: Record<string, unknown>;
|
||||||
state: DakaoOutboxMessageState;
|
state: DakaoOutboxMessageState;
|
||||||
publishAttempts: number;
|
publishAttempts: number;
|
||||||
@@ -960,6 +964,10 @@ export const DakaoOutboxMessageSchema = new Schema<DakaoOutboxMessage>(
|
|||||||
type: { type: String, required: true },
|
type: { type: String, required: true },
|
||||||
subject: { type: String, required: true },
|
subject: { type: String, required: true },
|
||||||
correlationId: { type: String },
|
correlationId: { type: String },
|
||||||
|
resultSubjects: {
|
||||||
|
succeeded: { type: String },
|
||||||
|
failed: { type: String },
|
||||||
|
},
|
||||||
payload: { type: Schema.Types.Mixed, required: true },
|
payload: { type: Schema.Types.Mixed, required: true },
|
||||||
state: {
|
state: {
|
||||||
type: String,
|
type: String,
|
||||||
|
|||||||
Reference in new issue
Block a user