App with function that runs every hour

gut_und_gunstig
Shopify Partner
6 0 1

Hi there! I was hoping somebody could give me some guidance on this matter.

 

I am developing an app. The app is simple -it (1) loops through all products, (2) grabs several pieces of information about each product, and (3) based on that information updates a specific product metafield.

 

So far, so good. I've done this part.

 

What I need to do now is for the app is to go through this process on an hourly basis - it should be able to run through steps 1-3 in the background, without someone having to open up the app and press the 'update' button.

 

Can anyone think of a way to do this?

 

For context, I am developing this app using the React/Remix template.

Replies 6 (6)

SBD_
Shopify Staff
1829 269 406

Hey @gut_und_gunstig 

 

Sure thing - be sure to use an offline access token. Scheduling the job to run every hour is different for each platform/host - who are you using?

Scott | Developer Advocate @ Shopify 

gut_und_gunstig
Shopify Partner
6 0 1

Hey @SBD_ thanks very much for your response. The app is hosted on Heroku currently. What would be the process with that? Thanks 🙂

SBD_
Shopify Staff
1829 269 406

Try this one 🙂 https://devcenter.heroku.com/articles/scheduler

Scott | Developer Advocate @ Shopify 

gut_und_gunstig
Shopify Partner
6 0 1

@SBD_ thanks very much!

I have one further question. The app I'm building has been built with the React/Remix template. As far as I can tell, there doesn't seem to be a straightforward way to run background/CRON job with Remix. It doesn't seem like CRON jobs can be done through remix routes, although I might be wrong.

 

Do you have any idea how I might set this up?

SBD_
Shopify Staff
1829 269 406

Hey @gut_und_gunstig 

 

This isn't specifically related to Remix, but rather the environment where your Remix app is running.

 

You could use something like this: https://devcenter.heroku.com/articles/crontogo to send a recurring webhook to your Remix app (and create a route to respond to the webhook). Don't forget to validate that the requests come from the webhook.

 

Good luck!

Scott | Developer Advocate @ Shopify 

gut_und_gunstig
Shopify Partner
6 0 1

Hey @SBD_ thanks so much for your guidance, I've followed your suggestion and got almost all of the way.

 

The only issue I'm facing is the setting up the route to respond to the webhook. I've written a function that successfully updates the database when i run it locally, but when it's deployed to heroku and I call the function through Cron To Go scheduler, the database isn't updated.

The function itself is fired and a 'successfully updated' response is received, but there's no change to the database at all.

Can you provide any guidance as to how I would set up and configure a route to respond to the webhook?