Google Cloud - How to deploy, start and log a nodejs app for background jobs

Viewed 93

I created a (standard) nodejs app on Google Cloud App engine. My aim is to build a background job app which will only send http requests to another server every x seconds.

// index.js
const winston = require("winston");

const { LoggingWinston } = require("@google-cloud/logging-winston");
const loggingWinston = new LoggingWinston();

const logger = winston.createLogger({
  level: "info",
  transports: [new winston.transports.Console(), loggingWinston],
});

logger.info("started");

const pingLoop = () =>
  setTimeout(function () {
    logger.info("ping");
    // send HTTP request
    pingLoop();
  }, 10000);

pingLoop();

Deploying the app seems to work fine gcloud app deploy --project=project-name. However, I can't see any logs when I run gcloud app logs tail --project=project-name

Waiting for new log entries...

My package.json

{
  "name": "test-nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "@google-cloud/logging-winston": "^4.0.4",
    "winston": "^3.3.3"
  }
}

and app.yaml

runtime: nodejs12

Are background jobs supposed to work on Google App engine?

1 Answers

There are two ways of setting up asynchronous tasks on App Engine.

  1. If you want to set up a CRON job, take a look at this link for cron.yaml reference.
  2. If you want trigger asynchronous tasks that run in background, take a look at Google Cloud Tasks.

Both Cloud Tasks and cron.yaml work on http requests. They call a HTTP endpoint in your app at a specified time or interval.

Related