Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Tasks samples. #516

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions appengine/cloudtasks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Node.js Google Cloud Tasks sample for Google App Engine

This sample demonstrates how to use [Google Cloud Tasks](https://cloud.google.com/cloud-tasks/)
on [Google App Engine Flexible Environment](https://cloud.google.com/appengine/docs/flexible/nodejs).

App Engine queues push tasks to an App Engine HTTP target. This directory
contains both the App Engine app to deploy, as well as the snippets to run
locally to push tasks to it, which could also be called on App Engine.

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

`server.js` is the main App Engine app. This app serves as an endpoint to
receive App Engine task attempts.

`app.yaml` configures the App Engine app.

* [Setup](#setup)
* [Running locally](#running-locally)
* [Deploying to App Engine](#deploying-to-app-engine)
* [Running the tests](#running-the-tests)

## Setup

Before you can run or deploy the sample, you need to do the following:

1. Refer to the [appengine/README.md][readme] file for instructions on
running and deploying.
1. Enable the Cloud Tasks API in the [Google Cloud Console](https://cloud.google.com/apis/library/cloudtasks.googleapis.com).
1. Install dependencies:

With `npm`:

npm install

or with `yarn`:

yarn install

## Creating a queue

To create a queue using the Cloud SDK, use the following gcloud command:

gcloud alpha tasks queues create-app-engine-queue my-appengine-queue

Note: A newly created queue will route to the default App Engine service and
version unless configured to do otherwise. Read the online help for the
`create-app-engine-queue` or the `update-app-engine-queue` commands to learn
about routing overrides for App Engine queues.

## Deploying the App Engine app

Deploy the App Engine app with gcloud:

gcloud app deploy

Verify the index page is serving:

gcloud app browse

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

gcloud app logs read

## Running the Samples

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

Which prints:

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

Examples:
node createTask.js --project my-project-id

For more information, see https://cloud.google.com/cloud-tasks
```
2 changes: 2 additions & 0 deletions appengine/cloudtasks/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env: flex
runtime: nodejs
129 changes: 129 additions & 0 deletions appengine/cloudtasks/createTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Copyright 2017, Google, Inc.
* 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
*
* http://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 google = require('googleapis');
const cloudtasks = google.cloudtasks('v2beta2');

function authorize (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
console.error('authentication failed: ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
authClient = authClient.createScoped(scopes);
}
callback(authClient);
});
}

/**
* Create a task for a given queue with an arbitrary payload.
*/
function createTask (project, location, queue, options) {
authorize((authClient) => {
const task = {
app_engine_http_request: {
http_method: 'POST',
relative_url: '/log_payload'
}
};

if (options.payload !== undefined) {
task.app_engine_http_request.payload = Buffer.from(options.payload).toString('base64');
}

if (options.inSeconds !== undefined) {
task.schedule_time = (new Date(options.inSeconds * 1000 + Date.now())).toISOString();
}

const request = {
parent: `projects/${project}/locations/${location}/queues/${queue}`, // TODO: Update placeholder value.
resource: {
task: task
},
auth: authClient
};

console.log('Sending task %j', task);

cloudtasks.projects.locations.queues.tasks.create(request, (err, response) => {
if (err) {
console.error(err);
return;
}

console.log('Created task.', response.name);
console.log(JSON.stringify(response, null, 2));
});
});
}

const cli = require(`yargs`)
.options({
location: {
alias: 'l',
description: 'Location of the queue to add the task to.',
type: 'string',
requiresArg: true,
required: true
},
queue: {
alias: 'q',
description: 'ID (short name) of the queue to add the task to.',
type: 'string',
requiresArg: true,
required: true
},
project: {
alias: 'p',
description: 'Project of the queue to add the task to.',
default: process.env.GCLOUD_PROJECT,
type: 'string',
requiresArg: true,
required: true
},
payload: {
alias: 'd',
description: '(Optional) Payload to attach to the push queue.',
type: 'string',
requiresArg: true
},
inSeconds: {
alias: 's',
description: '(Optional) The number of seconds from now to schedule task attempt.',
type: 'number',
requiresArg: true
}
})
.example(`node $0 --project my-project-id`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/cloud-tasks`)
.strict();

if (module === require.main) {
const opts = cli.help().parse(process.argv.slice(2));

process.env.GCLOUD_PROJECT = opts.project;

createTask(opts.project, opts.location, opts.queue, opts);
}

exports.authorize = authorize;
exports.createTask = createTask;
45 changes: 45 additions & 0 deletions appengine/cloudtasks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "appengine-cloudtasks",
"description": "Google App Engine Flexible Environment Cloud Tasks example.",
"version": "0.0.0",
"license": "Apache-2.0",
"author": "Google Inc.",
"private": true,
"repository": "GoogleCloudPlatform/nodejs-docs-samples",
"engines": {
"node": ">=4.0.0"
},
"scripts": {
"deploy": "gcloud app deploy",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"unit-test": "ava --verbose test/*.test.js",
"system-test": "repo-tools test app --config package.json --config-key cloud-repo-tools",
"all-test": "npm run unit-test && npm run system-test",
"test": "repo-tools test run --cmd npm -- run all-test",
"e2e-test": "repo-tools test deploy --config package.json --config-key cloud-repo-tools"
},
"dependencies": {
"body-parser": "1.18.2",
"express": "4.16.2",
"googleapis": "22.2.0",
"yargs": "10.0.3"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "2.1.1",
"proxyquire": "1.8.0",
"sinon": "4.0.2"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"test": {
"app": {
"msg": "Hello, world!",
"args": [
"server.js"
]
}
}
}
}
47 changes: 47 additions & 0 deletions appengine/cloudtasks/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2017, Google, Inc.
* 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
*
* http://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 bodyParser = require('body-parser');
const express = require('express');

const app = express();
app.enable('trust proxy');

app.use(bodyParser.raw());
app.use(bodyParser.json());
app.use(bodyParser.text());

app.get('/', (req, res, next) => {
// Basic index to verify app is serving
res.send('Hello, world!').end();
});

app.post('/log_payload', (req, res, next) => {
// Log the request payload
console.log('Received task with payload: %s', req.body);
res.send(`Printed task payload: ${req.body}`).end();
});

app.get('*', (req, res) => {
res.send('OK').end();
});

const PORT = process.env.PORT || 8080;
app.listen(process.env.PORT || 8080, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
59 changes: 59 additions & 0 deletions appengine/cloudtasks/test/createTask.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2017, Google, Inc.
* 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
*
* http://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 proxyquire = require(`proxyquire`).noCallThru();
const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);

test.cb(`should create a task`, (t) => {
const responseMock = {
name: 'foo'
};
const cloudtasksMock = {
projects: {
locations: {
queues: {
tasks: {
create: sinon.stub().yields(responseMock)
}
}
}
}
};
const authClientMock = {};

const util = proxyquire(`../createTask`, {
googleapis: {
cloudtasks: sinon.stub().returns(cloudtasksMock),
auth: {
getApplicationDefault: sinon.stub().yields(null, authClientMock)
}
}
});

util.createTask('p', 'l', 'q', {});

setTimeout(() => {
t.true(console.log.called);
t.is(cloudtasksMock.projects.locations.queues.tasks.create.callCount, 1);
t.end();
}, 500);
});
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ deployment:
- node scripts/build "auth"
- node scripts/build "appengine/pubsub"
- node scripts/build "bigquery"
- export GCP_QUEUE=nodejs-test-queue-do-not-delete; node scripts/build "cloudtasks"
- node scripts/build "containerengine/hello-world"
- node scripts/build "datastore"
- node scripts/build "debugger"
Expand Down
Loading