From 34864b3ee99b1201e7bcc2c930037953247f31e9 Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Wed, 10 Apr 2019 14:18:45 -0700 Subject: [PATCH] docs(samples): Add HTTP Push Queue Sample (#199) --- cloud-tasks/README.md | 42 +++++++---- cloud-tasks/{app.flexible.yaml => app.yaml} | 5 +- cloud-tasks/createHttpTask.js | 78 +++++++++++++++++++++ cloud-tasks/createTask.js | 75 +++----------------- cloud-tasks/package.json | 3 +- cloud-tasks/server.js | 2 +- cloud-tasks/test/test.samples.js | 10 ++- 7 files changed, 133 insertions(+), 82 deletions(-) rename cloud-tasks/{app.flexible.yaml => app.yaml} (90%) create mode 100644 cloud-tasks/createHttpTask.js diff --git a/cloud-tasks/README.md b/cloud-tasks/README.md index 9ab5442f27..213e4f681f 100644 --- a/cloud-tasks/README.md +++ b/cloud-tasks/README.md @@ -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) @@ -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: @@ -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 @@ -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://.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` @@ -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 diff --git a/cloud-tasks/app.flexible.yaml b/cloud-tasks/app.yaml similarity index 90% rename from cloud-tasks/app.flexible.yaml rename to cloud-tasks/app.yaml index 4b39439b00..24d25d8e52 100644 --- a/cloud-tasks/app.flexible.yaml +++ b/cloud-tasks/app.yaml @@ -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 @@ -11,5 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -env: flex -runtime: nodejs +runtime: nodejs10 diff --git a/cloud-tasks/createHttpTask.js b/cloud-tasks/createHttpTask.js new file mode 100644 index 0000000000..d4700aaa59 --- /dev/null +++ b/cloud-tasks/createHttpTask.js @@ -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://.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); diff --git a/cloud-tasks/createTask.js b/cloud-tasks/createTask.js index 018e273581..416c863398 100644 --- a/cloud-tasks/createTask.js +++ b/cloud-tasks/createTask.js @@ -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 @@ -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'; @@ -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, }; } @@ -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; @@ -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); diff --git a/cloud-tasks/package.json b/cloud-tasks/package.json index d0a4d8e68b..6882eb404d 100644 --- a/cloud-tasks/package.json +++ b/cloud-tasks/package.json @@ -8,7 +8,8 @@ "node": ">=8" }, "scripts": { - "test": "mocha" + "test": "mocha", + "start": "node server.js" }, "dependencies": { "@google-cloud/tasks": "^0.5.0", diff --git a/cloud-tasks/server.js b/cloud-tasks/server.js index 804f3840c7..5812013a48 100644 --- a/cloud-tasks/server.js +++ b/cloud-tasks/server.js @@ -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 diff --git a/cloud-tasks/test/test.samples.js b/cloud-tasks/test/test.samples.js index 8d3fd0f6d8..14904947da 100644 --- a/cloud-tasks/test/test.samples.js +++ b/cloud-tasks/test/test.samples.js @@ -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', () => { @@ -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/); });