Search code examples
node.jsgoogle-cloud-platformgoogle-cloud-rungoogle-cloud-scheduler

Google cloud scheduler for google cloud run using nodejs


I want to write a nodejs script which calls google cloud scheduler to schedule a cloud run service to be run only once (non recurring event). How can I do it?

I've checked out this library npmjs.com/package/@google-cloud/scheduler , but couldn't find a way to create a non recurring job.

I've tried something like this

const { CloudSchedulerClient } = require('@google-cloud/scheduler');
const axios = require('axios');


const projectId = 'project-id';
const location = 'location'; 
const jobName = 'job-name';
const cloudRunURL = 'cloud-run-url';

async function createJob() {
  const client = new CloudSchedulerClient();

  const parent = `projects/${projectId}/locations/${location}`;
  const job = {
    name: `projects/${projectId}/locations/${location}/jobs/${jobName}`,
    httpTarget: {
      uri: cloudRunURL,
      httpMethod: 'GET',
    },
    schedule: '0 12 * * *',
    timeZone: 'UTC', 
  };

  try {
    const [createdJob] = await client.createJob({
      parent: parent,
      job: job,
    });

    console.log('Job created:', createdJob.name);
  } catch (err) {
    console.error('Error creating job:', err);
  }
}


createJob();

But I'm stuck How to make it non recurring?


Solution

  • As @Jon Skeet, stated Cloud Scheduler is designed for recurring jobs. Thus, creating a job to run only once is not intended.

    Cloud Tasks should be able to satisfy your use case. Tasks are meant for one time execution and can be scheduled up to 30 days in the future. Each task has its own payload and can call any public HTTP endpoint using this beta feature.

    Also have a look at this Documentation where there are API’s for working with Cloud Tasks so one doesn't have to code REST. Probably a good idea to have a read at all the Cloud Task docs.

    Also have a look at this Stackoverflow Link for reference.

    If you are not satisfied with the above options feel free to raise a new Feature request explaining your use case and requesting non recurring functionality in cloud scheduler. You can refer issue tracker page when raising a feature request.