forked from payloadcms/payload
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: delete scheduled publish jobs when deleting documents (payloadcm…
…s#10584) ### What? When a document gets deleted we are not cleaning up jobs that would fail if the document doesn't exist. This change makes an extra call to the DB to delete any incomplete jobs for the document. ### Why? The jobs queue will error and retry needlessly unless these are purged. ### How? Adds a call to delete jobs from the delete operation.
- Loading branch information
1 parent
d4039f2
commit 05b9d94
Showing
5 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
60 changes: 60 additions & 0 deletions
60
packages/payload/src/versions/deleteScheduledPublishJobs.ts
This file contains 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,60 @@ | ||
import type { PayloadRequest } from '../types/index.js' | ||
|
||
import { type Payload } from '../index.js' | ||
|
||
type Args = { | ||
id?: number | string | ||
payload: Payload | ||
req?: PayloadRequest | ||
slug: string | ||
} | ||
|
||
export const deleteScheduledPublishJobs = async ({ | ||
id, | ||
slug, | ||
payload, | ||
req, | ||
}: Args): Promise<void> => { | ||
try { | ||
await payload.db.deleteMany({ | ||
collection: 'payload-jobs', | ||
req, | ||
where: { | ||
and: [ | ||
// only want to delete jobs have not run yet | ||
{ | ||
completedAt: { | ||
exists: false, | ||
}, | ||
}, | ||
{ | ||
processing: { | ||
equals: false, | ||
}, | ||
}, | ||
{ | ||
'input.doc.value': { | ||
equals: id, | ||
}, | ||
}, | ||
{ | ||
'input.doc.relationTo': { | ||
equals: slug, | ||
}, | ||
}, | ||
// data.type narrows scheduled publish jobs in case of another job having input.doc.value | ||
{ | ||
taskSlug: { | ||
equals: 'schedulePublish', | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
} catch (err) { | ||
payload.logger.error({ | ||
err, | ||
msg: `There was an error deleting scheduled publish jobs from the queue for ${slug} document with ID ${id}.`, | ||
}) | ||
} | ||
} |
This file contains 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