Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cronjob not working using node-cron in CloudRun

Hi guys, I'm using a simple typscript file for running one simple cronjob. Below the code:

 
import cron from 'node-cron';
 
cron.schedule('0 */2 * * *', async () => {
    await record();
  });
 
We are running this app in Cloud Run having it dockerized.
 
The cron job is not working at all. We dont know If we need to choose a different spec of the running node. We have the same exactly code running in Herokuapp (our sandbox) but in GCP it doesn't work.
 
0 1 1,361
1 REPLY 1

Hi,

Does this basically run indefinitely, waking up periodically do run the job, then go back to sleep? If that's the case, you'll need to select the "CPU always allocated" option for Cloud Run and probably set min instances=1 as well. Without this, your job will not run, because by default, Cloud Run services only run when they receive a request. 

Another option is to deploy the actual job part as a Cloud Run job, and set up a Cloud Scheduler trigger to run it on schedule. This will likely work out cheaper as you'll only pay for CPU when the job is actually running. https://cloud.google.com/run/docs/execute/jobs-on-schedule