How to use @google-cloud/scheduler inside of Firebase Functions (Google Cloud Functions)?

Viewed 243

I want to use @google-cloud/scheduler inside of Firebase Functions like following.

import { CloudSchedulerClient } from "@google-cloud/scheduler";

export const someFunction = functions
  .firestore.document("/path/to/document")
  .onUpdate(async (change, context) => {

    // start process
    functions.logger.info("start");
    const client = new CloudSchedulerClient();

    // create instance
    const pjid = await client.auth.getProjectId();
    functions.logger.info("pjid", pjid);

    // create job
    const [res] = await client.createJob({
      job: {
        pubsubTarget: {
          topicName: "some-topic",
          data: Buffer.from("test")
        },
        schedule: "* * * * *"
      },
      parent: client.locationPath("some project", "asia-northeast1")
    });

    // finish
    functions.logger.info("name: ", res.name)
  });

But this returns permission error.

Error: 7 PERMISSION_DENIED: Cloud Scheduler API has not been used in project XXXXXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudscheduler.googleapis.com/overview?project=XXXXXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
    at Object.callErrorFromStatus (/workspace/node_modules/@google-cloud/scheduler/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
    at Object.onReceiveStatus (/workspace/node_modules/@google-cloud/scheduler/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
    at Object.onReceiveStatus (/workspace/node_modules/@google-cloud/scheduler/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:342:141)
    at Object.onReceiveStatus (/workspace/node_modules/@google-cloud/scheduler/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:305:181)
    at process.nextTick (/workspace/node_modules/@google-cloud/scheduler/node_modules/@grpc/grpc-js/build/src/call-stream.js:124:78)
    at process._tickCallback (internal/process/next_tick.js:61:11) 

XXXXXX inside of the error is a number but it isn't same as pjid I logged. (pjid is string) When I access to the URL that written in the error, it says you don't have permission or project doesn't exist.

Firestore, Functions, and Cloud Scheduler I mentioned above are on a same project so I think I don't need to authorize manually but do i need it?

https://cloud.google.com/docs/authentication/production

I've tried to authorize manually by using constructor options of CloudSchedulerClient, but it returned same error.

[Edited]

If I access to the URL that written in the error with replacing XXXXXX to pjid, I can see correct dashboard and it says API is enabled.

[Edited2]

Even if I set keyFile for constructor of the CloudSchedulerClient, it returned same error. I checked the file exists at a path I indicated.

  const dir = await fs.readdir(__dirname);
  functions.logger.info("dir ", dir);
  const client = new CloudSchedulerClient({
    keyFilename: __dirname + "/keyfile.json"
  });
0 Answers
Related