TriggerParserAndAwaitResource triggers the parser and polls the runtime until the given resource's spec version has been updated (or ctx is canceled).
(ctx context.Context, depl *database.Deployment, name, kind string)
| 474 | |
| 475 | // TriggerParserAndAwaitResource triggers the parser and polls the runtime until the given resource's spec version has been updated (or ctx is canceled). |
| 476 | func (s *Service) TriggerParserAndAwaitResource(ctx context.Context, depl *database.Deployment, name, kind string) error { |
| 477 | rt, err := s.OpenRuntimeClient(depl) |
| 478 | if err != nil { |
| 479 | return err |
| 480 | } |
| 481 | defer rt.Close() |
| 482 | |
| 483 | resourceReq := &runtimev1.GetResourceRequest{ |
| 484 | InstanceId: depl.RuntimeInstanceID, |
| 485 | Name: &runtimev1.ResourceName{ |
| 486 | Kind: kind, |
| 487 | Name: name, |
| 488 | }, |
| 489 | } |
| 490 | |
| 491 | // Get old spec version |
| 492 | var oldSpecVersion *int64 |
| 493 | r, err := rt.GetResource(ctx, resourceReq) |
| 494 | if err == nil { |
| 495 | oldSpecVersion = &r.Resource.Meta.SpecVersion |
| 496 | } |
| 497 | |
| 498 | // Trigger parser |
| 499 | _, err = rt.CreateTrigger(ctx, &runtimev1.CreateTriggerRequest{ |
| 500 | InstanceId: depl.RuntimeInstanceID, |
| 501 | Parser: true, |
| 502 | }) |
| 503 | if err != nil { |
| 504 | return err |
| 505 | } |
| 506 | |
| 507 | // Poll every 1 seconds until the resource is found or the ctx is cancelled or times out |
| 508 | ctx, cancel := context.WithTimeout(ctx, time.Minute) |
| 509 | defer cancel() |
| 510 | ticker := time.NewTicker(time.Second) |
| 511 | defer ticker.Stop() |
| 512 | for { |
| 513 | select { |
| 514 | case <-ctx.Done(): |
| 515 | return ctx.Err() |
| 516 | case <-ticker.C: |
| 517 | } |
| 518 | |
| 519 | r, err := rt.GetResource(ctx, resourceReq) |
| 520 | if err != nil { |
| 521 | if s, ok := status.FromError(err); !ok || s.Code() != codes.NotFound { |
| 522 | return fmt.Errorf("failed to poll for resource: %w", err) |
| 523 | } |
| 524 | if oldSpecVersion != nil { |
| 525 | // Success - previously the resource was found, now we cannot find it anymore |
| 526 | return nil |
| 527 | } |
| 528 | // Continue polling |
| 529 | continue |
| 530 | } |
| 531 | if oldSpecVersion == nil { |
| 532 | // Success - previously the resource was not found, now we found one |
| 533 | return nil |
no test coverage detected