|
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"/> |
2 | 4 |
|
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) |
5 | 6 |
|
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] |
9 | 8 |
|
10 |
| -`createTask.js` is a simple command-line program to create tasks to be pushed to |
11 |
| -the App Engine app. |
12 | 9 |
|
13 |
| -`createHttpTask.js` is a simple command-line program to create tasks to be pushed to |
14 |
| -a HTTP endpoint. |
15 | 10 |
|
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 |
18 | 12 |
|
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) |
23 | 23 |
|
24 |
| -## Setup |
| 24 | +## Before you begin |
25 | 25 |
|
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). |
27 | 28 |
|
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 |
33 | 30 |
|
34 |
| - With `npm`: |
35 | 31 |
|
36 |
| - npm install |
37 | 32 |
|
38 |
| - or with `yarn`: |
| 33 | +### Create Http Task |
39 | 34 |
|
40 |
| - yarn install |
| 35 | +View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/createHttpTask.js). |
41 | 36 |
|
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) |
43 | 38 |
|
44 |
| -To create a queue using the Cloud SDK, use the following gcloud command: |
| 39 | +__Usage:__ |
45 | 40 |
|
46 |
| - gcloud beta tasks queues create-app-engine-queue my-appengine-queue |
47 | 41 |
|
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` |
50 | 43 |
|
51 |
| -## Deploying the app to App Engine |
52 | 44 |
|
53 |
| -Deploy to App Engine Standard environment with gcloud: |
| 45 | +----- |
54 | 46 |
|
55 |
| - gcloud app deploy |
56 | 47 |
|
57 |
| -Verify the index page is serving: |
58 | 48 |
|
59 |
| - gcloud app browse |
60 | 49 |
|
61 |
| -## Run the Sample Using the Command Line |
| 50 | +### Create Http Task With Token |
62 | 51 |
|
63 |
| -Set environment variables: |
| 52 | +View the [source code](https://github.com/googleapis/nodejs-tasks/blob/master/samples/createHttpTaskWithToken.js). |
64 | 53 |
|
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) |
66 | 55 |
|
67 |
| -``` |
68 |
| -export PROJECT_ID=my-project-id |
69 |
| -``` |
| 56 | +__Usage:__ |
70 | 57 |
|
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`. |
73 | 58 |
|
74 |
| -``` |
75 |
| -export QUEUE_ID=my-appengine-queue |
76 |
| -``` |
| 59 | +`node createHttpTaskWithToken.js` |
77 | 60 |
|
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"). |
83 | 61 |
|
84 |
| -``` |
85 |
| -export LOCATION_ID=us-central1 |
86 |
| -``` |
| 62 | +----- |
87 | 63 |
|
88 |
| -### Using App Engine Queues |
89 |
| -Running the sample will create a task, targeted at the `/log_payload` |
90 |
| -endpoint, with a payload specified: |
91 | 64 |
|
92 |
| -``` |
93 |
| -node createTask.js $PROJECT_ID $LOCATION_ID $QUEUE_ID hello |
94 |
| -``` |
95 | 65 |
|
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: |
99 | 66 |
|
100 |
| - gcloud app logs read |
| 67 | +### Create Queue |
101 | 68 |
|
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). |
104 | 70 |
|
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) |
108 | 72 |
|
109 |
| -### Using HTTP Push Queues |
| 73 | +__Usage:__ |
110 | 74 |
|
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 |
| -``` |
116 | 75 |
|
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` |
119 | 77 |
|
120 |
| -``` |
121 |
| -node createHttpTask $PROJECT_ID $LOCATION_ID $QUEUE_ID $URL hello |
122 |
| -``` |
123 | 78 |
|
124 |
| -## More Info |
| 79 | +----- |
125 | 80 |
|
126 |
| -To get usage information: `node createTask.js --help` |
127 | 81 |
|
128 |
| -Which prints: |
129 | 82 |
|
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] |
139 | 83 |
|
140 |
| -Examples: |
141 |
| - node createTask.js --project my-project-id |
| 84 | +### Create Task |
142 | 85 |
|
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). |
145 | 87 |
|
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/ |
0 commit comments