diff --git a/cloud-tasks/createHttpTask.js b/cloud-tasks/createHttpTask.js index bfff837481..a43b15a94b 100644 --- a/cloud-tasks/createHttpTask.js +++ b/cloud-tasks/createHttpTask.js @@ -13,7 +13,6 @@ // limitations under the License. 'use strict'; -/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/ // sample-metadata: // title: Cloud Tasks Create HTTP Target @@ -23,7 +22,7 @@ /** * Create a task with an HTTP target for a given queue with an arbitrary payload. */ -async function createHttpTask( +function main( 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 @@ -38,41 +37,49 @@ async function createHttpTask( // Instantiates a client. const client = new CloudTasksClient(); - // TODO(developer): Uncomment these lines and replace with your values. - // const project = 'my-project-id'; - // const queue = 'my-queue'; - // const location = 'us-central1'; - // const url = 'https://example.com/taskhandler'; - // const payload = 'Hello, World!'; + async function createHttpTask() { + // TODO(developer): Uncomment these lines and replace with your values. + // const project = 'my-project-id'; + // const queue = 'my-queue'; + // const location = 'us-central1'; + // const url = 'https://example.com/taskhandler'; + // const payload = 'Hello, World!'; - // Construct the fully qualified queue name. - const parent = client.queuePath(project, location, queue); + // Construct the fully qualified queue name. + const parent = client.queuePath(project, location, queue); - const task = { - httpRequest: { - httpMethod: 'POST', - url, - }, - }; + const task = { + httpRequest: { + httpMethod: 'POST', + url, + }, + }; - if (payload) { - task.httpRequest.body = Buffer.from(payload).toString('base64'); - } + if (payload) { + task.httpRequest.body = Buffer.from(payload).toString('base64'); + } - if (inSeconds) { - // The time when the task is scheduled to be attempted. - task.scheduleTime = { - seconds: inSeconds + Date.now() / 1000, - }; - } + if (inSeconds) { + // The time when the task is scheduled to be attempted. + task.scheduleTime = { + seconds: inSeconds + Date.now() / 1000, + }; + } - // Send create task request. - console.log('Sending task:'); - console.log(task); - const request = {parent, task}; - const [response] = await client.createTask(request); - console.log(`Created task ${response.name}`); + // Send create task request. + console.log('Sending task:'); + console.log(task); + const request = {parent, task}; + const [response] = await client.createTask(request); + console.log(`Created task ${response.name}`); + } + createHttpTask(); // [END cloud_tasks_create_http_task] } -createHttpTask(...process.argv.slice(2)).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/cloud-tasks/createHttpTaskWithToken.js b/cloud-tasks/createHttpTaskWithToken.js index a82db52855..0c8380edc7 100644 --- a/cloud-tasks/createHttpTaskWithToken.js +++ b/cloud-tasks/createHttpTaskWithToken.js @@ -13,7 +13,6 @@ // limitations under the License. 'use strict'; -/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/ // sample-metadata: // title: Cloud Tasks Create HTTP Target with Token @@ -23,7 +22,7 @@ /** * Create a task with an HTTP target for a given queue with an arbitrary payload. */ -async function createHttpTaskWithToken( +function main( project = 'my-project-id', // Your GCP Project id queue = 'my-queue', // Name of your Queue location = 'us-central1', // The GCP region of your queue @@ -39,47 +38,54 @@ async function createHttpTaskWithToken( // Instantiates a client. const client = new CloudTasksClient(); - // TODO(developer): Uncomment these lines and replace with your values. - // const project = 'my-project-id'; - // const queue = 'my-queue'; - // const location = 'us-central1'; - // const url = 'https://example.com/taskhandler'; - // const serviceAccountEmail = 'client@.iam.gserviceaccount.com'; - // const payload = 'Hello, World!'; + async function createHttpTaskWithToken() { + // TODO(developer): Uncomment these lines and replace with your values. + // const project = 'my-project-id'; + // const queue = 'my-queue'; + // const location = 'us-central1'; + // const url = 'https://example.com/taskhandler'; + // const serviceAccountEmail = 'client@.iam.gserviceaccount.com'; + // const payload = 'Hello, World!'; - // Construct the fully qualified queue name. - const parent = client.queuePath(project, location, queue); + // Construct the fully qualified queue name. + const parent = client.queuePath(project, location, queue); - const task = { - httpRequest: { - httpMethod: 'POST', - url, - oidcToken: { - serviceAccountEmail, + const task = { + httpRequest: { + httpMethod: 'POST', + url, + oidcToken: { + serviceAccountEmail, + }, }, - }, - }; - - if (payload) { - task.httpRequest.body = Buffer.from(payload).toString('base64'); - } - - if (inSeconds) { - // The time when the task is scheduled to be attempted. - task.scheduleTime = { - seconds: inSeconds + Date.now() / 1000, }; - } - console.log('Sending task:'); - console.log(task); - // Send create task request. - const request = {parent, task}; - const [response] = await client.createTask(request); - const name = response.name; - console.log(`Created task ${name}`); + if (payload) { + task.httpRequest.body = Buffer.from(payload).toString('base64'); + } + if (inSeconds) { + // The time when the task is scheduled to be attempted. + task.scheduleTime = { + seconds: inSeconds + Date.now() / 1000, + }; + } + + console.log('Sending task:'); + console.log(task); + // Send create task request. + const request = {parent, task}; + const [response] = await client.createTask(request); + const name = response.name; + console.log(`Created task ${name}`); + } + createHttpTaskWithToken(); // [END cloud_tasks_create_http_task_with_token] } -createHttpTaskWithToken(...process.argv.slice(2)).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/cloud-tasks/createQueue.js b/cloud-tasks/createQueue.js index 4f78c9605e..6d0c223d70 100644 --- a/cloud-tasks/createQueue.js +++ b/cloud-tasks/createQueue.js @@ -14,39 +14,46 @@ 'use strict'; -// [START cloud_tasks_create_queue] /** * Create a new Task Queue */ -async function createQueue( +function main( project = 'my-project-id', // Your GCP Project id queue = 'my-appengine-queue', // Name of the Queue to create location = 'us-central1' // The GCP region in which to create the queue ) { + // [START cloud_tasks_create_queue] // Imports the Google Cloud Tasks library. const cloudTasks = require('@google-cloud/tasks'); // Instantiates a client. const client = new cloudTasks.CloudTasksClient(); - // Send create queue request. - const [response] = await client.createQueue({ - // The fully qualified path to the location where the queue is created - parent: client.locationPath(project, location), - queue: { - // The fully qualified path to the queue - name: client.queuePath(project, location, queue), - appEngineHttpQueue: { - appEngineRoutingOverride: { - // The App Engine service that will receive the tasks. - service: 'default', + async function createQueue() { + // Send create queue request. + const [response] = await client.createQueue({ + // The fully qualified path to the location where the queue is created + parent: client.locationPath(project, location), + queue: { + // The fully qualified path to the queue + name: client.queuePath(project, location, queue), + appEngineHttpQueue: { + appEngineRoutingOverride: { + // The App Engine service that will receive the tasks. + service: 'default', + }, }, }, - }, - }); - console.log(`Created queue ${response.name}`); + }); + console.log(`Created queue ${response.name}`); + } + createQueue(); + // [END cloud_tasks_create_queue] } -// [END cloud_tasks_create_queue] -const args = process.argv.slice(2); -createQueue(...args).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/cloud-tasks/createTask.js b/cloud-tasks/createTask.js index 80307f126d..fb3335f2bd 100644 --- a/cloud-tasks/createTask.js +++ b/cloud-tasks/createTask.js @@ -13,7 +13,6 @@ // limitations under the License. 'use strict'; -/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/ // sample-metadata: // title: Cloud Tasks Create App Engine Target @@ -23,7 +22,7 @@ /** * Create a task for a given queue with an arbitrary payload. */ -async function createTask( +function main( 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 @@ -38,43 +37,50 @@ async function createTask( // Instantiates a client. const client = new 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 payload = 'Hello, World!'; + async function createTask() { + // 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 payload = 'Hello, World!'; - // Construct the fully qualified queue name. - const parent = client.queuePath(project, location, queue); + // Construct the fully qualified queue name. + const parent = client.queuePath(project, location, queue); - const task = { - appEngineHttpRequest: { - httpMethod: 'POST', - relativeUri: '/log_payload', - }, - }; - - if (payload) { - task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64'); - } - - if (inSeconds) { - // The time when the task is scheduled to be attempted. - task.scheduleTime = { - seconds: inSeconds + Date.now() / 1000, + const task = { + appEngineHttpRequest: { + httpMethod: 'POST', + relativeUri: '/log_payload', + }, }; - } - console.log('Sending task:'); - console.log(task); - // Send create task request. - const request = {parent, task}; - const [response] = await client.createTask(request); - const name = response.name; - console.log(`Created task ${name}`); + if (payload) { + task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64'); + } + if (inSeconds) { + // The time when the task is scheduled to be attempted. + task.scheduleTime = { + seconds: inSeconds + Date.now() / 1000, + }; + } + + console.log('Sending task:'); + console.log(task); + // Send create task request. + const request = {parent, task}; + const [response] = await client.createTask(request); + const name = response.name; + console.log(`Created task ${name}`); + } + createTask(); // [END cloud_tasks_appengine_create_task] // [END tasks_quickstart] } -createTask(...process.argv.slice(2)).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/cloud-tasks/deleteQueue.js b/cloud-tasks/deleteQueue.js index 1cedca2760..ab124aed9d 100644 --- a/cloud-tasks/deleteQueue.js +++ b/cloud-tasks/deleteQueue.js @@ -14,29 +14,36 @@ 'use strict'; -// [START cloud_tasks_delete_queue] /** * Delete a given Queue */ -async function deleteQueue( +function main( project = 'my-project-id', // Your GCP Project id queue = 'my-appengine-queue', // Name of the Queue to delete location = 'us-central1' // The GCP region in which to delete the queue ) { + // [START cloud_tasks_delete_queue] // Imports the Google Cloud Tasks library. const cloudTasks = require('@google-cloud/tasks'); // Instantiates a client. const client = new cloudTasks.CloudTasksClient(); - // Get the fully qualified path to the queue - const name = client.queuePath(project, location, queue); + async function deleteQueue() { + // Get the fully qualified path to the queue + const name = client.queuePath(project, location, queue); - // Send delete queue request. - await client.deleteQueue({name}); - console.log(`Deleted queue '${queue}'.`); + // Send delete queue request. + await client.deleteQueue({name}); + console.log(`Deleted queue '${queue}'.`); + } + deleteQueue(); + // [END cloud_tasks_delete_queue] } -// [END cloud_tasks_delete_queue] -const args = process.argv.slice(2); -deleteQueue(...args).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/cloud-tasks/listQueues.js b/cloud-tasks/listQueues.js index b4f23fdba9..8f14a7f6f8 100644 --- a/cloud-tasks/listQueues.js +++ b/cloud-tasks/listQueues.js @@ -14,33 +14,40 @@ 'use strict'; -// [START cloud_tasks_list_queues] -async function listQueues( +function main( project = 'my-project-id', // Your GCP Project id location = 'us-central1' // The GCP region to search for queues ) { + // [START cloud_tasks_list_queues] // Imports the Google Cloud Tasks library. const cloudTasks = require('@google-cloud/tasks'); // Instantiates a client. const client = new cloudTasks.CloudTasksClient(); - // Get the fully qualified path to the region - const parent = client.locationPath(project, location); + async function listQueues() { + // Get the fully qualified path to the region + const parent = client.locationPath(project, location); - // list all fo the queues - const [queues] = await client.listQueues({parent}); + // list all fo the queues + const [queues] = await client.listQueues({parent}); - if (queues.length > 0) { - console.log('Queues:'); - queues.forEach(queue => { - console.log(` ${queue.name}`); - }); - } else { - console.log('No queues found!'); + if (queues.length > 0) { + console.log('Queues:'); + queues.forEach(queue => { + console.log(` ${queue.name}`); + }); + } else { + console.log('No queues found!'); + } } + listQueues(); + // [END cloud_tasks_list_queues] } -// [END cloud_tasks_list_queues] -const args = process.argv.slice(2); -listQueues(...args).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/cloud-tasks/package.json b/cloud-tasks/package.json index 6890311b90..7e9981e3e1 100644 --- a/cloud-tasks/package.json +++ b/cloud-tasks/package.json @@ -17,8 +17,7 @@ "dependencies": { "@google-cloud/tasks": "^2.1.1", "body-parser": "^1.18.3", - "express": "^4.16.3", - "yargs": "^16.0.0" + "express": "^4.16.3" }, "devDependencies": { "chai": "^4.2.0", diff --git a/cloud-tasks/quickstart.js b/cloud-tasks/quickstart.js index 88bb0372f2..c36fda6035 100644 --- a/cloud-tasks/quickstart.js +++ b/cloud-tasks/quickstart.js @@ -13,12 +13,11 @@ // limitations under the License. 'use strict'; -/*eslint no-warning-comments: [0, { "terms": ["todo", "fixme"], "location": "anywhere" }]*/ /** * Create a task for a given queue with an arbitrary payload. */ -async function createTask(project, location, queue, payload, inSeconds) { +function main(project, location, queue, payload, inSeconds) { // [START tasks_quickstart] // Imports the Google Cloud Tasks library. const {CloudTasksClient} = require('@google-cloud/tasks'); @@ -26,45 +25,52 @@ async function createTask(project, location, queue, payload, inSeconds) { // Instantiates a client. const client = new 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 payload = 'hello'; + async function quickstart() { + // 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 payload = 'hello'; - // Construct the fully qualified queue name. - const parent = client.queuePath(project, location, queue); + // Construct the fully qualified queue name. + const parent = client.queuePath(project, location, queue); - const task = { - appEngineHttpRequest: { - httpMethod: 'POST', - relativeUri: '/log_payload', - }, - }; - - if (payload) { - task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64'); - } - - if (inSeconds) { - task.scheduleTime = { - seconds: inSeconds + Date.now() / 1000, + const task = { + appEngineHttpRequest: { + httpMethod: 'POST', + relativeUri: '/log_payload', + }, }; - } - const request = { - parent: parent, - task: task, - }; + if (payload) { + task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64'); + } - 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}`); + 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}`); + } + quickstart(); // [END tasks_quickstart] } -createTask(...process.argv.slice(2)).catch(console.error); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2));