Louis Sankey
Louis Sankey

Reputation: 492

Can I schedule a Google Cloud Task on the client side to call a cloud function with a payload?

I want to make sure I'm thinking about Cloud Tasks right conceptually, and not sure that I am.

The examples I've been looking at seem to trigger a cloud function first that then schedules a task, that then calls a cloud function again.

(Or at least this is what I'm understanding, I could be wrong).

I'd like to set up something so that when a user clicks a button, it schedules a cloud task for some time in the future (anywhere from 1 minute to an hour and half). The cloud task then triggers the cloud function to upload the payload to the db.

I tried to set this up client side but I've been getting the error "You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments."

I don't want the user to have to authenticate if that's what this is saying (not sure why I'd have to do that for my use case).

This is the code that gives that error.

const {CloudTasksClient} = require('@google-cloud/tasks');
const client = new CloudTasksClient();


// import { Plugins } from '@capacitor/core';
//     const {  RemotePlugin } = Plugins;

const scheduleTask = async(seconds) => {
  async function createHttpTask() {

    const project = 'spiral-productivity';
    const queue = 'spiral';
    const location = 'us-west2';
    const url = 'https://example.com/taskhandler';
    const payload = 'Hello, World!';
    const inSeconds = 5;

    // Construct the fully qualified queue name.
    const parent = client.queuePath(project, location, queue);

    const task = {
      httpRequest: {
        httpMethod: 'POST',
        url,
      },
    };

    if (payload) {
      task.httpRequest.body = Buffer.from(payload).toString('base64');
    }

    if (inSeconds) {
      // The time when the task is scheduled to be attempted.
      task.scheduleTime = {
        seconds: inSeconds + Date.now() / 1000,
      };
    }

    // Send create task request.
    console.log('Sending task:');
    console.log(task);
    const request = {parent: parent, task: task};
    const [response] = await client.createTask(request);
    console.log(`Created task ${response.name}`);
  }
  createHttpTask();
  // [END cloud_tasks_create_http_task]

}

More recently I set up a service account and download a .json file and all of that. But doesn't this mean my users will have to authenticate?

That's why I stopped. Maybe I'm on the wrong track, but if anyone wants to answer what I need to do to schedule a cloud task from the client side without making the user authenticate, it would be a big help.

As always, I'm happy to improve the question if anything isn't clear. Just let me know, thanks!

Upvotes: 0

Views: 1071

Answers (1)

Andrés
Andrés

Reputation: 515

Yes.

Your understanding is mostly accurate. Cloud Tasks is a way to queue "tasks". The examples are likely using Cloud Functions as an analog for "some app" (a web app) that would be analogous to your Node.js (web) app, i.e. your Node.js app can submit tasks to Cloud Tasks. To access Google Cloud Platform services (e.g. Cloud Tasks), you need to authenticate and authorize.

Since your app is the "user" of the GCP services, you're correct in using a Service Account.

See Application Default Credentials to understand authenticating (code) as a service account.

Additionally, see Controlling access to webapps.

Upvotes: 2

Related Questions