Cloud Task Wrapper: Node.js Client
Cloud Tasks Wrapper API client for Node.js
This module offers additional wrapper on top of @google-cloud/tasks (https://www.npmjs.com/package/@google-cloud/task).
There are some of the features that are missing as part of the current @google-cloud/task module. This cloud task wrapper aims at addressing the missing utils by using @google-cloud/task under the hood.
Challenges this Cloud Task Wrapper addresses:
- Easier instantiation of cloud task client (just pass the config and access the methods)
- Unless you know the complete name of a cloud task, you wouldn't be able to fetch or access the task. This module makes it easier. Provide a regex and it gives you the list of matching cloud tasks
- Update the scheduled time of a cloud task - App Engine/Http Request
- Update the body of a cloud task - AppEngineRequest
- Update the body of a cloud task - HttpRequest
- Simplifies creation, deletion and listing queues
Quickstart
Before you begin
- [Select or create a Cloud Platform project][projects].
- [Enable billing for your project][billing].
- [Enable the Cloud Tasks API][enable_api].
- [Set up authentication with a service account][auth] so you can access the
API from your local workstation.
Installing the client library
npm i cloud-task-wrapper
Using the client library to fetch tasks that match regex
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
location,
};
const wrapper = new CloudTaskWrapper(config);
const cloudTasks = await wrapper.getCloudTasksByRegex(regex, queue);
console.log(`List of matching cloud tasks ${cloudTasks}`);
}
quickstart();
Using the client library to delete tasks that match regex
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
location,
};
const wrapper = new CloudTaskWrapper(config);
const cloudTasks = await wrapper.getCloudTasksByRegex(regex, queue);
for (const cloudTask of cloudTasks) {
const taskName = cloudTask.name.split("/").pop();
await wrapper.deleteCloudTaskByName(taskName, queue);
}
console.log(`List of deleted cloud tasks ${cloudTasks}`);
}
quickstart();
Using the client library to update the scheduled time of a cloud task
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
location,
};
const wrapper = new CloudTaskWrapper(config);
const updatedCloudTask = await wrapper.updateCloudTaskScheduledTime(
name,
20,
queue,
newName
);
console.log(`Updated the cloud task and its new name - ${updatedCloudTask}`);
}
quickstart();
Using the client library to get the full cloud task if you know the name
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
location,
};
const wrapper = new CloudTaskWrapper(config);
const cloudTask = await wrapper.getCloudTaskByName(name, queue);
console.log(`The cloud task is - ${cloudTask}`);
}
quickstart();
Using the client library to delete the cloud task if you know the name
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
location,
};
const wrapper = new CloudTaskWrapper(config);
await wrapper.deleteCloudTaskByName(name, queue);
console.log(`Deleted the cloud task`);
}
quickstart();
Using the client library to create a cloud task - App Engine Request
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
queue,
location,
};
const wrapper = new CloudTaskWrapper(config);
const task = {
appEngineHttpRequest: {
httpMethod: "POST",
relativeUri: "/log_payload",
body: Buffer.from(payload).toString("base64")
},
scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
};
const cloudTask = await wrapper.createCloudTask(task, queue, name);
console.log(`Created task ${cloudTask}`);
}
quickstart();
Using the client library to create a cloud task - Http Request
const { CloudTaskWrapper } = require("cloud-task-wrapper");
async function quickstart() {
const config = {
project,
location,
};
const wrapper = new CloudTaskWrapper(config);
const task = {
httpRequest: {
httpMethod: "POST",
url: "https://ram-sankhavaram.com",
body: Buffer.from(payload).toString("base64"),
},
scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
};
const cloudTask = await wrapper.createCloudTask(task, queue, name);
console.log(`Created task ${cloudTask}`);
}
quickstart();