1
1
/**
2
- * Copyright 2018, Google, Inc.
2
+ * Copyright 2019 Google LLC
3
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
4
* you may not use this file except in compliance with the License.
5
5
* You may obtain a copy of the License at
18
18
/**
19
19
* Create a task for a given queue with an arbitrary payload.
20
20
*/
21
- async function createTask ( project , location , queue , options ) {
21
+ async function createTask ( project , location , queue , payload , inSeconds ) {
22
22
// [START cloud_tasks_appengine_create_task]
23
23
// [START tasks_quickstart]
24
24
// Imports the Google Cloud Tasks library.
25
- const cloudTasks = require ( '@google-cloud/tasks' ) ;
25
+ const { CloudTasksClient } = require ( '@google-cloud/tasks' ) ;
26
26
27
27
// Instantiates a client.
28
- const client = new cloudTasks . CloudTasksClient ( ) ;
28
+ const client = new CloudTasksClient ( ) ;
29
29
30
30
// TODO(developer): Uncomment these lines and replace with your values.
31
31
// const project = 'my-project-id';
@@ -43,15 +43,13 @@ async function createTask(project, location, queue, options) {
43
43
} ,
44
44
} ;
45
45
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' ) ;
50
48
}
51
49
52
- if ( options . inSeconds !== undefined ) {
50
+ if ( inSeconds ) {
53
51
task . scheduleTime = {
54
- seconds : options . inSeconds + Date . now ( ) / 1000 ,
52
+ seconds : inSeconds + Date . now ( ) / 1000 ,
55
53
} ;
56
54
}
57
55
@@ -60,7 +58,8 @@ async function createTask(project, location, queue, options) {
60
58
task : task ,
61
59
} ;
62
60
63
- console . log ( 'Sending task %j' , task ) ;
61
+ console . log ( 'Sending task:' ) ;
62
+ console . log ( task ) ;
64
63
// Send create task request.
65
64
const [ response ] = await client . createTask ( request ) ;
66
65
const name = response . name ;
@@ -70,56 +69,4 @@ async function createTask(project, location, queue, options) {
70
69
// [END tasks_quickstart]
71
70
}
72
71
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