1
votes

After read the docs: https://cloud.google.com/appengine/docs/standard/nodejs/scheduling-jobs-with-cron-yaml

It seems that cron jobs only support HTTP cloud function.

I want to use GAE cron jobs and google pubsub to trigger my background cloud function every hour. Like:

GAE corn jobs => Cloud pub/sub => background cloud function.

Is this possible?

3
I've edited my answer to include Cloud Scheduler, which can be also helpful. - amport
@Mangu thanks a lot. I built this system successfully. - slideshowp2

3 Answers

0
votes

The Cron service is not integrated with cloud functions. It is part of app engine.

0
votes

It is possible, and is described in detail here.

0
votes

You can't directly call Cloud Functions from a Cron job, as it's an App Engine service, but you can call an App Engine handler from your Cron job, and make that handler call your Cloud Function, using whichever information you want to use from Pub/Sub.

There is an example here, which is basically what I said. You can substitute it to use Node.js in App Engine instead of Python:

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  const request = require('request');

  request('YOUR_FUNCTION_URL', { json: false }, (err, res, body) => {
    if (err) {
      return console.log(err);
    }
    res
      .status(200)
      .send('Trigger called')
      .end();
  });
});

// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

EDIT:

Cloud Scheduler is a new service (as of right now in beta) which can create cron jobs that target either App Engine, Pub/Sub or URL's. In your case, you can set up one job hitting the URL of your function, as mentioned in here. It's much simpler.