📨 The ultimate agentic social media scheduling tool 🤖
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
fix(clipping): close the clip and draft creation races from review
Nevo Davidf5d83b19f662
diff --git a/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.repository.ts b/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.repository.tsindex 063a4c9da..b6df74176 100644--- a/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.repository.ts+++ b/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.repository.ts@@ -208,13 +208,21 @@ export class ClippingRepository {});}- // In one transaction with a look at what is there: an attempt that timed out- // can still be running when its retry gets here, and only one may store clips+ // An attempt that timed out can still be running when its retry gets here, and+ // only one may store clips. The look at what is there is not enough on its own+ // (both can see nothing), so the update takes the row of the clipping first:+ // the second attempt waits on it until the first commits and then finds its clipscreateClips(clippingId: string,clips: { title: string; content: string; start: number; end: number }[]) {return this._transaction.model.$transaction(async (tx) => {+ await tx.clipping.update({+ where: { id: clippingId },+ data: { updatedAt: new Date() },+ select: { id: true },+ });+const select = { where: { clippingId }, select: { id: true } };const existing = await tx.clippingClip.findMany(select);if (existing.length) {@@ -245,6 +253,21 @@ export class ClippingRepository {});}+ // Only the attempt that moves draftedAt off null may create the draft+ async claimClipDraft(id: string) {+ const { count } = await this._clippingClip.model.clippingClip.updateMany({+ where: {+ id,+ draftedAt: null,+ },+ data: {+ draftedAt: new Date(),+ },+ });++ return count === 1;+ }+getClipById(id: string) {return this._clippingClip.model.clippingClip.findUnique({where: {@@ -265,7 +288,7 @@ export class ClippingRepository {mediaId?: string;path?: string;thumbnail?: string;- draftedAt?: Date;+ draftedAt?: Date | null;}) {return this._clippingClip.model.clippingClip.update({diff --git a/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.service.ts b/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.service.tsindex 31dd28f29..eaf44edaf 100644--- a/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.service.ts+++ b/libraries/nestjs-libraries/src/database/prisma/clipping/clipping.service.ts@@ -127,8 +127,12 @@ export class ClippingService {case 'UNSUPPORTED_INPUT':return 'This link is not a video that can be clipped. Live streams are not supported.';default:- // the message and the stderr tail can carry presigned urls- console.error('Clipping job failed:', JSON.stringify(failure));+ // the message and the stderr tail can carry presigned urls, which are+ // as good as a key to the file for as long as they live+ console.error(+ 'Clipping job failed:',+ JSON.stringify(failure)?.replace(/https?:\/\/[^\s"'\\]+/g, '[url]')+ );return `The video could not be processed (${failure?.code || 'FAILED'}).`;@@ -935,46 +939,55 @@ export class ClippingService {continue;}- const nextTime = await this._postsService.findFreeDateTime(- clipping.organizationId- );+ // claimed before the draft exists: a retry, or an attempt that timed out+ // and is still running, skips the clip instead of drafting it a second time+ if (!(await this._clippingRepository.claimClipDraft(clip.id))) {+ continue;+ }- await this._postsService.createPost(- clipping.organizationId,- {- date: nextTime + 'Z',- order: makeId(10),- shortLink: false,- type: 'draft',- tags: [],- posts: integrations.map((integration) => ({- settings: {- __type: integration!.providerIdentifier as any,- },- group: makeId(10),- integration: { id: integration!.id },- value: [- {- id: makeId(10),- delay: 0,- content: clip.content,- image: [- {- id: clip.mediaId || makeId(10),- path: clip.path!,- ...(clip.thumbnail ? { thumbnail: clip.thumbnail } : {}),- },- ],- },- ],- })),- },- 'UNKNOWN'- );+ try {+ const nextTime = await this._postsService.findFreeDateTime(+ clipping.organizationId+ );- await this._clippingRepository.updateClip(clip.id, {- draftedAt: new Date(),- });+ await this._postsService.createPost(+ clipping.organizationId,+ {+ date: nextTime + 'Z',+ order: makeId(10),+ shortLink: false,+ type: 'draft',+ tags: [],+ posts: integrations.map((integration) => ({+ settings: {+ __type: integration!.providerIdentifier as any,+ },+ group: makeId(10),+ integration: { id: integration!.id },+ value: [+ {+ id: makeId(10),+ delay: 0,+ content: clip.content,+ image: [+ {+ id: clip.mediaId || makeId(10),+ path: clip.path!,+ ...(clip.thumbnail ? { thumbnail: clip.thumbnail } : {}),+ },+ ],+ },+ ],+ })),+ },+ 'UNKNOWN'+ );+ } catch (err) {+ await this._clippingRepository.updateClip(clip.id, {+ draftedAt: null,+ });+ throw err;+ }}}diff --git a/libraries/react-shared-libraries/src/translation/locales/tr/translation.json b/libraries/react-shared-libraries/src/translation/locales/tr/translation.jsonindex 76f9dbb17..3ad6a0a6a 100644--- a/libraries/react-shared-libraries/src/translation/locales/tr/translation.json+++ b/libraries/react-shared-libraries/src/translation/locales/tr/translation.json@@ -530,7 +530,7 @@"billing_advanced_picture_editor": "Gelişmiş Resim Editörü","billing_ai_images_per_month": "Aylık Yapay Zeka Görselleri","billing_ai_videos_per_month": "Aylık Yapay Zeka Videoları",- "billing_clipping_minutes_per_month": "dakika aylık yapay zeka video kırpma",+ "billing_clipping_minutes_per_month": "dakika/ay yapay zeka video kırpma","billing_billing_address": "Fatura Adresi","billing_payment": "Ödeme","billing_powered_by_stripe": "Güvenli ödemeler tarafından işlenir",