Skip to content

Instantly share code, notes, and snippets.

@alexander-fenster
Created July 14, 2022 01:54
Show Gist options
  • Save alexander-fenster/832dcdaf46f1e21f5d3ea337266c7778 to your computer and use it in GitHub Desktop.
Save alexander-fenster/832dcdaf46f1e21f5d3ea337266c7778 to your computer and use it in GitHub Desktop.
Cloud Tasks quickstart sample in TypeScript
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
import {CloudTasksClient, protos} from '@google-cloud/tasks';
/**
* Create a task for a given queue with an arbitrary payload.
*/
function main(project: string, location: string, queue: string, payload?: string, inSeconds?: string) {
// [START cloud_tasks_quickstart]
// Instantiates a client.
const client = new CloudTasksClient();
async function quickstart() {
// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-appengine-queue';
// const location = 'us-central1';
// const payload = 'hello';
// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
const task: protos.google.cloud.tasks.v2.ITask = {
appEngineHttpRequest: {
httpMethod: 'POST',
relativeUri: '/log_payload',
},
};
if (payload) {
task.appEngineHttpRequest!.body = Buffer.from(payload).toString('base64');
}
if (inSeconds) {
task.scheduleTime = {
seconds: Number(inSeconds) + Date.now() / 1000,
};
}
const request = {
parent: parent,
task: task,
};
console.log('Sending task:');
console.log(task);
// Send create task request.
const [response] = await client.createTask(request);
const name = response.name;
console.log(`Created task ${name}`);
}
quickstart();
// [END cloud_tasks_quickstart]
}
process.on('unhandledRejection', err => {
console.error((err as Error).message);
process.exitCode = 1;
});
main(...(process.argv.slice(2) as [string, string, string, string?, string?]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment