从云功能删除云任务

时间:2020-03-02 18:53:43

标签: javascript google-cloud-functions google-cloud-tasks

我正在尝试通过云功能删除Google云任务。这是我认为我需要基于Google文档的代码。

export const deleteHearingReminder = functions.firestore
  .document('Hearings/{HearingID}/Accepted/{AcceptedId}')
  .onUpdate(async change => {
    const dataBefore = change.before.data() as data;
    const dataAfter = change.before.data() as data;

    if (dataBefore === dataAfter) {
      console.log("Text didn't change");
      return null;
    }

    const taskID ={ name : dataAfter.taskId };

    const client = new CloudTasksClient();

    const [response] = await client.deleteTask(taskID);

    console.log(`delete task ${response.name}`);

    return Promise.resolve({ task: response.name });
  });

调用此函数时出现错误

Error: 7 PERMISSION_DENIED: Permission denied on resource project 6cDNgaqLniz6kHGonePh.

其中6cDN ...是我要删除的taskID,所以我的问题是我是否没有向Google任务提供足够的信息来删除此任务,导致我遇到PERMISSION_DENIED错误?另外,如果有更多信息,我应该提供相应的字段名称,因为从我在Google文档中看到的deleteTask只取“名称”。任何建议表示赞赏,谢谢。

我非常感谢所有帮助,因此现在我的代码如下所示。

const request = {
        name: `projects/${project}/locations/${location}/queues/${default_queue}/tasks/${dataAfter.taskId}`,
    };
    taskClient.deleteTask(request).catch(error => {
        console.error(`There was an error ${error}`);
    });

它可以正常工作,谢谢,我仍然在此catch方法中遇到错误

5 NOT_FOUND: Requested entity was not found

我已经对其进行了数次测试,结果始终如一。

2 个答案:

答案 0 :(得分:0)

您需要将必要的权限分配给云功能服务帐户才能删除任务。假设您使用默认的PROJECT_ID@appspot.gserviceaccount.com as服务帐户运行云功能,只需将Cloud Tasks Task Deleter (Access to delete tasks)角色授予该服务帐户即可。

Granting roles to service accounts

答案 1 :(得分:0)

对于权限问题,您需要给服务帐户使用IAM页面中的Cloud Tasks Delete角色

但是,您试图错误地删除任务。

您可以看到here the official documentation有关任务删除的信息。

尝试遵循example here,该指南显示了如何删除队列,但是删除了任务

async function deleteQueue(
  project = 'my-project-id', // Your GCP Project id
  queue = 'my-appengine-queue', // Name of the Queue to delete
  location = 'us-central1' // The GCP region in which to delete the queue
) {
  // Imports the Google Cloud Tasks library.
  const cloudTasks = require('@google-cloud/tasks');

  // Instantiates a client.
  const client = new cloudTasks.CloudTasksClient();

  // Get the fully qualified path to the queue
  const name = client.queuePath(project, location, queue);

  // Send delete queue request.
  await client.deleteQueue({name});
  console.log(`Deleted queue '${queue}'.`);
}

const args = process.argv.slice(2);
deleteQueue(...args).catch(console.error);

您传递的参数必须是以下格式的任务名称:

name=projects/[PROJECT_ID]/locations/[LOCATION]/queues/[QUEUE]/tasks/[TASK]

Here,您可以看到删除任务方法的工作方式以及期望收到的内容。

  // Deletes a task.
  //
  // A task can be deleted if it is scheduled or dispatched. A task
  // cannot be deleted if it has completed successfully or permanently
  // failed.
  rpc DeleteTask(DeleteTaskRequest) returns (google.protobuf.Empty) {
    option (google.api.http) = {
      delete: "/v2beta2/{name=projects/*/locations/*/queues/*/tasks/*}"
    };
    option (google.api.method_signature) = "name";
  }
相关问题