feat(seo): atomic directive fencing and transactional blog publication
Deploy Canina / deploy (push) Successful in 4m45s
Deploy Canina / deploy (push) Successful in 4m45s
This commit is contained in:
1 parent
0a156fd7b4
commit
fb1deadbbc
10 files changed
+353
-187
No files matched your search
@@ -46,6 +46,12 @@ export class ActionFindingDto {
|
||||
@IsOptional() @IsString() @MaxLength(128) actionId?: string;
|
||||
@IsOptional() @IsEnum(ResearchFindingStatus) status?: ResearchFindingStatus;
|
||||
}
|
||||
export class PublishFindingDto {
|
||||
@IsInt() @Min(0) expectedVersion: number;
|
||||
@IsInt() @Min(0) feedbackVersion: number;
|
||||
@IsString() @MinLength(1) @MaxLength(128) actionId: string;
|
||||
@IsObject() blog: Record<string, any>;
|
||||
}
|
||||
export class QueryFindingsDto {
|
||||
@IsOptional() @Type(() => Number) @IsInt() @Min(1) @Max(100000) page?: number;
|
||||
@IsOptional() @Type(() => Number) @IsInt() @Min(1) @Max(100) limit?: number;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { BlogsService } from '../admin/blogs.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { RevalidationService } from '../common/revalidation/revalidation.service';
|
||||
|
||||
/** Reuse the admin create rules with the transaction client. Never run network
|
||||
* revalidation (or the scheduler lifecycle) before the outer commit. */
|
||||
export async function createPublicationBlog(
|
||||
tx: Prisma.TransactionClient, data: Record<string, any>, userId: string,
|
||||
) {
|
||||
const deferred = { revalidateBlog: async () => false } as unknown as RevalidationService;
|
||||
const service = new BlogsService(tx as PrismaService, deferred);
|
||||
const { cacheRevalidation: _deferred, ...blog } = await service.createBlog(data, userId);
|
||||
return blog;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { PassportModule, PassportStrategy } from '@nestjs/passport';
|
||||
import { Strategy, ExtractJwt } from 'passport-jwt';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { spawn } from 'child_process';
|
||||
import { resolve } from 'path';
|
||||
import { SeoTrackingModule } from './seo-tracking.module';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { RevalidationService } from '../common/revalidation/revalidation.service';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { SeoTrackingService } from './seo-tracking.service';
|
||||
|
||||
class PublicationStrategy extends PassportStrategy(Strategy) {
|
||||
constructor() { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: 'publication-disposable-only' }); }
|
||||
validate(payload: any) { return payload; }
|
||||
}
|
||||
const url = process.env.PUBLICATION_TEST_DATABASE_URL;
|
||||
if (url && !url.startsWith('postgresql://publication_test:disposable-only@127.0.0.1:55447/'))
|
||||
throw new Error('Disposable publication database only');
|
||||
const deferred = () => { let resolve!: () => void; const promise = new Promise<void>(r => resolve = r); return { promise, resolve }; };
|
||||
(url ? describe : describe.skip)('atomic publication: real PostgreSQL row ordering', () => {
|
||||
const db = new PrismaClient({ datasources: { db: { url } } });
|
||||
let user: any;
|
||||
const cache = { revalidateBlog: jest.fn(async () => false) };
|
||||
const service = new SeoTrackingService(db as any, cache as any);
|
||||
const ids: string[] = [];
|
||||
it('dormant Python entrypoint uses authenticated real HTTP publication endpoint', async () => {
|
||||
const module = await Test.createTestingModule({ imports: [PassportModule, SeoTrackingModule], providers: [PublicationStrategy] })
|
||||
.overrideProvider(PrismaService).useValue(db).compile();
|
||||
const app = module.createNestApplication({ logger: false }); app.setGlobalPrefix('api');
|
||||
try {
|
||||
await app.listen(0, '127.0.0.1');
|
||||
const jwt = new JwtService({ secret: 'publication-disposable-only' });
|
||||
const token = jwt.sign({ id: user.id, role: 'admin' }, { expiresIn: '5m' });
|
||||
const origin = `http://127.0.0.1:${app.getHttpServer().address().port}`;
|
||||
const child = spawn('python3', [resolve(__dirname, '../../../scripts/content-research/verify_real_http.py')], { env: { ...process.env, PUBLICATION_LOCAL_ORIGIN: origin, PUBLICATION_LOCAL_TOKEN: token } });
|
||||
let output = ''; child.stdout.on('data', d => output += d); child.stderr.on('data', d => output += d);
|
||||
const code = await new Promise(resolve => child.on('close', resolve));
|
||||
expect({ code, output }).toEqual({ code: 0, output: 'PUBLICATION_REAL_HTTP_PASSED\n' });
|
||||
const fixtures = await db.contentResearchFinding.findMany({ where: { title: 'publication-http-fixture' } });
|
||||
ids.push(...fixtures.map(f => f.id));
|
||||
} finally { await app.close(); }
|
||||
}, 30000);
|
||||
beforeAll(async () => { user = await db.user.create({ data: { firstName: 'Test', lastName: 'Fixture', mobile: String(Date.now()), role: 'admin' } }); });
|
||||
afterAll(async () => {
|
||||
await db.contentResearchFinding.deleteMany({ where: { id: { in: ids } } });
|
||||
await db.blog.deleteMany({ where: { slug: { startsWith: 'publication-fixture-' } } });
|
||||
await db.user.delete({ where: { id: user.id } }); await db.$disconnect();
|
||||
});
|
||||
async function finding() {
|
||||
const f = await service.createFinding({ title: 'fixture', hypothesis: 'fixture', targetKeywords: ['test'], primaryKeyword: 'test', expectedOutcome: 'fixture' });
|
||||
ids.push(f.id); return f;
|
||||
}
|
||||
const payload = () => ({ title: 'fixture', slug: 'publication-fixture-' + Math.random(), content: '<p>reviewed fixture</p>', tagNames: ['publication-fixture-tag'] });
|
||||
function barrierClient(entered: ReturnType<typeof deferred>, release: ReturnType<typeof deferred>, pids: number[]) {
|
||||
return new Proxy(db, { get(target, prop) {
|
||||
if (prop === '$transaction') return (fn: any) => db.$transaction(async tx => {
|
||||
pids.push((await tx.$queryRaw<any[]>`SELECT pg_backend_pid() AS pid`)[0].pid);
|
||||
return fn(new Proxy(tx, { get(t, key) {
|
||||
if (key === 'blog') return new Proxy(t.blog, { get(model, method) {
|
||||
if (method === 'create') return async (args: any) => { entered.resolve(); await release.promise; return model.create(args); };
|
||||
return (model as any)[method];
|
||||
} });
|
||||
return (t as any)[key];
|
||||
} }));
|
||||
}, { timeout: 10000 });
|
||||
return (target as any)[prop];
|
||||
} });
|
||||
}
|
||||
it('feedback committed during draft/preflight rejects publication and creates no Blog', async () => {
|
||||
const f = await finding(), blog = payload();
|
||||
await service.updateCeoFeedback(f.id, { expectedVersion: f.version, feedback: 'changed during preflight' });
|
||||
await expect(service.publishFinding(f.id, { expectedVersion: f.version, feedbackVersion: f.feedbackVersion, actionId: 'stale', blog }, user.id)).rejects.toMatchObject({ status: 409 });
|
||||
expect(await db.blog.count({ where: { slug: blog.slug } })).toBe(0);
|
||||
});
|
||||
it('publication lock blocks concurrent feedback; stale writer conflicts after commit; fresh feedback applies next', async () => {
|
||||
const f = await finding(), entered = deferred(), release = deferred(), pids: number[] = [];
|
||||
const publishing = new SeoTrackingService(barrierClient(entered, release, pids) as any, cache as any);
|
||||
const publish = publishing.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'winner', blog: payload() }, user.id);
|
||||
await entered.promise;
|
||||
let settled = false;
|
||||
// Separate Prisma connection/transaction; actual row lock wait proved with pg_stat_activity.
|
||||
const feedback = service.updateCeoFeedback(f.id, { expectedVersion: 0, feedback: 'concurrent' }).then(v => { settled = true; return v; }, e => { settled = true; return e; });
|
||||
let waiting = false;
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const rows = await db.$queryRaw<any[]>`SELECT pid FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND pid <> ${pids[0]}`;
|
||||
if (rows.length) { waiting = true; expect(rows[0].pid).not.toBe(pids[0]); break; }
|
||||
await new Promise(r => setTimeout(r, 10));
|
||||
}
|
||||
expect(waiting).toBe(true); expect(settled).toBe(false); release.resolve();
|
||||
const result = await publish; expect((await feedback).status).toBe(409);
|
||||
expect(result.finding.blogId).toBe(result.blog.id); expect(result.consumedFeedbackVersion).toBe(0);
|
||||
const next = await service.updateCeoFeedback(f.id, { expectedVersion: result.finding.version, feedback: 'next action' });
|
||||
expect(next.feedbackVersion).toBe(1); expect(next.blogId).toBe(result.blog.id);
|
||||
});
|
||||
it('invalid author/relation rolls back both claim and Blog; duplicate request cannot create twice', async () => {
|
||||
const f = await finding(), blog = payload();
|
||||
await expect(service.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'rollback', blog }, '00000000-0000-4000-8000-000000000000')).rejects.toBeTruthy();
|
||||
expect((await service.findOne(f.id)).version).toBe(0);
|
||||
expect(await db.blog.count({ where: { slug: blog.slug } })).toBe(0);
|
||||
const r = await service.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'once', blog }, user.id);
|
||||
await expect(service.publishFinding(f.id, { expectedVersion: 0, feedbackVersion: 0, actionId: 'once', blog }, user.id)).rejects.toMatchObject({ status: 409 });
|
||||
expect(await db.blog.count({ where: { slug: blog.slug } })).toBe(1);
|
||||
expect(r.cacheRevalidation).toBe('failed');
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
Controller,
|
||||
Request,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
@@ -14,6 +15,7 @@ import {
|
||||
import { SeoTrackingService } from './seo-tracking.service';
|
||||
import {
|
||||
ActionFindingDto,
|
||||
PublishFindingDto,
|
||||
CreateFindingDto,
|
||||
QueryFindingsDto,
|
||||
UpdateCeoFeedbackDto,
|
||||
@@ -67,6 +69,15 @@ export class SeoTrackingController {
|
||||
return this.seoTrackingService.actionFinding(id, dto);
|
||||
}
|
||||
|
||||
@Post(':id/publication')
|
||||
async publishFinding(
|
||||
@Param('id', new ParseUUIDPipe()) id: string,
|
||||
@Body() dto: PublishFindingDto,
|
||||
@Request() req: { user: { id: string } },
|
||||
) {
|
||||
return this.seoTrackingService.publishFinding(id, dto, req.user.id);
|
||||
}
|
||||
|
||||
@Post(':id/snapshots')
|
||||
async addSnapshot(
|
||||
@Param('id', new ParseUUIDPipe()) id: string,
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import {
|
||||
Injectable,
|
||||
Optional,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
} from '@nestjs/common';
|
||||
import { createPublicationBlog } from './publication-blog.adapter';
|
||||
import { RevalidationService } from '../common/revalidation/revalidation.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import {
|
||||
CreateFindingDto,
|
||||
@@ -11,6 +14,7 @@ import {
|
||||
UpdateCeoFeedbackDto,
|
||||
CreateSnapshotDto,
|
||||
ActionFindingDto,
|
||||
PublishFindingDto,
|
||||
} from './dto/seo-tracking.dto';
|
||||
import {
|
||||
createPaginationMeta,
|
||||
@@ -37,7 +41,10 @@ function checked<T extends object>(type: new () => T, value: T): T {
|
||||
}
|
||||
@Injectable()
|
||||
export class SeoTrackingService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
@Optional() private revalidation?: RevalidationService,
|
||||
) {}
|
||||
async createFinding(input: CreateFindingDto) {
|
||||
const dto = checked(CreateFindingDto, input);
|
||||
if (
|
||||
@@ -143,6 +150,38 @@ export class SeoTrackingService {
|
||||
return tx.contentResearchFinding.findUniqueOrThrow({ where: { id } });
|
||||
});
|
||||
}
|
||||
async publishFinding(id: string, input: PublishFindingDto, userId: string) {
|
||||
const dto = checked(PublishFindingDto, input);
|
||||
if (!userId || !dto.actionId.trim())
|
||||
throw new BadRequestException('Authenticated author and action required');
|
||||
// updateMany is a row-locking CAS. Feedback writes use the same row and
|
||||
// version: whichever writer wins forces the other stale request to 409.
|
||||
const result = await this.prisma.$transaction(async (tx) => {
|
||||
const claim = await tx.contentResearchFinding.updateMany({
|
||||
where: { id, version: dto.expectedVersion,
|
||||
feedbackVersion: dto.feedbackVersion, blogId: null },
|
||||
data: { actionId: dto.actionId, version: { increment: 1 } },
|
||||
});
|
||||
if (!claim.count) {
|
||||
if (!(await tx.contentResearchFinding.findUnique({ where: { id } })))
|
||||
throw new NotFoundException('Research finding not found');
|
||||
throw new ConflictException('Finding/directive changed or already published');
|
||||
}
|
||||
const blog = await createPublicationBlog(tx, dto.blog, userId);
|
||||
const finding = await tx.contentResearchFinding.update({
|
||||
where: { id }, data: { blogId: blog.id },
|
||||
});
|
||||
return { blog, finding, consumedFeedbackVersion: dto.feedbackVersion };
|
||||
});
|
||||
// Network/cache failures cannot roll back or turn a committed Blog into an
|
||||
// ambiguous error. No external content generation runs inside the lock.
|
||||
let cacheRevalidation: 'success' | 'failed' = 'failed';
|
||||
try {
|
||||
if (await (this.revalidation ?? new RevalidationService()).revalidateBlog(result.blog.slug))
|
||||
cacheRevalidation = 'success';
|
||||
} catch { /* return committed identity even on cache failure */ }
|
||||
return { ...result, cacheRevalidation };
|
||||
}
|
||||
async addSnapshot(findingId: string, input: CreateSnapshotDto) {
|
||||
const dto = checked(CreateSnapshotDto, input);
|
||||
const periodStart = new Date(dto.periodStart),
|
||||
|
||||
Reference in new issue
Block a user