1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Code generated by protoc-gen-go_gapic. DO NOT EDIT. 16 17package cloudtasks 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "github.com/golang/protobuf/proto" 27 gax "github.com/googleapis/gax-go/v2" 28 "google.golang.org/api/iterator" 29 "google.golang.org/api/option" 30 gtransport "google.golang.org/api/transport/grpc" 31 taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2beta2" 32 iampb "google.golang.org/genproto/googleapis/iam/v1" 33 "google.golang.org/grpc" 34 "google.golang.org/grpc/codes" 35 "google.golang.org/grpc/metadata" 36) 37 38// CallOptions contains the retry settings for each method of Client. 39type CallOptions struct { 40 ListQueues []gax.CallOption 41 GetQueue []gax.CallOption 42 CreateQueue []gax.CallOption 43 UpdateQueue []gax.CallOption 44 DeleteQueue []gax.CallOption 45 PurgeQueue []gax.CallOption 46 PauseQueue []gax.CallOption 47 ResumeQueue []gax.CallOption 48 GetIamPolicy []gax.CallOption 49 SetIamPolicy []gax.CallOption 50 TestIamPermissions []gax.CallOption 51 ListTasks []gax.CallOption 52 GetTask []gax.CallOption 53 CreateTask []gax.CallOption 54 DeleteTask []gax.CallOption 55 LeaseTasks []gax.CallOption 56 AcknowledgeTask []gax.CallOption 57 RenewLease []gax.CallOption 58 CancelLease []gax.CallOption 59 RunTask []gax.CallOption 60} 61 62func defaultClientOptions() []option.ClientOption { 63 return []option.ClientOption{ 64 option.WithEndpoint("cloudtasks.googleapis.com:443"), 65 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 66 option.WithScopes(DefaultAuthScopes()...), 67 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 68 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 69 } 70} 71 72func defaultCallOptions() *CallOptions { 73 return &CallOptions{ 74 ListQueues: []gax.CallOption{ 75 gax.WithRetry(func() gax.Retryer { 76 return gax.OnCodes([]codes.Code{ 77 codes.Unavailable, 78 codes.DeadlineExceeded, 79 }, gax.Backoff{ 80 Initial: 100 * time.Millisecond, 81 Max: 10000 * time.Millisecond, 82 Multiplier: 1.30, 83 }) 84 }), 85 }, 86 GetQueue: []gax.CallOption{ 87 gax.WithRetry(func() gax.Retryer { 88 return gax.OnCodes([]codes.Code{ 89 codes.Unavailable, 90 codes.DeadlineExceeded, 91 }, gax.Backoff{ 92 Initial: 100 * time.Millisecond, 93 Max: 10000 * time.Millisecond, 94 Multiplier: 1.30, 95 }) 96 }), 97 }, 98 CreateQueue: []gax.CallOption{}, 99 UpdateQueue: []gax.CallOption{}, 100 DeleteQueue: []gax.CallOption{ 101 gax.WithRetry(func() gax.Retryer { 102 return gax.OnCodes([]codes.Code{ 103 codes.Unavailable, 104 codes.DeadlineExceeded, 105 }, gax.Backoff{ 106 Initial: 100 * time.Millisecond, 107 Max: 10000 * time.Millisecond, 108 Multiplier: 1.30, 109 }) 110 }), 111 }, 112 PurgeQueue: []gax.CallOption{}, 113 PauseQueue: []gax.CallOption{}, 114 ResumeQueue: []gax.CallOption{}, 115 GetIamPolicy: []gax.CallOption{ 116 gax.WithRetry(func() gax.Retryer { 117 return gax.OnCodes([]codes.Code{ 118 codes.Unavailable, 119 codes.DeadlineExceeded, 120 }, gax.Backoff{ 121 Initial: 100 * time.Millisecond, 122 Max: 10000 * time.Millisecond, 123 Multiplier: 1.30, 124 }) 125 }), 126 }, 127 SetIamPolicy: []gax.CallOption{}, 128 TestIamPermissions: []gax.CallOption{ 129 gax.WithRetry(func() gax.Retryer { 130 return gax.OnCodes([]codes.Code{ 131 codes.Unavailable, 132 codes.DeadlineExceeded, 133 }, gax.Backoff{ 134 Initial: 100 * time.Millisecond, 135 Max: 10000 * time.Millisecond, 136 Multiplier: 1.30, 137 }) 138 }), 139 }, 140 ListTasks: []gax.CallOption{ 141 gax.WithRetry(func() gax.Retryer { 142 return gax.OnCodes([]codes.Code{ 143 codes.Unavailable, 144 codes.DeadlineExceeded, 145 }, gax.Backoff{ 146 Initial: 100 * time.Millisecond, 147 Max: 10000 * time.Millisecond, 148 Multiplier: 1.30, 149 }) 150 }), 151 }, 152 GetTask: []gax.CallOption{ 153 gax.WithRetry(func() gax.Retryer { 154 return gax.OnCodes([]codes.Code{ 155 codes.Unavailable, 156 codes.DeadlineExceeded, 157 }, gax.Backoff{ 158 Initial: 100 * time.Millisecond, 159 Max: 10000 * time.Millisecond, 160 Multiplier: 1.30, 161 }) 162 }), 163 }, 164 CreateTask: []gax.CallOption{}, 165 DeleteTask: []gax.CallOption{ 166 gax.WithRetry(func() gax.Retryer { 167 return gax.OnCodes([]codes.Code{ 168 codes.Unavailable, 169 codes.DeadlineExceeded, 170 }, gax.Backoff{ 171 Initial: 100 * time.Millisecond, 172 Max: 10000 * time.Millisecond, 173 Multiplier: 1.30, 174 }) 175 }), 176 }, 177 LeaseTasks: []gax.CallOption{}, 178 AcknowledgeTask: []gax.CallOption{}, 179 RenewLease: []gax.CallOption{}, 180 CancelLease: []gax.CallOption{}, 181 RunTask: []gax.CallOption{}, 182 } 183} 184 185// Client is a client for interacting with Cloud Tasks API. 186// 187// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 188type Client struct { 189 // Connection pool of gRPC connections to the service. 190 connPool gtransport.ConnPool 191 192 // The gRPC API client. 193 client taskspb.CloudTasksClient 194 195 // The call options for this service. 196 CallOptions *CallOptions 197 198 // The x-goog-* metadata to be sent with each request. 199 xGoogMetadata metadata.MD 200} 201 202// NewClient creates a new cloud tasks client. 203// 204// Cloud Tasks allows developers to manage the execution of background 205// work in their applications. 206func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { 207 connPool, err := gtransport.DialPool(ctx, append(defaultClientOptions(), opts...)...) 208 if err != nil { 209 return nil, err 210 } 211 c := &Client{ 212 connPool: connPool, 213 CallOptions: defaultCallOptions(), 214 215 client: taskspb.NewCloudTasksClient(connPool), 216 } 217 c.setGoogleClientInfo() 218 219 return c, nil 220} 221 222// Connection returns a connection to the API service. 223// 224// Deprecated. 225func (c *Client) Connection() *grpc.ClientConn { 226 return c.connPool.Conn() 227} 228 229// Close closes the connection to the API service. The user should invoke this when 230// the client is no longer required. 231func (c *Client) Close() error { 232 return c.connPool.Close() 233} 234 235// setGoogleClientInfo sets the name and version of the application in 236// the `x-goog-api-client` header passed on each request. Intended for 237// use by Google-written clients. 238func (c *Client) setGoogleClientInfo(keyval ...string) { 239 kv := append([]string{"gl-go", versionGo()}, keyval...) 240 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 241 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 242} 243 244// ListQueues lists queues. 245// 246// Queues are returned in lexicographical order. 247func (c *Client) ListQueues(ctx context.Context, req *taskspb.ListQueuesRequest, opts ...gax.CallOption) *QueueIterator { 248 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 249 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 250 opts = append(c.CallOptions.ListQueues[0:len(c.CallOptions.ListQueues):len(c.CallOptions.ListQueues)], opts...) 251 it := &QueueIterator{} 252 req = proto.Clone(req).(*taskspb.ListQueuesRequest) 253 it.InternalFetch = func(pageSize int, pageToken string) ([]*taskspb.Queue, string, error) { 254 var resp *taskspb.ListQueuesResponse 255 req.PageToken = pageToken 256 if pageSize > math.MaxInt32 { 257 req.PageSize = math.MaxInt32 258 } else { 259 req.PageSize = int32(pageSize) 260 } 261 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 262 var err error 263 resp, err = c.client.ListQueues(ctx, req, settings.GRPC...) 264 return err 265 }, opts...) 266 if err != nil { 267 return nil, "", err 268 } 269 270 it.Response = resp 271 return resp.Queues, resp.NextPageToken, nil 272 } 273 fetch := func(pageSize int, pageToken string) (string, error) { 274 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 275 if err != nil { 276 return "", err 277 } 278 it.items = append(it.items, items...) 279 return nextPageToken, nil 280 } 281 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 282 it.pageInfo.MaxSize = int(req.PageSize) 283 it.pageInfo.Token = req.PageToken 284 return it 285} 286 287// GetQueue gets a queue. 288func (c *Client) GetQueue(ctx context.Context, req *taskspb.GetQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) { 289 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 290 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 291 opts = append(c.CallOptions.GetQueue[0:len(c.CallOptions.GetQueue):len(c.CallOptions.GetQueue)], opts...) 292 var resp *taskspb.Queue 293 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 294 var err error 295 resp, err = c.client.GetQueue(ctx, req, settings.GRPC...) 296 return err 297 }, opts...) 298 if err != nil { 299 return nil, err 300 } 301 return resp, nil 302} 303 304// CreateQueue creates a queue. 305// 306// Queues created with this method allow tasks to live for a maximum of 31 307// days. After a task is 31 days old, the task will be deleted regardless of whether 308// it was dispatched or not. 309// 310// WARNING: Using this method may have unintended side effects if you are 311// using an App Engine queue.yaml or queue.xml file to manage your queues. 312// Read 313// Overview of Queue Management and 314// queue.yaml (at https://cloud.google.com/tasks/docs/queue-yaml) before using 315// this method. 316func (c *Client) CreateQueue(ctx context.Context, req *taskspb.CreateQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) { 317 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 318 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 319 opts = append(c.CallOptions.CreateQueue[0:len(c.CallOptions.CreateQueue):len(c.CallOptions.CreateQueue)], opts...) 320 var resp *taskspb.Queue 321 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 322 var err error 323 resp, err = c.client.CreateQueue(ctx, req, settings.GRPC...) 324 return err 325 }, opts...) 326 if err != nil { 327 return nil, err 328 } 329 return resp, nil 330} 331 332// UpdateQueue updates a queue. 333// 334// This method creates the queue if it does not exist and updates 335// the queue if it does exist. 336// 337// Queues created with this method allow tasks to live for a maximum of 31 338// days. After a task is 31 days old, the task will be deleted regardless of whether 339// it was dispatched or not. 340// 341// WARNING: Using this method may have unintended side effects if you are 342// using an App Engine queue.yaml or queue.xml file to manage your queues. 343// Read 344// Overview of Queue Management and 345// queue.yaml (at https://cloud.google.com/tasks/docs/queue-yaml) before using 346// this method. 347func (c *Client) UpdateQueue(ctx context.Context, req *taskspb.UpdateQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) { 348 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "queue.name", url.QueryEscape(req.GetQueue().GetName()))) 349 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 350 opts = append(c.CallOptions.UpdateQueue[0:len(c.CallOptions.UpdateQueue):len(c.CallOptions.UpdateQueue)], opts...) 351 var resp *taskspb.Queue 352 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 353 var err error 354 resp, err = c.client.UpdateQueue(ctx, req, settings.GRPC...) 355 return err 356 }, opts...) 357 if err != nil { 358 return nil, err 359 } 360 return resp, nil 361} 362 363// DeleteQueue deletes a queue. 364// 365// This command will delete the queue even if it has tasks in it. 366// 367// Note: If you delete a queue, a queue with the same name can’t be created 368// for 7 days. 369// 370// WARNING: Using this method may have unintended side effects if you are 371// using an App Engine queue.yaml or queue.xml file to manage your queues. 372// Read 373// Overview of Queue Management and 374// queue.yaml (at https://cloud.google.com/tasks/docs/queue-yaml) before using 375// this method. 376func (c *Client) DeleteQueue(ctx context.Context, req *taskspb.DeleteQueueRequest, opts ...gax.CallOption) error { 377 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 378 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 379 opts = append(c.CallOptions.DeleteQueue[0:len(c.CallOptions.DeleteQueue):len(c.CallOptions.DeleteQueue)], opts...) 380 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 381 var err error 382 _, err = c.client.DeleteQueue(ctx, req, settings.GRPC...) 383 return err 384 }, opts...) 385 return err 386} 387 388// PurgeQueue purges a queue by deleting all of its tasks. 389// 390// All tasks created before this method is called are permanently deleted. 391// 392// Purge operations can take up to one minute to take effect. Tasks 393// might be dispatched before the purge takes effect. A purge is irreversible. 394func (c *Client) PurgeQueue(ctx context.Context, req *taskspb.PurgeQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) { 395 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 396 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 397 opts = append(c.CallOptions.PurgeQueue[0:len(c.CallOptions.PurgeQueue):len(c.CallOptions.PurgeQueue)], opts...) 398 var resp *taskspb.Queue 399 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 400 var err error 401 resp, err = c.client.PurgeQueue(ctx, req, settings.GRPC...) 402 return err 403 }, opts...) 404 if err != nil { 405 return nil, err 406 } 407 return resp, nil 408} 409 410// PauseQueue pauses the queue. 411// 412// If a queue is paused then the system will stop dispatching tasks 413// until the queue is resumed via 414// ResumeQueue. Tasks can still be added 415// when the queue is paused. A queue is paused if its 416// state is PAUSED. 417func (c *Client) PauseQueue(ctx context.Context, req *taskspb.PauseQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) { 418 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 419 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 420 opts = append(c.CallOptions.PauseQueue[0:len(c.CallOptions.PauseQueue):len(c.CallOptions.PauseQueue)], opts...) 421 var resp *taskspb.Queue 422 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 423 var err error 424 resp, err = c.client.PauseQueue(ctx, req, settings.GRPC...) 425 return err 426 }, opts...) 427 if err != nil { 428 return nil, err 429 } 430 return resp, nil 431} 432 433// ResumeQueue resume a queue. 434// 435// This method resumes a queue after it has been 436// PAUSED or 437// DISABLED. The state of a queue is stored 438// in the queue’s state; after calling this method it 439// will be set to RUNNING. 440// 441// WARNING: Resuming many high-QPS queues at the same time can 442// lead to target overloading. If you are resuming high-QPS 443// queues, follow the 500/50/5 pattern described in 444// Managing Cloud Tasks Scaling 445// Risks (at https://cloud.google.com/tasks/docs/manage-cloud-task-scaling). 446func (c *Client) ResumeQueue(ctx context.Context, req *taskspb.ResumeQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) { 447 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 448 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 449 opts = append(c.CallOptions.ResumeQueue[0:len(c.CallOptions.ResumeQueue):len(c.CallOptions.ResumeQueue)], opts...) 450 var resp *taskspb.Queue 451 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 452 var err error 453 resp, err = c.client.ResumeQueue(ctx, req, settings.GRPC...) 454 return err 455 }, opts...) 456 if err != nil { 457 return nil, err 458 } 459 return resp, nil 460} 461 462// GetIamPolicy gets the access control policy for a Queue. 463// Returns an empty policy if the resource exists and does not have a policy 464// set. 465// 466// Authorization requires the following 467// Google IAM (at https://cloud.google.com/iam) permission on the specified 468// resource parent: 469// 470// cloudtasks.queues.getIamPolicy 471func (c *Client) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 472 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 473 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 474 opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...) 475 var resp *iampb.Policy 476 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 477 var err error 478 resp, err = c.client.GetIamPolicy(ctx, req, settings.GRPC...) 479 return err 480 }, opts...) 481 if err != nil { 482 return nil, err 483 } 484 return resp, nil 485} 486 487// SetIamPolicy sets the access control policy for a Queue. Replaces any existing 488// policy. 489// 490// Note: The Cloud Console does not check queue-level IAM permissions yet. 491// Project-level permissions are required to use the Cloud Console. 492// 493// Authorization requires the following 494// Google IAM (at https://cloud.google.com/iam) permission on the specified 495// resource parent: 496// 497// cloudtasks.queues.setIamPolicy 498func (c *Client) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 499 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 500 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 501 opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...) 502 var resp *iampb.Policy 503 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 504 var err error 505 resp, err = c.client.SetIamPolicy(ctx, req, settings.GRPC...) 506 return err 507 }, opts...) 508 if err != nil { 509 return nil, err 510 } 511 return resp, nil 512} 513 514// TestIamPermissions returns permissions that a caller has on a Queue. 515// If the resource does not exist, this will return an empty set of 516// permissions, not a NOT_FOUND error. 517// 518// Note: This operation is designed to be used for building permission-aware 519// UIs and command-line tools, not for authorization checking. This operation 520// may “fail open” without warning. 521func (c *Client) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) { 522 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 523 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 524 opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...) 525 var resp *iampb.TestIamPermissionsResponse 526 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 527 var err error 528 resp, err = c.client.TestIamPermissions(ctx, req, settings.GRPC...) 529 return err 530 }, opts...) 531 if err != nil { 532 return nil, err 533 } 534 return resp, nil 535} 536 537// ListTasks lists the tasks in a queue. 538// 539// By default, only the BASIC view is retrieved 540// due to performance considerations; 541// response_view controls the 542// subset of information which is returned. 543// 544// The tasks may be returned in any order. The ordering may change at any 545// time. 546func (c *Client) ListTasks(ctx context.Context, req *taskspb.ListTasksRequest, opts ...gax.CallOption) *TaskIterator { 547 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 548 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 549 opts = append(c.CallOptions.ListTasks[0:len(c.CallOptions.ListTasks):len(c.CallOptions.ListTasks)], opts...) 550 it := &TaskIterator{} 551 req = proto.Clone(req).(*taskspb.ListTasksRequest) 552 it.InternalFetch = func(pageSize int, pageToken string) ([]*taskspb.Task, string, error) { 553 var resp *taskspb.ListTasksResponse 554 req.PageToken = pageToken 555 if pageSize > math.MaxInt32 { 556 req.PageSize = math.MaxInt32 557 } else { 558 req.PageSize = int32(pageSize) 559 } 560 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 561 var err error 562 resp, err = c.client.ListTasks(ctx, req, settings.GRPC...) 563 return err 564 }, opts...) 565 if err != nil { 566 return nil, "", err 567 } 568 569 it.Response = resp 570 return resp.Tasks, resp.NextPageToken, nil 571 } 572 fetch := func(pageSize int, pageToken string) (string, error) { 573 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 574 if err != nil { 575 return "", err 576 } 577 it.items = append(it.items, items...) 578 return nextPageToken, nil 579 } 580 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 581 it.pageInfo.MaxSize = int(req.PageSize) 582 it.pageInfo.Token = req.PageToken 583 return it 584} 585 586// GetTask gets a task. 587func (c *Client) GetTask(ctx context.Context, req *taskspb.GetTaskRequest, opts ...gax.CallOption) (*taskspb.Task, error) { 588 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 589 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 590 opts = append(c.CallOptions.GetTask[0:len(c.CallOptions.GetTask):len(c.CallOptions.GetTask)], opts...) 591 var resp *taskspb.Task 592 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 593 var err error 594 resp, err = c.client.GetTask(ctx, req, settings.GRPC...) 595 return err 596 }, opts...) 597 if err != nil { 598 return nil, err 599 } 600 return resp, nil 601} 602 603// CreateTask creates a task and adds it to a queue. 604// 605// Tasks cannot be updated after creation; there is no UpdateTask command. 606// 607// For [App Engine queues][google.cloud.tasks.v2beta2.AppEngineHttpTarget], the maximum task size is 608// 100KB. 609// 610// For [pull queues][google.cloud.tasks.v2beta2.PullTarget], the maximum task size is 1MB. 611func (c *Client) CreateTask(ctx context.Context, req *taskspb.CreateTaskRequest, opts ...gax.CallOption) (*taskspb.Task, error) { 612 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 613 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 614 opts = append(c.CallOptions.CreateTask[0:len(c.CallOptions.CreateTask):len(c.CallOptions.CreateTask)], opts...) 615 var resp *taskspb.Task 616 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 617 var err error 618 resp, err = c.client.CreateTask(ctx, req, settings.GRPC...) 619 return err 620 }, opts...) 621 if err != nil { 622 return nil, err 623 } 624 return resp, nil 625} 626 627// DeleteTask deletes a task. 628// 629// A task can be deleted if it is scheduled or dispatched. A task 630// cannot be deleted if it has completed successfully or permanently 631// failed. 632func (c *Client) DeleteTask(ctx context.Context, req *taskspb.DeleteTaskRequest, opts ...gax.CallOption) error { 633 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 634 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 635 opts = append(c.CallOptions.DeleteTask[0:len(c.CallOptions.DeleteTask):len(c.CallOptions.DeleteTask)], opts...) 636 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 637 var err error 638 _, err = c.client.DeleteTask(ctx, req, settings.GRPC...) 639 return err 640 }, opts...) 641 return err 642} 643 644// LeaseTasks leases tasks from a pull queue for 645// lease_duration. 646// 647// This method is invoked by the worker to obtain a lease. The 648// worker must acknowledge the task via 649// AcknowledgeTask after they have 650// performed the work associated with the task. 651// 652// The payload is intended to store data that 653// the worker needs to perform the work associated with the task. To 654// return the payloads in the response, set 655// response_view to 656// FULL. 657// 658// A maximum of 10 qps of LeaseTasks 659// requests are allowed per 660// queue. RESOURCE_EXHAUSTED 661// is returned when this limit is 662// exceeded. RESOURCE_EXHAUSTED 663// is also returned when 664// max_tasks_dispatched_per_second 665// is exceeded. 666func (c *Client) LeaseTasks(ctx context.Context, req *taskspb.LeaseTasksRequest, opts ...gax.CallOption) (*taskspb.LeaseTasksResponse, error) { 667 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 668 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 669 opts = append(c.CallOptions.LeaseTasks[0:len(c.CallOptions.LeaseTasks):len(c.CallOptions.LeaseTasks)], opts...) 670 var resp *taskspb.LeaseTasksResponse 671 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 672 var err error 673 resp, err = c.client.LeaseTasks(ctx, req, settings.GRPC...) 674 return err 675 }, opts...) 676 if err != nil { 677 return nil, err 678 } 679 return resp, nil 680} 681 682// AcknowledgeTask acknowledges a pull task. 683// 684// The worker, that is, the entity that 685// leased this task must call this method 686// to indicate that the work associated with the task has finished. 687// 688// The worker must acknowledge a task within the 689// lease_duration or the lease 690// will expire and the task will become available to be leased 691// again. After the task is acknowledged, it will not be returned 692// by a later LeaseTasks, 693// GetTask, or 694// ListTasks. 695func (c *Client) AcknowledgeTask(ctx context.Context, req *taskspb.AcknowledgeTaskRequest, opts ...gax.CallOption) error { 696 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 697 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 698 opts = append(c.CallOptions.AcknowledgeTask[0:len(c.CallOptions.AcknowledgeTask):len(c.CallOptions.AcknowledgeTask)], opts...) 699 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 700 var err error 701 _, err = c.client.AcknowledgeTask(ctx, req, settings.GRPC...) 702 return err 703 }, opts...) 704 return err 705} 706 707// RenewLease renew the current lease of a pull task. 708// 709// The worker can use this method to extend the lease by a new 710// duration, starting from now. The new task lease will be 711// returned in the task’s schedule_time. 712func (c *Client) RenewLease(ctx context.Context, req *taskspb.RenewLeaseRequest, opts ...gax.CallOption) (*taskspb.Task, error) { 713 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 714 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 715 opts = append(c.CallOptions.RenewLease[0:len(c.CallOptions.RenewLease):len(c.CallOptions.RenewLease)], opts...) 716 var resp *taskspb.Task 717 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 718 var err error 719 resp, err = c.client.RenewLease(ctx, req, settings.GRPC...) 720 return err 721 }, opts...) 722 if err != nil { 723 return nil, err 724 } 725 return resp, nil 726} 727 728// CancelLease cancel a pull task’s lease. 729// 730// The worker can use this method to cancel a task’s lease by 731// setting its schedule_time to now. This will 732// make the task available to be leased to the next caller of 733// LeaseTasks. 734func (c *Client) CancelLease(ctx context.Context, req *taskspb.CancelLeaseRequest, opts ...gax.CallOption) (*taskspb.Task, error) { 735 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 736 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 737 opts = append(c.CallOptions.CancelLease[0:len(c.CallOptions.CancelLease):len(c.CallOptions.CancelLease)], opts...) 738 var resp *taskspb.Task 739 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 740 var err error 741 resp, err = c.client.CancelLease(ctx, req, settings.GRPC...) 742 return err 743 }, opts...) 744 if err != nil { 745 return nil, err 746 } 747 return resp, nil 748} 749 750// RunTask forces a task to run now. 751// 752// When this method is called, Cloud Tasks will dispatch the task, even if 753// the task is already running, the queue has reached its RateLimits or 754// is PAUSED. 755// 756// This command is meant to be used for manual debugging. For 757// example, RunTask can be used to retry a failed 758// task after a fix has been made or to manually force a task to be 759// dispatched now. 760// 761// The dispatched task is returned. That is, the task that is returned 762// contains the status after the task is dispatched but 763// before the task is received by its target. 764// 765// If Cloud Tasks receives a successful response from the task’s 766// target, then the task will be deleted; otherwise the task’s 767// schedule_time will be reset to the time that 768// RunTask was called plus the retry delay specified 769// in the queue’s RetryConfig. 770// 771// RunTask returns 772// NOT_FOUND when it is called on a 773// task that has already succeeded or permanently failed. 774// 775// RunTask cannot be called on a 776// [pull task][google.cloud.tasks.v2beta2.PullMessage]. 777func (c *Client) RunTask(ctx context.Context, req *taskspb.RunTaskRequest, opts ...gax.CallOption) (*taskspb.Task, error) { 778 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 779 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 780 opts = append(c.CallOptions.RunTask[0:len(c.CallOptions.RunTask):len(c.CallOptions.RunTask)], opts...) 781 var resp *taskspb.Task 782 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 783 var err error 784 resp, err = c.client.RunTask(ctx, req, settings.GRPC...) 785 return err 786 }, opts...) 787 if err != nil { 788 return nil, err 789 } 790 return resp, nil 791} 792 793// QueueIterator manages a stream of *taskspb.Queue. 794type QueueIterator struct { 795 items []*taskspb.Queue 796 pageInfo *iterator.PageInfo 797 nextFunc func() error 798 799 // Response is the raw response for the current page. 800 // It must be cast to the RPC response type. 801 // Calling Next() or InternalFetch() updates this value. 802 Response interface{} 803 804 // InternalFetch is for use by the Google Cloud Libraries only. 805 // It is not part of the stable interface of this package. 806 // 807 // InternalFetch returns results from a single call to the underlying RPC. 808 // The number of results is no greater than pageSize. 809 // If there are no more results, nextPageToken is empty and err is nil. 810 InternalFetch func(pageSize int, pageToken string) (results []*taskspb.Queue, nextPageToken string, err error) 811} 812 813// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 814func (it *QueueIterator) PageInfo() *iterator.PageInfo { 815 return it.pageInfo 816} 817 818// Next returns the next result. Its second return value is iterator.Done if there are no more 819// results. Once Next returns Done, all subsequent calls will return Done. 820func (it *QueueIterator) Next() (*taskspb.Queue, error) { 821 var item *taskspb.Queue 822 if err := it.nextFunc(); err != nil { 823 return item, err 824 } 825 item = it.items[0] 826 it.items = it.items[1:] 827 return item, nil 828} 829 830func (it *QueueIterator) bufLen() int { 831 return len(it.items) 832} 833 834func (it *QueueIterator) takeBuf() interface{} { 835 b := it.items 836 it.items = nil 837 return b 838} 839 840// TaskIterator manages a stream of *taskspb.Task. 841type TaskIterator struct { 842 items []*taskspb.Task 843 pageInfo *iterator.PageInfo 844 nextFunc func() error 845 846 // Response is the raw response for the current page. 847 // It must be cast to the RPC response type. 848 // Calling Next() or InternalFetch() updates this value. 849 Response interface{} 850 851 // InternalFetch is for use by the Google Cloud Libraries only. 852 // It is not part of the stable interface of this package. 853 // 854 // InternalFetch returns results from a single call to the underlying RPC. 855 // The number of results is no greater than pageSize. 856 // If there are no more results, nextPageToken is empty and err is nil. 857 InternalFetch func(pageSize int, pageToken string) (results []*taskspb.Task, nextPageToken string, err error) 858} 859 860// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 861func (it *TaskIterator) PageInfo() *iterator.PageInfo { 862 return it.pageInfo 863} 864 865// Next returns the next result. Its second return value is iterator.Done if there are no more 866// results. Once Next returns Done, all subsequent calls will return Done. 867func (it *TaskIterator) Next() (*taskspb.Task, error) { 868 var item *taskspb.Task 869 if err := it.nextFunc(); err != nil { 870 return item, err 871 } 872 item = it.items[0] 873 it.items = it.items[1:] 874 return item, nil 875} 876 877func (it *TaskIterator) bufLen() int { 878 return len(it.items) 879} 880 881func (it *TaskIterator) takeBuf() interface{} { 882 b := it.items 883 it.items = nil 884 return b 885} 886