UpdateWorkspaceUsageWithBodyContext periodically posts workspace usage for the workspace with the given id and app name in the background. The caller is responsible for calling the returned function to stop the background process.
(ctx context.Context, workspaceID uuid.UUID, req PostWorkspaceUsageRequest)
| 403 | // The caller is responsible for calling the returned function to stop the background |
| 404 | // process. |
| 405 | func (c *Client) UpdateWorkspaceUsageWithBodyContext(ctx context.Context, workspaceID uuid.UUID, req PostWorkspaceUsageRequest) func() { |
| 406 | hbCtx, hbCancel := context.WithCancel(ctx) |
| 407 | // Perform one initial update |
| 408 | err := c.PostWorkspaceUsageWithBody(hbCtx, workspaceID, req) |
| 409 | if err != nil { |
| 410 | c.logger.Warn(ctx, "failed to post workspace usage", slog.Error(err)) |
| 411 | } |
| 412 | ticker := time.NewTicker(time.Minute) |
| 413 | doneCh := make(chan struct{}) |
| 414 | go func() { |
| 415 | defer func() { |
| 416 | ticker.Stop() |
| 417 | close(doneCh) |
| 418 | }() |
| 419 | for { |
| 420 | select { |
| 421 | case <-ticker.C: |
| 422 | err := c.PostWorkspaceUsageWithBody(hbCtx, workspaceID, req) |
| 423 | if err != nil { |
| 424 | c.logger.Warn(ctx, "failed to post workspace usage in background", slog.Error(err)) |
| 425 | } |
| 426 | case <-hbCtx.Done(): |
| 427 | return |
| 428 | } |
| 429 | } |
| 430 | }() |
| 431 | return func() { |
| 432 | hbCancel() |
| 433 | <-doneCh |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // UpdateWorkspaceUsageContext periodically posts workspace usage for the workspace |
| 438 | // with the given id in the background. |
no test coverage detected