Skip to content

Commit

Permalink
samples: add queue create and delete samples (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and grayside committed Nov 3, 2022
1 parent d8032de commit 9320185
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 62 deletions.
50 changes: 50 additions & 0 deletions cloud-tasks/createQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

/**
* Create a new Task Queue
*/
async function createQueue(
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of the Queue to create
location = 'us-central1' // The GCP region in which to create the queue
) {
// Imports the Google Cloud Tasks library.
const cloudTasks = require('@google-cloud/tasks');

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

// Send create queue request.
const [response] = await client.createQueue({
// The fully qualified path to the location where the queue is created
parent: client.locationPath(project, location),
queue: {
// The fully qualified path to the queue
name: client.queuePath(project, location, queue),
appEngineHttpQueue: {
appEngineRoutingOverride: {
// The App Engine service that will receive the tasks.
service: 'default',
},
},
},
});
console.log(`Created queue ${response.name}`);
}

const args = process.argv.slice(2);
createQueue(...args).catch(console.error);
40 changes: 40 additions & 0 deletions cloud-tasks/deleteQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

/**
* Delete a given Queue
*/
async function deleteQueue(
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of the Queue to delete
location = 'us-central1' // The GCP region in which to delete the queue
) {
// Imports the Google Cloud Tasks library.
const cloudTasks = require('@google-cloud/tasks');

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

// Get the fully qualified path to the queue
const name = client.queuePath(project, location, queue);

// Send delete queue request.
await client.deleteQueue({name});
console.log(`Deleted queue '${queue}'.`);
}

const args = process.argv.slice(2);
deleteQueue(...args).catch(console.error);
22 changes: 3 additions & 19 deletions cloud-tasks/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"name": "appengine-cloudtasks",
"description": "Google App Engine Cloud Tasks example.",
"version": "0.2.0",
"license": "Apache-2.0",
"author": "Google Inc.",
"private": true,
"engines": {
"node": ">=8"
},
"scripts": {
"unit-test": "mocha system-test/*.test.js --timeout=600000",
"system-test": "repo-tools test app --config package.json --config-key cloud-repo-tools",
"test": "npm run unit-test && npm run system-test"
"test": "mocha system-test"
},
"dependencies": {
"@google-cloud/tasks": "^0.2.2",
Expand All @@ -20,21 +17,8 @@
"yargs": "^12.0.1"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"execa": "^1.0.0",
"mocha": "^5.2.0",
"proxyquire": "^2.0.1",
"sinon": "^7.0.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"test": {
"app": {
"msg": "Hello, World!",
"args": [
"server.js"
]
}
}
"uuid": "^3.3.2"
}
}
1 change: 0 additions & 1 deletion cloud-tasks/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ env:
mocha: true
rules:
node/no-unpublished-require: off
no-empty: off
42 changes: 0 additions & 42 deletions cloud-tasks/system-test/createTask.test.js

This file was deleted.

44 changes: 44 additions & 0 deletions cloud-tasks/system-test/test.samples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const path = require('path');
const assert = require('assert');
const execa = require('execa');
const uuid = require('uuid');

const PROJECT_ID = process.env.GCLOUD_PROJECT;
const queueName = `gcloud-${uuid.v4().split('-')[0]}`;
const cwd = path.join(__dirname, '..');
const exec = cmd => execa.shell(cmd, {cwd});

describe('Cloud Task Sample Tests', () => {
it('should create a queue', async () => {
const {stdout} = await exec(`node createQueue ${PROJECT_ID} ${queueName}`);
assert.ok(stdout.includes('Created queue'));
});

it('should create a task', async () => {
const {stdout} = await exec(
`node createTask --project=${PROJECT_ID} --location=us-central1 --queue=${queueName}`
);
assert.ok(stdout.includes('Created task'));
});

it('should delete a queue', async () => {
const {stdout} = await exec(`node deleteQueue ${PROJECT_ID} ${queueName}`);
assert.ok(stdout.includes('Deleted queue'));
});
});

0 comments on commit 9320185

Please sign in to comment.