bvdputte / kirby-autopublish

Kirby plugin to schedule the automatic publishing of pages on a certain date+time. Built to work with enabled cache.
MIT License
33 stars 6 forks source link

The plugin keeps re-publishing the same draft & then immediately unpublished it on every cron cycle #15

Open andykamezou opened 9 months ago

andykamezou commented 9 months ago

I recently launched my first Kirby website and today I realised that the autopublish.log file is larger than expected. So I looked into it, and I saw these in the log:

.
.
[2024-02-16 17:05:02.497627] [info] Auto-unpublished news/test-autopublish-auto-unpublish
[2024-02-16 17:10:01.756671] [info] Autopublished news/test-autopublish-auto-unpublish
[2024-02-16 17:10:01.804146] [info] Auto-unpublished news/test-autopublish-auto-unpublish
[2024-02-16 17:15:01.912729] [info] Autopublished news/test-autopublish-auto-unpublish
[2024-02-16 17:15:01.950906] [info] Auto-unpublished news/test-autopublish-auto-unpublish
[2024-02-16 17:20:01.652764] [info] Autopublished news/test-autopublish-auto-unpublish
[2024-02-16 17:20:01.687352] [info] Auto-unpublished news/test-autopublish-auto-unpublish
[2024-02-16 17:25:02.179824] [info] Autopublished news/test-autopublish-auto-unpublish
[2024-02-16 17:25:02.226582] [info] Auto-unpublished news/test-autopublish-auto-unpublish
[2024-02-16 17:30:02.495575] [info] Autopublished news/test-autopublish-auto-unpublish
[2024-02-16 17:30:02.542977] [info] Auto-unpublished news/test-autopublish-auto-unpublish
[2024-02-16 17:35:01.669064] [info] Autopublished news/test-autopublish-auto-unpublish
[2024-02-16 17:35:01.698959] [info] Auto-unpublished news/test-autopublish-auto-unpublish

It was the test page that I made to test this plugin last month. The plugin has been publishing and unpublished the same test page again and again.

I assume it's because despite the page has been published/unpublished, the auto-publish and auto-unpublish fields still have a date and time set in it. So on every cron cycle, the plugin will:

  1. found this draft with auto-publish date & time set
  2. publish the page (draft -> listed)
  3. found the page (which was just published) with auto-unpublish date & time is set
  4. unpublish the page (listed -> draft)

Maybe the plugin needs to clear the date & time fields after publishing/unpublish the page.

bvdputte commented 9 months ago

TBH, I'm not sure the plugin should alter the field... ?

If I understand this correctly, the plugin works as designed and publishes/unpublishes as described in the field, right?

andykamezou commented 9 months ago

If I understand this correctly, the plugin works as designed and publishes/unpublishes as described in the field, right?

Yes, it seems to be working as you designed.

But because both autopublish and autounpublish fields still have values, the plugin keeps re-publishing the same draft and then immediately unpublish it.

To be clear, this will only happen when the page is set to use both autopublish and autounpublish.

. I think that was unnecessary behaviour because there is no reason to re-publish the same draft and then immediately unpublish it, especially when the date & time for both autopublish and autounpublish fields already passed a long time ago.

For example, my test page has been auto-published and auto-unpublished last month. I had to manually clear the fields to make sure the page was not included again in the next cron. Or else the plugin will keep trying to re-publish the page until the end of the world~

That one test page also produces about 750kb of autopublish log after 3 weeks of cron every 15 minutes. There's probably someone out there who frequently uses both fields for many pages and didn't realise that their log files are growing bigger.

. So what I was thinking is if I have to fix it on my own, the quickest solution is probably clearing the autopublish field after successfully auto-publish the page. The same goes for when auto-unpublish the page.

bvdputte commented 9 months ago

Hmmm. I'm still in dubio about actually altering the content file.

Maybe add a check if there's

  1. both a publish date and an unpublish date
  2. and when unpublish is < now

don't re-publish?

andykamezou commented 9 months ago

Yeah, maybe something like that should work too. As the issue is caused by drafts that has both publish and unpublish time set, we need extra check to exclude certain drafts from the autoPublishedDrafts collection.

When filtering autoPublishedDrafts collection, don't include the draft if :

  1. The page has both $publishTime and $unpublishTime set
  2. And both time has passed

Something like that?