Skip to content

Commit 9320185

Browse files
JustinBeckwithgrayside
authored andcommitted
samples: add queue create and delete samples (#142)
1 parent d8032de commit 9320185

File tree

6 files changed

+137
-62
lines changed

6 files changed

+137
-62
lines changed

cloud-tasks/createQueue.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
/**
18+
* Create a new Task Queue
19+
*/
20+
async function createQueue(
21+
project = 'my-project-id', // Your GCP Project id
22+
queue = 'my-appengine-queue', // Name of the Queue to create
23+
location = 'us-central1' // The GCP region in which to create the queue
24+
) {
25+
// Imports the Google Cloud Tasks library.
26+
const cloudTasks = require('@google-cloud/tasks');
27+
28+
// Instantiates a client.
29+
const client = new cloudTasks.CloudTasksClient();
30+
31+
// Send create queue request.
32+
const [response] = await client.createQueue({
33+
// The fully qualified path to the location where the queue is created
34+
parent: client.locationPath(project, location),
35+
queue: {
36+
// The fully qualified path to the queue
37+
name: client.queuePath(project, location, queue),
38+
appEngineHttpQueue: {
39+
appEngineRoutingOverride: {
40+
// The App Engine service that will receive the tasks.
41+
service: 'default',
42+
},
43+
},
44+
},
45+
});
46+
console.log(`Created queue ${response.name}`);
47+
}
48+
49+
const args = process.argv.slice(2);
50+
createQueue(...args).catch(console.error);

cloud-tasks/deleteQueue.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
/**
18+
* Delete a given Queue
19+
*/
20+
async function deleteQueue(
21+
project = 'my-project-id', // Your GCP Project id
22+
queue = 'my-appengine-queue', // Name of the Queue to delete
23+
location = 'us-central1' // The GCP region in which to delete the queue
24+
) {
25+
// Imports the Google Cloud Tasks library.
26+
const cloudTasks = require('@google-cloud/tasks');
27+
28+
// Instantiates a client.
29+
const client = new cloudTasks.CloudTasksClient();
30+
31+
// Get the fully qualified path to the queue
32+
const name = client.queuePath(project, location, queue);
33+
34+
// Send delete queue request.
35+
await client.deleteQueue({name});
36+
console.log(`Deleted queue '${queue}'.`);
37+
}
38+
39+
const args = process.argv.slice(2);
40+
deleteQueue(...args).catch(console.error);

cloud-tasks/package.json

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
{
22
"name": "appengine-cloudtasks",
33
"description": "Google App Engine Cloud Tasks example.",
4-
"version": "0.2.0",
54
"license": "Apache-2.0",
65
"author": "Google Inc.",
76
"private": true,
87
"engines": {
98
"node": ">=8"
109
},
1110
"scripts": {
12-
"unit-test": "mocha system-test/*.test.js --timeout=600000",
13-
"system-test": "repo-tools test app --config package.json --config-key cloud-repo-tools",
14-
"test": "npm run unit-test && npm run system-test"
11+
"test": "mocha system-test"
1512
},
1613
"dependencies": {
1714
"@google-cloud/tasks": "^0.2.2",
@@ -20,21 +17,8 @@
2017
"yargs": "^12.0.1"
2118
},
2219
"devDependencies": {
23-
"@google-cloud/nodejs-repo-tools": "^3.0.0",
20+
"execa": "^1.0.0",
2421
"mocha": "^5.2.0",
25-
"proxyquire": "^2.0.1",
26-
"sinon": "^7.0.0"
27-
},
28-
"cloud-repo-tools": {
29-
"requiresKeyFile": true,
30-
"requiresProjectId": true,
31-
"test": {
32-
"app": {
33-
"msg": "Hello, World!",
34-
"args": [
35-
"server.js"
36-
]
37-
}
38-
}
22+
"uuid": "^3.3.2"
3923
}
4024
}

cloud-tasks/system-test/.eslintrc.yml

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ env:
33
mocha: true
44
rules:
55
node/no-unpublished-require: off
6-
no-empty: off

cloud-tasks/system-test/createTask.test.js

-42
This file was deleted.
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
const path = require('path');
18+
const assert = require('assert');
19+
const execa = require('execa');
20+
const uuid = require('uuid');
21+
22+
const PROJECT_ID = process.env.GCLOUD_PROJECT;
23+
const queueName = `gcloud-${uuid.v4().split('-')[0]}`;
24+
const cwd = path.join(__dirname, '..');
25+
const exec = cmd => execa.shell(cmd, {cwd});
26+
27+
describe('Cloud Task Sample Tests', () => {
28+
it('should create a queue', async () => {
29+
const {stdout} = await exec(`node createQueue ${PROJECT_ID} ${queueName}`);
30+
assert.ok(stdout.includes('Created queue'));
31+
});
32+
33+
it('should create a task', async () => {
34+
const {stdout} = await exec(
35+
`node createTask --project=${PROJECT_ID} --location=us-central1 --queue=${queueName}`
36+
);
37+
assert.ok(stdout.includes('Created task'));
38+
});
39+
40+
it('should delete a queue', async () => {
41+
const {stdout} = await exec(`node deleteQueue ${PROJECT_ID} ${queueName}`);
42+
assert.ok(stdout.includes('Deleted queue'));
43+
});
44+
});

0 commit comments

Comments
 (0)