diff --git a/appengine/cloudtasks/README.md b/appengine/cloudtasks/README.md index ffacee89fe..b9b1c614c9 100644 --- a/appengine/cloudtasks/README.md +++ b/appengine/cloudtasks/README.md @@ -1,155 +1,5 @@ -# Node.js Google Cloud Tasks sample for Google App Engine +Samples for the [Google Cloud Tasks API Node.js Client][client] +have moved to [github.com/googleapis/nodejs-tasks/tree/master/samples/][samples]. -This sample application shows how to use [Google Cloud Tasks](https://cloud.google.com/cloud-tasks/) -on Google App Engine Node.js [standard environment][appengine-std] and [flexible environment][appengine-flex]. - -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.standard.yaml` configures the app for App Engine Node.js standard -environment. - -`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) -* [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://console.cloud.google.com/apis/api/tasks.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 beta 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 to App Engine standard environment - -Deploy the App Engine app with gcloud: - - gcloud app deploy app.standard.yaml - -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 - -## Deploying the app to App Engine flexible environment - -Deploy the App Engine app with gcloud: - - gcloud app deploy app.flexible.yaml - -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 - -Set environment variables: - -First, your project ID: - -``` -export PROJECT_ID=my-project-id -``` - -Then the queue ID, as specified at queue creation time. Queue IDs already -created can be listed with `gcloud beta tasks queues list`. - -``` -export QUEUE_ID=my-appengine-queue -``` - -And finally the location ID, which can be discovered with -`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in -the "name" value (for instance, if the name is -"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the -location is "us-central1"). - -``` -export LOCATION_ID=us-central1 -``` - -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 -``` - -Now view that the payload was received and verify the payload: - -``` -gcloud app logs read -``` - -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 -``` - - -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 -``` - -[appengine-flex]: https://cloud.google.com/appengine/docs/flexible/nodejs -[appengine-std]: https://cloud.google.com/appengine/docs/standard/nodejs +[client]: https://github.com/googleapis/nodejs-tasks +[samples]: https://github.com/googleapis/nodejs-tasks/tree/master/samples \ No newline at end of file diff --git a/appengine/cloudtasks/app.flexible.yaml b/appengine/cloudtasks/app.flexible.yaml deleted file mode 100644 index 4b39439b00..0000000000 --- a/appengine/cloudtasks/app.flexible.yaml +++ /dev/null @@ -1,15 +0,0 @@ -# Copyright 2018, 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. - -env: flex -runtime: nodejs diff --git a/appengine/cloudtasks/app.standard.yaml b/appengine/cloudtasks/app.standard.yaml deleted file mode 100644 index e938832ccb..0000000000 --- a/appengine/cloudtasks/app.standard.yaml +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright 2018, 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. - -# [START app_yaml] -runtime: nodejs8 -# [END app_yaml] diff --git a/appengine/cloudtasks/createTask.js b/appengine/cloudtasks/createTask.js deleted file mode 100644 index cc30c62f86..0000000000 --- a/appengine/cloudtasks/createTask.js +++ /dev/null @@ -1,114 +0,0 @@ -/** -* Copyright 2018, 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'; - -/** -* Create a task for a given queue with an arbitrary payload. -*/ -function createTask (project, location, queue, options) { - // [START cloud_tasks_appengine_create_task] - // Imports the Google Cloud Tasks library. - const cloudTasks = require('@google-cloud/tasks'); - - // Instantiates a client. - const client = new cloudTasks.CloudTasksClient(); - - // Construct the fully qualified queue name. - const parent = client.queuePath(project, location, queue); - - const task = { - appEngineHttpRequest: { - httpMethod: 'POST', - relativeUrl: '/log_payload' - } - }; - - if (options.payload !== undefined) { - task.appEngineHttpRequest.payload = Buffer.from(options.payload).toString('base64'); - } - - if (options.inSeconds !== undefined) { - task.scheduleTime = { - seconds: (options.inSeconds + Date.now() / 1000) - }; - } - - const request = { - parent: parent, - task: task - }; - - console.log('Sending task %j', task); - // Send create task request. - client.createTask(request).then(response => { - const task = response[0].name; - console.log(`Created task ${task}`); - }).catch(err => { - console.error(`Error in createTask: ${err.message || err}`); - }); - // [END cloud_tasks_appengine_create_task] -} - -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.createTask = createTask; diff --git a/appengine/cloudtasks/package.json b/appengine/cloudtasks/package.json deleted file mode 100644 index 5cfdf1a977..0000000000 --- a/appengine/cloudtasks/package.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "name": "appengine-cloudtasks", - "description": "Google App Engine Standard Environment Cloud Tasks example.", - "version": "0.0.0", - "license": "Apache-2.0", - "author": "Google Inc.", - "private": true, - "repository": "GoogleCloudPlatform/nodejs-docs-samples", - "engines": { - "node": "8.x.x" - }, - "scripts": { - "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" - }, - "dependencies": { - "@google-cloud/tasks": "^0.1.0", - "body-parser": "^1.18.3", - "express": "4.16.3", - "yargs": "11.0.0" - }, - "devDependencies": { - "@google-cloud/nodejs-repo-tools": "2.3.0", - "proxyquire": "2.0.1", - "sinon": "6.0.1" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true, - "test": { - "app": { - "msg": "Hello, World!", - "args": [ - "server.js" - ] - } - } - } -} diff --git a/appengine/cloudtasks/server.js b/appengine/cloudtasks/server.js deleted file mode 100644 index 4f110fbfdd..0000000000 --- a/appengine/cloudtasks/server.js +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2018, 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'; - -// [START cloud_tasks_appengine_quickstart] -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.'); -}); -// [END cloud_tasks_appengine_quickstart] diff --git a/appengine/cloudtasks/test/createTask.test.js b/appengine/cloudtasks/test/createTask.test.js deleted file mode 100644 index 697600a80b..0000000000 --- a/appengine/cloudtasks/test/createTask.test.js +++ /dev/null @@ -1,39 +0,0 @@ -/** -* Copyright 2018, 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 path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); - -const {runAsync} = require(`@google-cloud/nodejs-repo-tools`); - -const PROJECT_ID = process.env.GCLOUD_PROJECT; -const QUEUE = process.env.QUEUE_ID || 'my-appengine-queue'; -const cmd = `node createTask.js`; -const cwd = path.join(__dirname, `..`); - -test.before((t) => { - if (!QUEUE) { - t.fail(`You must set the QUEUE_ID environment variable!`); - } -}); -test.before(tools.checkCredentials); - -test.serial(`should create a task`, async (t) => { - const output = await runAsync(`${cmd} --project=${PROJECT_ID} --location=us-central1 --queue=${QUEUE}`, cwd); - t.true(output.includes('Created task')); -});