Skip to content

Commit

Permalink
docs(samples): Add HTTP Push Queue Sample (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch authored and JustinBeckwith committed Apr 10, 2019
1 parent 2539f9e commit 34864b3
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 82 deletions.
42 changes: 30 additions & 12 deletions cloud-tasks/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Node.js Google Cloud Tasks sample for Google App Engine

This sample application shows how to use [Google Cloud Tasks](https://cloud.google.com/cloud-tasks/)
on Google App Engine [flexible environment][appengine-flex].
on [Google App Engine][appengine].

App Engine queues push tasks to an App Engine HTTP target. This directory
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.

`createHttpTask.js` is a simple command-line program to create tasks to be pushed to
a HTTP endpoint.

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

`app.flexible.yaml` configures the app for App Engine Node.js flexible
environment.

* [Setup](#setup)
* [Running locally](#running-locally)
* [Deploying to App Engine](#deploying-to-app-engine)
Expand Down Expand Up @@ -48,11 +48,11 @@ To create a queue using the Cloud SDK, use the following gcloud command:
Note: A newly created queue will route to the default App Engine service and
version unless configured to do otherwise.

## Deploying the app to App Engine flexible environment
## Deploying the app to App Engine

Deploy the App Engine app with gcloud:
Deploy to App Engine Standard environment with gcloud:

gcloud app deploy app.flexible.yaml
gcloud app deploy

Verify the index page is serving:

Expand Down Expand Up @@ -85,10 +85,12 @@ location is "us-central1").
export LOCATION_ID=us-central1
```

Create a task, targeted at the `log_payload` endpoint, with a payload specified:
### Using App Engine Queues
Running the sample will create a task, targeted at the `/log_payload`
endpoint, with a payload specified:

```
node createTask.js --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello
node createTask.js $PROJECT_ID $LOCATION_ID $QUEUE_ID hello
```

The App Engine app serves as a target for the push requests. It has an
Expand All @@ -101,9 +103,25 @@ Create a task that will be scheduled for a time in the future using the
`--in_seconds` flag:

```
node createTask.js --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --payload=hello --in_seconds=30
node createTask.js $PROJECT_ID $LOCATION_ID $QUEUE_ID hello 30
```

### Using HTTP Push Queues

Set an environment variable for the endpoint to your task handler. This is an
example url to send requests to the App Engine task handler:
```
export URL=https://<project_id>.appspot.com/log_payload
```

Running the sample will create a task and send the task to the specific URL
endpoint, with a payload specified:

```
node createHttpTask $PROJECT_ID $LOCATION_ID $QUEUE_ID $URL hello
```

## More Info

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

Expand All @@ -125,5 +143,5 @@ Examples:
For more information, see https://cloud.google.com/cloud-tasks
```

[appengine-flex]: https://cloud.google.com/appengine/docs/flexible/nodejs
[appengine]: https://cloud.google.com/appengine/docs/nodejs
[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs
5 changes: 2 additions & 3 deletions cloud-tasks/app.flexible.yaml → cloud-tasks/app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018, Google, Inc.
# Copyright 2019 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
Expand All @@ -11,5 +11,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

env: flex
runtime: nodejs
runtime: nodejs10
78 changes: 78 additions & 0 deletions cloud-tasks/createHttpTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright 2019 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
*
* 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';

/**
* Create a task with an HTTP target for a given queue with an arbitrary payload.
*/
async function createHttpTask(
project,
location,
queue,
url,
payload,
inSeconds
) {
// [START cloud_tasks_create_http_task]
// Imports the Google Cloud Tasks library.
const {v2beta3} = require('@google-cloud/tasks');

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

// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
// const queue = 'my-appengine-queue';
// const location = 'us-central1';
// const url = 'https://<project-id>.appspot.com/log_payload'
// const options = {payload: 'hello'};

// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);

const task = {
httpRequest: {
httpMethod: 'POST',
url, //The full url path that the request will be sent to.
},
};

if (payload) {
task.httpRequest.body = Buffer.from(payload).toString('base64');
}

if (inSeconds) {
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}

const request = {
parent: parent,
task: task,
};

console.log('Sending task:');
console.log(task);
// Send create task request.
const [response] = await client.createTask(request);
const name = response.name;
console.log(`Created task ${name}`);

// [END cloud_tasks_create_http_task]
}

createHttpTask(...process.argv.slice(2)).catch(console.error);
75 changes: 11 additions & 64 deletions cloud-tasks/createTask.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018, Google, Inc.
* Copyright 2019 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
Expand All @@ -18,14 +18,14 @@
/**
* Create a task for a given queue with an arbitrary payload.
*/
async function createTask(project, location, queue, options) {
async function createTask(project, location, queue, payload, inSeconds) {
// [START cloud_tasks_appengine_create_task]
// [START tasks_quickstart]
// Imports the Google Cloud Tasks library.
const cloudTasks = require('@google-cloud/tasks');
const {CloudTasksClient} = require('@google-cloud/tasks');

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

// TODO(developer): Uncomment these lines and replace with your values.
// const project = 'my-project-id';
Expand All @@ -43,15 +43,13 @@ async function createTask(project, location, queue, options) {
},
};

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

if (options.inSeconds !== undefined) {
if (inSeconds) {
task.scheduleTime = {
seconds: options.inSeconds + Date.now() / 1000,
seconds: inSeconds + Date.now() / 1000,
};
}

Expand All @@ -60,7 +58,8 @@ async function createTask(project, location, queue, options) {
task: task,
};

console.log('Sending task %j', task);
console.log('Sending task:');
console.log(task);
// Send create task request.
const [response] = await client.createTask(request);
const name = response.name;
Expand All @@ -70,56 +69,4 @@ async function createTask(project, location, queue, options) {
// [END tasks_quickstart]
}

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).catch(
console.error
);
}

exports.createTask = createTask;
createTask(...process.argv.slice(2)).catch(console.error);
3 changes: 2 additions & 1 deletion cloud-tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"node": ">=8"
},
"scripts": {
"test": "mocha"
"test": "mocha",
"start": "node server.js"
},
"dependencies": {
"@google-cloud/tasks": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion cloud-tasks/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018, Google, Inc.
* Copyright 2019 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
Expand Down
10 changes: 9 additions & 1 deletion cloud-tasks/test/test.samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const PROJECT_ID = process.env.GCLOUD_PROJECT;
const queueName = `gcloud-${uuid.v4().split('-')[0]}`;
const URL = `https://${PROJECT_ID}.appspot.com/log_payload`;

describe('Cloud Task Sample Tests', () => {
it('should create a queue', () => {
Expand All @@ -31,7 +32,14 @@ describe('Cloud Task Sample Tests', () => {

it('should create a task', () => {
const stdout = execSync(
`node createTask --project=${PROJECT_ID} --location=us-central1 --queue=${queueName}`
`node createTask ${PROJECT_ID} us-central1 ${queueName}`
);
assert.match(stdout, /Created task/);
});

it('should create a HTTP task', () => {
const stdout = execSync(
`node createHttpTask ${PROJECT_ID} us-central1 my-appengine-queue ${URL}`
);
assert.match(stdout, /Created task/);
});
Expand Down

0 comments on commit 34864b3

Please sign in to comment.