Skip to content

Commit 28976dc

Browse files
averikitschAce Nassri
authored andcommitted
Update Tasks Samples for App Engine (#676)
* update license * updated with client library * passing tests * update env var for QUEUE * upgrade packages * update license * updated with client library * passing tests * update env var for QUEUE * upgrade packages
1 parent dfcd762 commit 28976dc

File tree

7 files changed

+144
-104
lines changed

7 files changed

+144
-104
lines changed

appengine/cloudtasks/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,51 @@ HTTP POST request and logs it. The log output can be viewed with:
8686

8787
## Running the Samples
8888

89+
Set environment variables:
90+
91+
First, your project ID:
92+
93+
```
94+
export PROJECT_ID=my-project-id
95+
```
96+
97+
Then the queue ID, as specified at queue creation time. Queue IDs already
98+
created can be listed with `gcloud alpha tasks queues list`.
99+
100+
```
101+
export QUEUE_ID=my-appengine-queue
102+
```
103+
104+
And finally the location ID, which can be discovered with
105+
`gcloud alpha tasks queues describe $QUEUE_ID`, with the location embedded in
106+
the "name" value (for instance, if the name is
107+
"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the
108+
location is "us-central1").
109+
110+
```
111+
export LOCATION_ID=us-central1
112+
```
113+
114+
Create a task, targeted at the `log_payload` endpoint, with a payload specified:
115+
116+
```
117+
node createTask.js --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello
118+
```
119+
120+
Now view that the payload was received and verify the payload:
121+
122+
```
123+
gcloud app logs read
124+
```
125+
126+
Create a task that will be scheduled for a time in the future using the
127+
`--in_seconds` flag:
128+
129+
```
130+
node createTask.js --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello --in_seconds=30
131+
```
132+
133+
89134
To get usage information: `node createTask.js --help`
90135

91136
Which prints:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1+
# Copyright 2018, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
114
env: flex
215
runtime: nodejs

appengine/cloudtasks/app.standard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2017, Google, Inc.
1+
# Copyright 2018, Google, Inc.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

appengine/cloudtasks/createTask.js

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
/**
2-
* Copyright 2017, Google, Inc.
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-
*/
2+
* Copyright 2018, Google, Inc.
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+
*/
1515

1616
'use strict';
1717

18-
const {google} = require('googleapis');
19-
const cloudtasks = google.cloudtasks('v2beta2');
20-
2118
/**
22-
* Create a task for a given queue with an arbitrary payload.
23-
*/
19+
* Create a task for a given queue with an arbitrary payload.
20+
*/
2421
function createTask (project, location, queue, options) {
2522
// [START cloud_tasks_appengine_create_task]
26-
google.auth.getClient({
27-
scopes: ['https://www.googleapis.com/auth/cloud-platform']
28-
}).then(authClient => {
29-
const task = {
30-
app_engine_http_request: {
31-
http_method: 'POST',
32-
relative_url: '/log_payload'
33-
}
34-
};
23+
// Imports the Google Cloud Tasks library.
24+
const cloudTasks = require('@google-cloud/tasks');
3525

36-
if (options.payload !== undefined) {
37-
task.app_engine_http_request.payload = Buffer.from(options.payload).toString('base64');
38-
}
26+
// Instantiates a client.
27+
const client = new cloudTasks.CloudTasksClient();
3928

40-
if (options.inSeconds !== undefined) {
41-
task.schedule_time = (new Date(options.inSeconds * 1000 + Date.now())).toISOString();
29+
// Construct the fully qualified queue name.
30+
const parent = client.queuePath(project, location, queue);
31+
32+
const task = {
33+
appEngineHttpRequest: {
34+
httpMethod: 'POST',
35+
relativeUrl: '/log_payload'
4236
}
37+
};
38+
39+
if (options.payload !== undefined) {
40+
task.appEngineHttpRequest.payload = Buffer.from(options.payload).toString('base64');
41+
}
4342

44-
const request = {
45-
parent: `projects/${project}/locations/${location}/queues/${queue}`, // TODO: Update placeholder value.
46-
resource: {
47-
task: task
48-
},
49-
auth: authClient
43+
if (options.inSeconds !== undefined) {
44+
task.scheduleTime = {
45+
seconds: (options.inSeconds + Date.now() / 1000)
5046
};
47+
}
48+
49+
const request = {
50+
parent: parent,
51+
task: task
52+
};
5153

52-
console.log('Sending task %j', task);
53-
return cloudtasks.projects.locations.queues.tasks.create(request);
54-
}).then(response => {
55-
console.log('Created task.', response.name);
56-
console.log(JSON.stringify(response, null, 2));
57-
}).catch(console.error);
54+
console.log('Sending task %j', task);
55+
// Send create task request.
56+
client.createTask(request).then(response => {
57+
const task = response[0].name;
58+
console.log(`Created task ${task}`);
59+
}).catch(err => {
60+
console.error(`Error in createTask: ${err.message || err}`);
61+
});
5862
// [END cloud_tasks_appengine_create_task]
5963
}
6064

appengine/cloudtasks/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
"test": "repo-tools test run --cmd npm -- run all-test"
1919
},
2020
"dependencies": {
21-
"body-parser": "1.18.2",
22-
"express": "4.16.2",
23-
"googleapis": "29.0.0",
21+
"@google-cloud/tasks": "^0.1.0",
22+
"body-parser": "^1.18.3",
23+
"express": "4.16.3",
2424
"yargs": "11.0.0"
2525
},
2626
"devDependencies": {
27-
"@google-cloud/nodejs-repo-tools": "2.2.1",
28-
"proxyquire": "2.0.0",
29-
"sinon": "4.4.2"
27+
"@google-cloud/nodejs-repo-tools": "2.3.0",
28+
"proxyquire": "2.0.1",
29+
"sinon": "6.0.1"
3030
},
3131
"cloud-repo-tools": {
3232
"requiresKeyFile": true,
3333
"requiresProjectId": true,
3434
"test": {
3535
"app": {
36-
"msg": "Hello, world!",
36+
"msg": "Hello, World!",
3737
"args": [
3838
"server.js"
3939
]

appengine/cloudtasks/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, Google, Inc.
2+
* Copyright 2018, Google, Inc.
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -28,7 +28,7 @@ app.use(bodyParser.text());
2828

2929
app.get('/', (req, res, next) => {
3030
// Basic index to verify app is serving
31-
res.send('Hello, world!').end();
31+
res.send('Hello, World!').end();
3232
});
3333

3434
app.post('/log_payload', (req, res, next) => {
Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,39 @@
11
/**
2-
* Copyright 2017, Google, Inc.
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-
*/
2+
* Copyright 2018, Google, Inc.
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+
*/
1515

1616
'use strict';
1717

18-
const proxyquire = require(`proxyquire`).noCallThru();
19-
const sinon = require(`sinon`);
18+
const path = require(`path`);
2019
const test = require(`ava`);
2120
const tools = require(`@google-cloud/nodejs-repo-tools`);
2221

23-
test.before(tools.stubConsole);
24-
test.after.always(tools.restoreConsole);
22+
const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);
2523

26-
test.cb(`should create a task`, (t) => {
27-
const responseMock = {
28-
name: 'foo'
29-
};
30-
const cloudtasksMock = {
31-
projects: {
32-
locations: {
33-
queues: {
34-
tasks: {
35-
create: sinon.stub().yields(responseMock)
36-
}
37-
}
38-
}
39-
}
40-
};
41-
const authClientMock = {};
24+
const PROJECT_ID = process.env.GCLOUD_PROJECT;
25+
const QUEUE = process.env.QUEUE_ID || 'my-appengine-queue';
26+
const cmd = `node createTask.js`;
27+
const cwd = path.join(__dirname, `..`);
4228

43-
const util = proxyquire(`../createTask`, {
44-
googleapis: {
45-
google: {
46-
cloudtasks: sinon.stub().returns(cloudtasksMock),
47-
auth: {
48-
getApplicationDefault: sinon.stub().yields(null, authClientMock)
49-
}
50-
}
51-
}
52-
});
53-
54-
util.createTask('p', 'l', 'q', {});
29+
test.before((t) => {
30+
if (!QUEUE) {
31+
t.fail(`You must set the QUEUE_ID environment variable!`);
32+
}
33+
});
34+
test.before(tools.checkCredentials);
5535

56-
setTimeout(() => {
57-
t.true(console.log.called);
58-
t.is(cloudtasksMock.projects.locations.queues.tasks.create.callCount, 1);
59-
t.end();
60-
}, 500);
36+
test.serial(`should create a task`, async (t) => {
37+
const output = await runAsync(`${cmd} --project=${PROJECT_ID} --location=us-central1 --queue=${QUEUE}`, cwd);
38+
t.true(output.includes('Created task'));
6139
});

0 commit comments

Comments
 (0)