Skip to content

Commit acb9c06

Browse files
docs(samples): update samples to latest standards (#475)
1 parent 39d788b commit acb9c06

8 files changed

+229
-184
lines changed

cloud-tasks/createHttpTask.js

+39-32
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/
1716

1817
// sample-metadata:
1918
// title: Cloud Tasks Create HTTP Target
@@ -23,7 +22,7 @@
2322
/**
2423
* Create a task with an HTTP target for a given queue with an arbitrary payload.
2524
*/
26-
async function createHttpTask(
25+
function main(
2726
project = 'my-project-id', // Your GCP Project id
2827
queue = 'my-appengine-queue', // Name of your Queue
2928
location = 'us-central1', // The GCP region of your queue
@@ -38,41 +37,49 @@ async function createHttpTask(
3837
// Instantiates a client.
3938
const client = new CloudTasksClient();
4039

41-
// TODO(developer): Uncomment these lines and replace with your values.
42-
// const project = 'my-project-id';
43-
// const queue = 'my-queue';
44-
// const location = 'us-central1';
45-
// const url = 'https://example.com/taskhandler';
46-
// const payload = 'Hello, World!';
40+
async function createHttpTask() {
41+
// TODO(developer): Uncomment these lines and replace with your values.
42+
// const project = 'my-project-id';
43+
// const queue = 'my-queue';
44+
// const location = 'us-central1';
45+
// const url = 'https://example.com/taskhandler';
46+
// const payload = 'Hello, World!';
4747

48-
// Construct the fully qualified queue name.
49-
const parent = client.queuePath(project, location, queue);
48+
// Construct the fully qualified queue name.
49+
const parent = client.queuePath(project, location, queue);
5050

51-
const task = {
52-
httpRequest: {
53-
httpMethod: 'POST',
54-
url,
55-
},
56-
};
51+
const task = {
52+
httpRequest: {
53+
httpMethod: 'POST',
54+
url,
55+
},
56+
};
5757

58-
if (payload) {
59-
task.httpRequest.body = Buffer.from(payload).toString('base64');
60-
}
58+
if (payload) {
59+
task.httpRequest.body = Buffer.from(payload).toString('base64');
60+
}
6161

62-
if (inSeconds) {
63-
// The time when the task is scheduled to be attempted.
64-
task.scheduleTime = {
65-
seconds: inSeconds + Date.now() / 1000,
66-
};
67-
}
62+
if (inSeconds) {
63+
// The time when the task is scheduled to be attempted.
64+
task.scheduleTime = {
65+
seconds: inSeconds + Date.now() / 1000,
66+
};
67+
}
6868

69-
// Send create task request.
70-
console.log('Sending task:');
71-
console.log(task);
72-
const request = {parent, task};
73-
const [response] = await client.createTask(request);
74-
console.log(`Created task ${response.name}`);
69+
// Send create task request.
70+
console.log('Sending task:');
71+
console.log(task);
72+
const request = {parent, task};
73+
const [response] = await client.createTask(request);
74+
console.log(`Created task ${response.name}`);
75+
}
76+
createHttpTask();
7577
// [END cloud_tasks_create_http_task]
7678
}
7779

78-
createHttpTask(...process.argv.slice(2)).catch(console.error);
80+
process.on('unhandledRejection', err => {
81+
console.error(err.message);
82+
process.exitCode = 1;
83+
});
84+
85+
main(...process.argv.slice(2));

cloud-tasks/createHttpTaskWithToken.js

+43-37
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/
1716

1817
// sample-metadata:
1918
// title: Cloud Tasks Create HTTP Target with Token
@@ -23,7 +22,7 @@
2322
/**
2423
* Create a task with an HTTP target for a given queue with an arbitrary payload.
2524
*/
26-
async function createHttpTaskWithToken(
25+
function main(
2726
project = 'my-project-id', // Your GCP Project id
2827
queue = 'my-queue', // Name of your Queue
2928
location = 'us-central1', // The GCP region of your queue
@@ -39,47 +38,54 @@ async function createHttpTaskWithToken(
3938
// Instantiates a client.
4039
const client = new CloudTasksClient();
4140

42-
// TODO(developer): Uncomment these lines and replace with your values.
43-
// const project = 'my-project-id';
44-
// const queue = 'my-queue';
45-
// const location = 'us-central1';
46-
// const url = 'https://example.com/taskhandler';
47-
// const serviceAccountEmail = 'client@<project-id>.iam.gserviceaccount.com';
48-
// const payload = 'Hello, World!';
41+
async function createHttpTaskWithToken() {
42+
// TODO(developer): Uncomment these lines and replace with your values.
43+
// const project = 'my-project-id';
44+
// const queue = 'my-queue';
45+
// const location = 'us-central1';
46+
// const url = 'https://example.com/taskhandler';
47+
// const serviceAccountEmail = 'client@<project-id>.iam.gserviceaccount.com';
48+
// const payload = 'Hello, World!';
4949

50-
// Construct the fully qualified queue name.
51-
const parent = client.queuePath(project, location, queue);
50+
// Construct the fully qualified queue name.
51+
const parent = client.queuePath(project, location, queue);
5252

53-
const task = {
54-
httpRequest: {
55-
httpMethod: 'POST',
56-
url,
57-
oidcToken: {
58-
serviceAccountEmail,
53+
const task = {
54+
httpRequest: {
55+
httpMethod: 'POST',
56+
url,
57+
oidcToken: {
58+
serviceAccountEmail,
59+
},
5960
},
60-
},
61-
};
62-
63-
if (payload) {
64-
task.httpRequest.body = Buffer.from(payload).toString('base64');
65-
}
66-
67-
if (inSeconds) {
68-
// The time when the task is scheduled to be attempted.
69-
task.scheduleTime = {
70-
seconds: inSeconds + Date.now() / 1000,
7161
};
72-
}
7362

74-
console.log('Sending task:');
75-
console.log(task);
76-
// Send create task request.
77-
const request = {parent, task};
78-
const [response] = await client.createTask(request);
79-
const name = response.name;
80-
console.log(`Created task ${name}`);
63+
if (payload) {
64+
task.httpRequest.body = Buffer.from(payload).toString('base64');
65+
}
8166

67+
if (inSeconds) {
68+
// The time when the task is scheduled to be attempted.
69+
task.scheduleTime = {
70+
seconds: inSeconds + Date.now() / 1000,
71+
};
72+
}
73+
74+
console.log('Sending task:');
75+
console.log(task);
76+
// Send create task request.
77+
const request = {parent, task};
78+
const [response] = await client.createTask(request);
79+
const name = response.name;
80+
console.log(`Created task ${name}`);
81+
}
82+
createHttpTaskWithToken();
8283
// [END cloud_tasks_create_http_task_with_token]
8384
}
8485

85-
createHttpTaskWithToken(...process.argv.slice(2)).catch(console.error);
86+
process.on('unhandledRejection', err => {
87+
console.error(err.message);
88+
process.exitCode = 1;
89+
});
90+
91+
main(...process.argv.slice(2));

cloud-tasks/createQueue.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,46 @@
1414

1515
'use strict';
1616

17-
// [START cloud_tasks_create_queue]
1817
/**
1918
* Create a new Task Queue
2019
*/
21-
async function createQueue(
20+
function main(
2221
project = 'my-project-id', // Your GCP Project id
2322
queue = 'my-appengine-queue', // Name of the Queue to create
2423
location = 'us-central1' // The GCP region in which to create the queue
2524
) {
25+
// [START cloud_tasks_create_queue]
2626
// Imports the Google Cloud Tasks library.
2727
const cloudTasks = require('@google-cloud/tasks');
2828

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

32-
// Send create queue request.
33-
const [response] = await client.createQueue({
34-
// The fully qualified path to the location where the queue is created
35-
parent: client.locationPath(project, location),
36-
queue: {
37-
// The fully qualified path to the queue
38-
name: client.queuePath(project, location, queue),
39-
appEngineHttpQueue: {
40-
appEngineRoutingOverride: {
41-
// The App Engine service that will receive the tasks.
42-
service: 'default',
32+
async function createQueue() {
33+
// Send create queue request.
34+
const [response] = await client.createQueue({
35+
// The fully qualified path to the location where the queue is created
36+
parent: client.locationPath(project, location),
37+
queue: {
38+
// The fully qualified path to the queue
39+
name: client.queuePath(project, location, queue),
40+
appEngineHttpQueue: {
41+
appEngineRoutingOverride: {
42+
// The App Engine service that will receive the tasks.
43+
service: 'default',
44+
},
4345
},
4446
},
45-
},
46-
});
47-
console.log(`Created queue ${response.name}`);
47+
});
48+
console.log(`Created queue ${response.name}`);
49+
}
50+
createQueue();
51+
// [END cloud_tasks_create_queue]
4852
}
49-
// [END cloud_tasks_create_queue]
5053

51-
const args = process.argv.slice(2);
52-
createQueue(...args).catch(console.error);
54+
process.on('unhandledRejection', err => {
55+
console.error(err.message);
56+
process.exitCode = 1;
57+
});
58+
59+
main(...process.argv.slice(2));

cloud-tasks/createTask.js

+39-33
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/
1716

1817
// sample-metadata:
1918
// title: Cloud Tasks Create App Engine Target
@@ -23,7 +22,7 @@
2322
/**
2423
* Create a task for a given queue with an arbitrary payload.
2524
*/
26-
async function createTask(
25+
function main(
2726
project = 'my-project-id', // Your GCP Project id
2827
queue = 'my-appengine-queue', // Name of your Queue
2928
location = 'us-central1', // The GCP region of your queue
@@ -38,43 +37,50 @@ async function createTask(
3837
// Instantiates a client.
3938
const client = new CloudTasksClient();
4039

41-
// TODO(developer): Uncomment these lines and replace with your values.
42-
// const project = 'my-project-id';
43-
// const queue = 'my-appengine-queue';
44-
// const location = 'us-central1';
45-
// const payload = 'Hello, World!';
40+
async function createTask() {
41+
// TODO(developer): Uncomment these lines and replace with your values.
42+
// const project = 'my-project-id';
43+
// const queue = 'my-appengine-queue';
44+
// const location = 'us-central1';
45+
// const payload = 'Hello, World!';
4646

47-
// Construct the fully qualified queue name.
48-
const parent = client.queuePath(project, location, queue);
47+
// Construct the fully qualified queue name.
48+
const parent = client.queuePath(project, location, queue);
4949

50-
const task = {
51-
appEngineHttpRequest: {
52-
httpMethod: 'POST',
53-
relativeUri: '/log_payload',
54-
},
55-
};
56-
57-
if (payload) {
58-
task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
59-
}
60-
61-
if (inSeconds) {
62-
// The time when the task is scheduled to be attempted.
63-
task.scheduleTime = {
64-
seconds: inSeconds + Date.now() / 1000,
50+
const task = {
51+
appEngineHttpRequest: {
52+
httpMethod: 'POST',
53+
relativeUri: '/log_payload',
54+
},
6555
};
66-
}
6756

68-
console.log('Sending task:');
69-
console.log(task);
70-
// Send create task request.
71-
const request = {parent, task};
72-
const [response] = await client.createTask(request);
73-
const name = response.name;
74-
console.log(`Created task ${name}`);
57+
if (payload) {
58+
task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
59+
}
7560

61+
if (inSeconds) {
62+
// The time when the task is scheduled to be attempted.
63+
task.scheduleTime = {
64+
seconds: inSeconds + Date.now() / 1000,
65+
};
66+
}
67+
68+
console.log('Sending task:');
69+
console.log(task);
70+
// Send create task request.
71+
const request = {parent, task};
72+
const [response] = await client.createTask(request);
73+
const name = response.name;
74+
console.log(`Created task ${name}`);
75+
}
76+
createTask();
7677
// [END cloud_tasks_appengine_create_task]
7778
// [END tasks_quickstart]
7879
}
7980

80-
createTask(...process.argv.slice(2)).catch(console.error);
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
86+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)