How to deploy a Cron Job Node Js Script to Google Cloud?

Viewed 520

I have a nodeJs script that is run by a Cron Job using the node-cron module

The purpose of this nodeJs script is to loop over items in my MongoDB and run some function.

Is it possible to deploy this nodeJS script/app to the GCP and have it run at every Sunday? In my CronJob config in my NodeJS app, I already have it run only every Sunday.

However I was wondering whether if I Could use GCP's scheduler or just keep my Cron-Job in my NodeJs.

I've achieved this before by using Heroku Scheduler, however I have been having problems with deploying Puppeteer to Heroku therefore I am using GCP since Puppeteer works fine in the google cloud node js environment.

If anyone can give me some insight or some instructions on what I have to do I would appreciate it.

Thank you

1 Answers

What you are trying to achieve could be done by setting up a MongoDB Atlas with Google Cloud. Here you can find the documentation.

Then, you could use the Cloud Scheduler and Pub/Sub to trigger a Cloud Function (in nodeJS, like your script). Here is an example tutorial.

Then, in order to be able to connect your Cloud Function to your MongoDB cluster, this detailed guide will show you how to do so.

This should give you some insights to start searching for more information by yourself. Have in mind there are different alternatives. For example, instead of using MongoDB, you could use Firestore with your Cloud Functions and set the Cron Schedule with the Pub/Sub as previously mentioned.

Related