diff --git a/cloud-tasks/createHttpTask.js b/cloud-tasks/createHttpTask.js index c7683aae32..e4c42c8a60 100644 --- a/cloud-tasks/createHttpTask.js +++ b/cloud-tasks/createHttpTask.js @@ -18,17 +18,18 @@ // sample-metadata: // title: Cloud Tasks Create HTTP Target // description: Create Cloud Tasks with a HTTP Target +// usage: node createHttpTask.js /** * Create a task with an HTTP target for a given queue with an arbitrary payload. */ async function createHttpTask( - project, - location, - queue, - url, - payload, - inSeconds + project = 'my-project-id', // Your GCP Project id + queue = 'my-appengine-queue', // Name of your Queue + location = 'us-central1', // The GCP region of your queue + url = 'https://example.com/taskhandler', // The full url path that the request will be sent to + payload = 'Hello, World!', // The task HTTP request body + inSeconds = 0 // Delay in task execution ) { // [START cloud_tasks_create_http_task] // Imports the Google Cloud Tasks library. @@ -41,8 +42,8 @@ async function createHttpTask( // const project = 'my-project-id'; // const queue = 'my-queue'; // const location = 'us-central1'; - // const url = 'https://example.com/taskhandler' - // const payload = 'hello'; + // const url = 'https://example.com/taskhandler'; + // const payload = 'Hello, World!'; // Construct the fully qualified queue name. const parent = client.queuePath(project, location, queue); @@ -50,7 +51,7 @@ async function createHttpTask( const task = { httpRequest: { httpMethod: 'POST', - url, //The full url path that the request will be sent to. + url, }, }; @@ -59,6 +60,7 @@ async function createHttpTask( } if (inSeconds) { + // The time when the task is scheduled to be attempted. task.scheduleTime = { seconds: inSeconds + Date.now() / 1000, }; diff --git a/cloud-tasks/createHttpTaskWithToken.js b/cloud-tasks/createHttpTaskWithToken.js index 1107158181..8241ade3b7 100644 --- a/cloud-tasks/createHttpTaskWithToken.js +++ b/cloud-tasks/createHttpTaskWithToken.js @@ -18,18 +18,19 @@ // sample-metadata: // title: Cloud Tasks Create HTTP Target with Token // description: Create Cloud Tasks with a HTTP Target with Token +// usage: node createHttpTaskWithToken.js /** * Create a task with an HTTP target for a given queue with an arbitrary payload. */ async function createHttpTaskWithToken( - project, - location, - queue, - url, - email, - payload, - inSeconds + project = 'my-project-id', // Your GCP Project id + queue = 'my-queue', // Name of your Queue + location = 'us-central1', // The GCP region of your queue + url = 'https://example.com/taskhandler', // The full url path that the request will be sent to + email = 'client@.iam.gserviceaccount.com', // Cloud IAM service account + payload = 'Hello, World!', // The task HTTP request body + inSeconds = 0 // Delay in task execution ) { // [START cloud_tasks_create_http_task_with_token] // Imports the Google Cloud Tasks library. @@ -42,9 +43,9 @@ async function createHttpTaskWithToken( // const project = 'my-project-id'; // const queue = 'my-queue'; // const location = 'us-central1'; - // const url = 'https://example.com/taskhandler' - // const email = 'client@.iam.gserviceaccount.com' - // const payload = 'hello'; + // const url = 'https://example.com/taskhandler'; + // email = 'client@.iam.gserviceaccount.com'; + // const payload = 'Hello, World!'; // Construct the fully qualified queue name. const parent = client.queuePath(project, location, queue); @@ -52,7 +53,7 @@ async function createHttpTaskWithToken( const task = { httpRequest: { httpMethod: 'POST', - url, //The full url path that the request will be sent to. + url, oidcToken: { serviceAccountEmail: email, }, @@ -64,6 +65,7 @@ async function createHttpTaskWithToken( } if (inSeconds) { + // The time when the task is scheduled to be attempted. task.scheduleTime = { seconds: inSeconds + Date.now() / 1000, }; diff --git a/cloud-tasks/createTask.js b/cloud-tasks/createTask.js index 3c709f1bcf..8c25116741 100644 --- a/cloud-tasks/createTask.js +++ b/cloud-tasks/createTask.js @@ -15,10 +15,21 @@ 'use strict'; +// sample-metadata: +// title: Cloud Tasks Create App Engine Target +// description: Create Cloud Tasks with a Google App Engine Target +// usage: node createTask.js + /** * Create a task for a given queue with an arbitrary payload. */ -async function createTask(project, location, queue, payload, inSeconds) { +async function createTask( + project = 'my-project-id', // Your GCP Project id + queue = 'my-appengine-queue', // Name of your Queue + location = 'us-central1', // The GCP region of your queue + payload = 'Hello, World!', // The task HTTP request body + inSeconds = 0 // Delay in task execution +) { // [START cloud_tasks_appengine_create_task] // [START tasks_quickstart] // Imports the Google Cloud Tasks library. @@ -31,8 +42,7 @@ async function createTask(project, location, queue, payload, inSeconds) { // const project = 'my-project-id'; // const queue = 'my-appengine-queue'; // const location = 'us-central1'; - // const payload = 'hello'; - // const inSeconds = 30; + // const payload = 'Hello, World!'; // Construct the fully qualified queue name. const parent = client.queuePath(project, location, queue); @@ -49,6 +59,7 @@ async function createTask(project, location, queue, payload, inSeconds) { } if (inSeconds) { + // The time when the task is scheduled to be attempted. task.scheduleTime = { seconds: inSeconds + Date.now() / 1000, }; diff --git a/cloud-tasks/package.json b/cloud-tasks/package.json index f8225e2616..783a7333be 100644 --- a/cloud-tasks/package.json +++ b/cloud-tasks/package.json @@ -9,7 +9,8 @@ }, "scripts": { "test": "mocha", - "start": "node server.js" + "start": "node server.js", + "lint": "eslint '**/*.js'" }, "dependencies": { "@google-cloud/tasks": "^1.1.0", diff --git a/cloud-tasks/test/test.samples.js b/cloud-tasks/test/test.samples.js index 1080876c3f..705a5c0379 100644 --- a/cloud-tasks/test/test.samples.js +++ b/cloud-tasks/test/test.samples.js @@ -31,7 +31,7 @@ describe('Cloud Task Sample Tests', () => { before(async () => { const client = new CloudTasksClient(); projectId = await client.getProjectId(); - url = `https://${projectId}.appspot.com/log_payload`; + url = `https://example.com/taskhandler`; }); it('should create a queue', () => { @@ -41,7 +41,7 @@ describe('Cloud Task Sample Tests', () => { it('should create a task', () => { const stdout = exec( - `node createTask ${projectId} us-central1 ${queueName}` + `node createTask ${projectId} ${queueName} us-central1` ); assert.match(stdout, /Created task/); }); @@ -55,14 +55,14 @@ describe('Cloud Task Sample Tests', () => { it('should create a HTTP task', () => { const stdout = exec( - `node createHttpTask ${projectId} us-central1 my-appengine-queue ${url}` + `node createHttpTask ${projectId} my-queue us-central1 ${url}` ); assert.match(stdout, /Created task/); }); it('should create a HTTP task with token', () => { const stdout = exec( - `node createHttpTaskWithToken ${projectId} us-central1 my-appengine-queue ${url} ${SERVICE_ACCOUNT}` + `node createHttpTaskWithToken ${projectId} my-queue us-central1 ${url} ${SERVICE_ACCOUNT}` ); assert.match(stdout, /Created task/); });