Skip to content

Commit c03a4c1

Browse files
authored
docs: generate docs for ga release (#221)
1 parent db3ab95 commit c03a4c1

File tree

3 files changed

+200
-100
lines changed

3 files changed

+200
-100
lines changed

cloud-tasks/README.md

+123-100
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,170 @@
1-
# Node.js Google Cloud Tasks sample for Google App Engine
1+
[//]: # "This README.md file is auto-generated, all changes to this file will be lost."
2+
[//]: # "To regenerate it, use `python -m synthtool`."
3+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
24

3-
This sample application shows how to use [Google Cloud Tasks](https://cloud.google.com/cloud-tasks/)
4-
on [Google App Engine][appengine].
5+
# [Cloud Tasks: Node.js Samples](https://github.com/googleapis/nodejs-tasks)
56

6-
This directory
7-
contains both the App Engine app to deploy, as well as the snippets to run
8-
locally to push tasks to it, which could also be called on App Engine.
7+
[![Open in Cloud Shell][shell_img]][shell_link]
98

10-
`createTask.js` is a simple command-line program to create tasks to be pushed to
11-
the App Engine app.
129

13-
`createHttpTask.js` is a simple command-line program to create tasks to be pushed to
14-
a HTTP endpoint.
1510

16-
`server.js` is the main App Engine app. This app serves as an endpoint to
17-
receive App Engine task attempts.
11+
## Table of Contents
1812

19-
* [Setup](#setup)
20-
* [Running locally](#running-locally)
21-
* [Deploying to App Engine](#deploying-to-app-engine)
22-
* [Running the tests](#running-the-tests)
13+
* [Before you begin](#before-you-begin)
14+
* [Samples](#samples)
15+
* [Create Http Task](#create-http-task)
16+
* [Create Http Task With Token](#create-http-task-with-token)
17+
* [Create Queue](#create-queue)
18+
* [Create Task](#create-task)
19+
* [Delete Queue](#delete-queue)
20+
* [List Queues](#list-queues)
21+
* [Quickstart](#quickstart)
22+
* [Server](#server)
2323

24-
## Setup
24+
## Before you begin
2525

26-
Before you can run or deploy the sample, you need to do the following:
26+
Before running the samples, make sure you've followed the steps outlined in
27+
[Using the client library](https://github.com/googleapis/nodejs-tasks#using-the-client-library).
2728

28-
1. Refer to the [appengine/README.md][readme] file for instructions on
29-
running and deploying.
30-
1. Enable the Cloud Tasks API in the [Google Cloud Console](https://console.cloud.google.com/apis/api/tasks.googleapis.com).
31-
1. Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).
32-
1. Install dependencies:
29+
## Samples
3330

34-
With `npm`:
3531

36-
npm install
3732

38-
or with `yarn`:
33+
### Create Http Task
3934

40-
yarn install
35+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/createHttpTask.js).
4136

42-
## Creating a queue
37+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/createHttpTask.js,samples/README.md)
4338

44-
To create a queue using the Cloud SDK, use the following gcloud command:
39+
__Usage:__
4540

46-
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
4741

48-
Note: A newly created queue will route to the default App Engine service and
49-
version unless configured to do otherwise.
42+
`node createHttpTask.js`
5043

51-
## Deploying the app to App Engine
5244

53-
Deploy to App Engine Standard environment with gcloud:
45+
-----
5446

55-
gcloud app deploy
5647

57-
Verify the index page is serving:
5848

59-
gcloud app browse
6049

61-
## Run the Sample Using the Command Line
50+
### Create Http Task With Token
6251

63-
Set environment variables:
52+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/createHttpTaskWithToken.js).
6453

65-
First, your project ID:
54+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/createHttpTaskWithToken.js,samples/README.md)
6655

67-
```
68-
export PROJECT_ID=my-project-id
69-
```
56+
__Usage:__
7057

71-
Then the queue ID, as specified at queue creation time. Queue IDs already
72-
created can be listed with `gcloud beta tasks queues list`.
7358

74-
```
75-
export QUEUE_ID=my-appengine-queue
76-
```
59+
`node createHttpTaskWithToken.js`
7760

78-
And finally the location ID, which can be discovered with
79-
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
80-
the "name" value (for instance, if the name is
81-
"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the
82-
location is "us-central1").
8361

84-
```
85-
export LOCATION_ID=us-central1
86-
```
62+
-----
8763

88-
### Using App Engine Queues
89-
Running the sample will create a task, targeted at the `/log_payload`
90-
endpoint, with a payload specified:
9164

92-
```
93-
node createTask.js $PROJECT_ID $LOCATION_ID $QUEUE_ID hello
94-
```
9565

96-
The App Engine app serves as a target for the push requests. It has an
97-
endpoint `/log_payload` that reads the payload (i.e., the request body) of the
98-
HTTP POST request and logs it. The log output can be viewed with:
9966

100-
gcloud app logs read
67+
### Create Queue
10168

102-
Create a task that will be scheduled for a time in the future using the
103-
`--in_seconds` flag:
69+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/createQueue.js).
10470

105-
```
106-
node createTask.js $PROJECT_ID $LOCATION_ID $QUEUE_ID hello 30
107-
```
71+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/createQueue.js,samples/README.md)
10872

109-
### Using HTTP Push Queues
73+
__Usage:__
11074

111-
Set an environment variable for the endpoint to your task handler. This is an
112-
example url to send requests to the App Engine task handler:
113-
```
114-
export URL=https://<project_id>.appspot.com/log_payload
115-
```
11675

117-
Running the sample will create a task and send the task to the specific URL
118-
endpoint, with a payload specified:
76+
`node createQueue.js`
11977

120-
```
121-
node createHttpTask $PROJECT_ID $LOCATION_ID $QUEUE_ID $URL hello
122-
```
12378

124-
## More Info
79+
-----
12580

126-
To get usage information: `node createTask.js --help`
12781

128-
Which prints:
12982

130-
```
131-
Options:
132-
--version Show version number [boolean]
133-
--location, -l Location of the queue to add the task to. [string] [required]
134-
--queue, -q ID (short name) of the queue to add the task to. [string] [required]
135-
--project, -p Project of the queue to add the task to. [string] [required]
136-
--payload, -d (Optional) Payload to attach to the push queue. [string]
137-
--inSeconds, -s (Optional) The number of seconds from now to schedule task attempt. [number]
138-
--help Show help [boolean]
13983

140-
Examples:
141-
node createTask.js --project my-project-id
84+
### Create Task
14285

143-
For more information, see https://cloud.google.com/cloud-tasks
144-
```
86+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/createTask.js).
14587

146-
[appengine]: https://cloud.google.com/appengine/docs/nodejs
147-
[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs
88+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/createTask.js,samples/README.md)
89+
90+
__Usage:__
91+
92+
93+
`node createTask.js`
94+
95+
96+
-----
97+
98+
99+
100+
101+
### Delete Queue
102+
103+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/deleteQueue.js).
104+
105+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/deleteQueue.js,samples/README.md)
106+
107+
__Usage:__
108+
109+
110+
`node deleteQueue.js`
111+
112+
113+
-----
114+
115+
116+
117+
118+
### List Queues
119+
120+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/listQueues.js).
121+
122+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/listQueues.js,samples/README.md)
123+
124+
__Usage:__
125+
126+
127+
`node listQueues.js`
128+
129+
130+
-----
131+
132+
133+
134+
135+
### Quickstart
136+
137+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/quickstart.js).
138+
139+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/quickstart.js,samples/README.md)
140+
141+
__Usage:__
142+
143+
144+
`node quickstart.js`
145+
146+
147+
-----
148+
149+
150+
151+
152+
### Server
153+
154+
View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/server.js).
155+
156+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/server.js,samples/README.md)
157+
158+
__Usage:__
159+
160+
161+
`node server.js`
162+
163+
164+
165+
166+
167+
168+
[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
169+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-tasks&page=editor&open_in_editor=samples/README.md
170+
[product-docs]: https://cloud.google.com/tasks/docs/

cloud-tasks/quickstart.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
/**
19+
* Create a task for a given queue with an arbitrary payload.
20+
*/
21+
async function createTask(project, location, queue, payload, inSeconds) {
22+
// [START tasks_quickstart]
23+
// Imports the Google Cloud Tasks library.
24+
const {CloudTasksClient} = require('@google-cloud/tasks');
25+
26+
// Instantiates a client.
27+
const client = new CloudTasksClient();
28+
29+
// TODO(developer): Uncomment these lines and replace with your values.
30+
// const project = 'my-project-id';
31+
// const queue = 'my-appengine-queue';
32+
// const location = 'us-central1';
33+
// const options = {payload: 'hello'};
34+
35+
// Construct the fully qualified queue name.
36+
const parent = client.queuePath(project, location, queue);
37+
38+
const task = {
39+
appEngineHttpRequest: {
40+
httpMethod: 'POST',
41+
relativeUri: '/log_payload',
42+
},
43+
};
44+
45+
if (payload) {
46+
task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
47+
}
48+
49+
if (inSeconds) {
50+
task.scheduleTime = {
51+
seconds: inSeconds + Date.now() / 1000,
52+
};
53+
}
54+
55+
const request = {
56+
parent: parent,
57+
task: task,
58+
};
59+
60+
console.log('Sending task:');
61+
console.log(task);
62+
// Send create task request.
63+
const [response] = await client.createTask(request);
64+
const name = response.name;
65+
console.log(`Created task ${name}`);
66+
67+
// [END tasks_quickstart]
68+
}
69+
70+
createTask(...process.argv.slice(2)).catch(console.error);

cloud-tasks/test/test.samples.js

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ describe('Cloud Task Sample Tests', () => {
3939
assert.match(stdout, /Created task/);
4040
});
4141

42+
it('quickstart sample should create a task', () => {
43+
const stdout = execSync(
44+
`node quickstart ${PROJECT_ID} us-central1 ${queueName}`
45+
);
46+
assert.match(stdout, /Created task/);
47+
});
48+
4249
it('should create a HTTP task', () => {
4350
const stdout = execSync(
4451
`node createHttpTask ${PROJECT_ID} us-central1 my-appengine-queue ${URL}`

0 commit comments

Comments
 (0)