Skip to content
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

fix: check if schedule changed before re-scheduling #6

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/hooks/syncSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export default function syncSchedule(
debug(`syncSchedule ${collection.slug} ${doc.id}`)
// eslint-disable-next-line no-underscore-dangle
const isPublishing = doc._status === 'published'
const shouldSchedule = doc?.publish_date && new Date(doc.publish_date) > new Date()
const publishInFuture = doc?.publish_date && new Date(doc.publish_date) > new Date()
const scheduleChanged = doc?.publish_date !== previousDoc?.publish_date
try {
if (isPublishing || scheduleChanged) {
debug('Deleting previous schedule')
// if `publish_date` is modified, remove any pending schedulers.
// there should only ever be a single result here in practice
await payload.delete({
const deleted = await payload.delete({
collection: 'scheduled_posts',
where: {
and: [
Expand All @@ -37,12 +37,16 @@ export default function syncSchedule(
req,
})

debug(`$deleted ${deleted?.docs?.length} previous schedules`)

if (isPublishing) {
// if the post is being published, we're done
return doc
}
}

if (shouldSchedule) {
// if the publish date has changed and it's in the future, schedule it
if (scheduleChanged && publishInFuture) {
debug('Scheduling post', collection.slug, doc.id)
let dbValue = doc.id

Expand All @@ -52,8 +56,7 @@ export default function syncSchedule(
dbValue = Number(doc.id)
}

// if the new date is in the future, schedule it
await payload.create({
const res = await payload.create({
collection: 'scheduled_posts',
data: {
post: {
Expand All @@ -65,6 +68,8 @@ export default function syncSchedule(
},
req,
})

debug(`[payload-plugin-scheduler] scheduled ${collection.slug}:${dbValue} ${res.id}`)
}
} catch (error: unknown) {
payload.logger.error('[payload-plugin-scheduler] Error scheduling post')
Expand Down
Loading