#How to “set up to process Jobs” for scheduled posts?

1 messages · Page 1 of 1 (latest)

dusty patrol
#

https://payloadcms.com/docs/versions/drafts#scheduled-publish
I’m not sure what that is supposed to mean to be honest—I’ve tested around for quite some time now, trying to add different jobs related things to my config

This whole scheduling thing seems pretty opaque in how it works, but the docs make it seem like it just works™ lol

I would even offer to help write some docs for it, but I litteraly have no idea how the scheduler works 😄

Payload

Payload is a headless CMS and application framework built with TypeScript, Node.js, React and MongoDB

next solarBOT
tidal shuttle
dusty patrol
scenic osprey
#

I am trying to solve this aswell... the documents doesn't explain how this works at all... just an important note saying that you need to enable jobs in the config but nothing more than that.

scenic osprey
#

Even using it in the website template doesn't work, so has to be a broken function..

dusty patrol
#

Hmm I think I'm onto something, try adding this to your payload config:

autoRun: [
    {
        cron: '*/1 * * * *',
        limit: 10,
        queue: 'default',
    },
],
scenic osprey
#

Alright will try.. I also tried to see if you had to handle the task yourself since the job that's being created is targeting the slug 'schedulePublish', but nothing happend.

    tasks: [
      {
        slug: 'schedulePublish',
        handler: ({ input, job, req }) => {
          console.log(input, job, req)
        },
      } as TaskConfig<'schedulePublish'>,
    ],
dusty patrol
scenic osprey
#

Alright sounds great, will remove it then and try again without it.

#

Still doesn't work... I doesn't really understand either why there should be a cron job for this that always run each minute. The job is suppose to run only once when the scheduled time has arrived and publish/unpublish a document.

toxic bramble
#

You need the cron running to continuously poll the job queue to see if there’s any scheduled posts that need to be published

#

I agree the docs are lacking here

scenic osprey
#

Sure.. but shouldn't that be enabled by default, especially in the website template that has schedulePublish enabled?

toxic bramble
#

Yeah there should probably be a vercel.json set up with everything you need in the template

#

I sort of get why it’s not configured by default because depending on how you deploy the app the method you use to trigger the cron coild be different. But it should really be highlighted in the docs it’s pretty confusing

scenic osprey
#

So if I'm self hosting, is there another approach to this? I'm not using vercel. So by having the autoRun setup the job should be executed at the time I schedule it for.

toxic bramble
#

Ahh

scenic osprey
#

Even have the shouldAutoRun return true as the docs says.

toxic bramble
#

If you’re on a dedicated server, not serverless, you can just run a cron on the machine yourself

scenic osprey
#

Okay and then have that cron go through the jobs by itself and then see if the scheduled post is ready to be published or not?

toxic bramble
#

Yeah

scenic osprey
#

I see..

#

Not what I had imagined at first haha, thought this would be handled by Payload for some reason

toxic bramble
#

It’s pretty confusing, I asked the same question here a few times before someone from the team clarified

#

It makes sense that they don’t want to like. Sniff out whether you’re running on serverless and adjust the queue behavior accordingly. But rly needs more clarity in the docs

scenic osprey
#

Yeah thats true, would've saved me some time knowing that. So to put it simply, have a cron go through jobs, if the schedule is on time get the input data and do whatever you want with it.

#

Well thanks for the help atleast

toxic bramble
#

Np

#

Check “vercel cron example” in the docs link I sent above

scenic osprey
#

Will do

#

Nice, got it working. Thanks!

dusty patrol
toxic bramble
#

I haven’t set this up personally but I think so yes

robust lodge
#

I have scheduled publishing and I see the jobs get ran-- they disappear from the jobs list and db after their schedule time. However, documents don't actually get published and if I have unique fields on them, it can suddenly mean that im unable to publish the document all together, as it'll error out, saying that there is already a document with their respective values. However, the document is not visible in the payload admin.

I just haven't had time to submit an issue yet; are you guys "past" this stage? Is it working for you to any extent beyond this?

dusty patrol
#

That’s not something I’ve experienced yet, but I also didn’t try publishing with unique fields yet

ebon nacelle
alpine turret
#

@ebon nacelle @scenic osprey @dusty patrol Can you guys please share working setup that you use?

ebon nacelle
# alpine turret <@770935976368406538> <@151051925150760960> <@140369619180453888> Can you guys p...

Sure, it is a simple setup, I got it working rn.
In the posts CollectionConfig i added:
versions: { drafts: { autosave: { interval: 100, }, schedulePublish: true, //this line }, maxPerDoc: 50, },

And in the payload.config.ts inside the buildConfig I just added:
jobs: { autoRun: [ { cron: '*/1 * * * *', //change this to whatever interval you would like, this now checks every minute limit: 10, queue: 'default', }, ], tasks: [], shouldAutoRun: async () => true, },

dusty patrol
undone zodiac
#

But some time schedulePublish ran but revalidate data does not work and data do not change.

ebon nacelle
#

Ah, i have in collections/Posts/hooks/revalidatePost.ts a check if there is a job context:

// Check if we're in a job context (payload API with no user) const isJobContext = req?.payloadAPI === 'REST' && !req.user

if that is true it skips over revalidation

undone zodiac
#

maybe I found a solution for my case

languid terrace
#

Hey everyone. Finally I was also able to make it work. For now, I needed to skip revalidation as it would trigger an error in my afterchange hook. How do you handle revalidation? Seems unintuitive to skip revalidation and add another revalidating step for scheduled posts.