11/**
2- * Copyright 2018, Google, Inc.
2+ * Copyright 2019 Google LLC
33 * Licensed under the Apache License, Version 2.0 (the "License");
44 * you may not use this file except in compliance with the License.
55 * You may obtain a copy of the License at
1818/**
1919 * Create a task for a given queue with an arbitrary payload.
2020 */
21- async function createTask ( project , location , queue , options ) {
21+ async function createTask ( project , location , queue , payload , inSeconds ) {
2222 // [START cloud_tasks_appengine_create_task]
2323 // [START tasks_quickstart]
2424 // Imports the Google Cloud Tasks library.
25- const cloudTasks = require ( '@google-cloud/tasks' ) ;
25+ const { CloudTasksClient } = require ( '@google-cloud/tasks' ) ;
2626
2727 // Instantiates a client.
28- const client = new cloudTasks . CloudTasksClient ( ) ;
28+ const client = new CloudTasksClient ( ) ;
2929
3030 // TODO(developer): Uncomment these lines and replace with your values.
3131 // const project = 'my-project-id';
@@ -43,15 +43,13 @@ async function createTask(project, location, queue, options) {
4343 } ,
4444 } ;
4545
46- if ( options . payload !== undefined ) {
47- task . appEngineHttpRequest . body = Buffer . from ( options . payload ) . toString (
48- 'base64'
49- ) ;
46+ if ( payload ) {
47+ task . appEngineHttpRequest . body = Buffer . from ( payload ) . toString ( 'base64' ) ;
5048 }
5149
52- if ( options . inSeconds !== undefined ) {
50+ if ( inSeconds ) {
5351 task . scheduleTime = {
54- seconds : options . inSeconds + Date . now ( ) / 1000 ,
52+ seconds : inSeconds + Date . now ( ) / 1000 ,
5553 } ;
5654 }
5755
@@ -60,7 +58,8 @@ async function createTask(project, location, queue, options) {
6058 task : task ,
6159 } ;
6260
63- console . log ( 'Sending task %j' , task ) ;
61+ console . log ( 'Sending task:' ) ;
62+ console . log ( task ) ;
6463 // Send create task request.
6564 const [ response ] = await client . createTask ( request ) ;
6665 const name = response . name ;
@@ -70,56 +69,4 @@ async function createTask(project, location, queue, options) {
7069 // [END tasks_quickstart]
7170}
7271
73- const cli = require ( `yargs` )
74- . options ( {
75- location : {
76- alias : 'l' ,
77- description : 'Location of the queue to add the task to.' ,
78- type : 'string' ,
79- requiresArg : true ,
80- required : true ,
81- } ,
82- queue : {
83- alias : 'q' ,
84- description : 'ID (short name) of the queue to add the task to.' ,
85- type : 'string' ,
86- requiresArg : true ,
87- required : true ,
88- } ,
89- project : {
90- alias : 'p' ,
91- description : 'Project of the queue to add the task to.' ,
92- default : process . env . GCLOUD_PROJECT ,
93- type : 'string' ,
94- requiresArg : true ,
95- required : true ,
96- } ,
97- payload : {
98- alias : 'd' ,
99- description : '(Optional) Payload to attach to the push queue.' ,
100- type : 'string' ,
101- requiresArg : true ,
102- } ,
103- inSeconds : {
104- alias : 's' ,
105- description :
106- '(Optional) The number of seconds from now to schedule task attempt.' ,
107- type : 'number' ,
108- requiresArg : true ,
109- } ,
110- } )
111- . example ( `node $0 --project my-project-id` )
112- . wrap ( 120 )
113- . recommendCommands ( )
114- . epilogue ( `For more information, see https://cloud.google.com/cloud-tasks` )
115- . strict ( ) ;
116-
117- if ( module === require . main ) {
118- const opts = cli . help ( ) . parse ( process . argv . slice ( 2 ) ) ;
119- process . env . GCLOUD_PROJECT = opts . project ;
120- createTask ( opts . project , opts . location , opts . queue , opts ) . catch (
121- console . error
122- ) ;
123- }
124-
125- exports . createTask = createTask ;
72+ createTask ( ...process . argv . slice ( 2 ) ) . catch ( console . error ) ;
0 commit comments