WaitForReinit polls a SSE endpoint, and receives an event back under the following conditions: - ping: ignored, keepalive - prebuild claimed: a prebuilt workspace is claimed, so the agent must reinitialize.
(ctx context.Context)
| 791 | // - ping: ignored, keepalive |
| 792 | // - prebuild claimed: a prebuilt workspace is claimed, so the agent must reinitialize. |
| 793 | func (c *Client) WaitForReinit(ctx context.Context) (*ReinitializationEvent, error) { |
| 794 | rpcURL, err := c.SDK.URL.Parse("/api/v2/workspaceagents/me/reinit") |
| 795 | if err != nil { |
| 796 | return nil, xerrors.Errorf("parse url: %w", err) |
| 797 | } |
| 798 | q := rpcURL.Query() |
| 799 | q.Set("wait", "true") |
| 800 | rpcURL.RawQuery = q.Encode() |
| 801 | |
| 802 | httpClient := &http.Client{ |
| 803 | Transport: c.SDK.HTTPClient.Transport, |
| 804 | } |
| 805 | |
| 806 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, rpcURL.String(), nil) |
| 807 | if err != nil { |
| 808 | return nil, xerrors.Errorf("build request: %w", err) |
| 809 | } |
| 810 | req.Header[codersdk.SessionTokenHeader] = []string{c.SDK.SessionToken()} |
| 811 | |
| 812 | res, err := httpClient.Do(req) |
| 813 | if err != nil { |
| 814 | return nil, xerrors.Errorf("execute request: %w", err) |
| 815 | } |
| 816 | defer res.Body.Close() |
| 817 | |
| 818 | if res.StatusCode != http.StatusOK { |
| 819 | return nil, codersdk.ReadBodyAsError(res) |
| 820 | } |
| 821 | |
| 822 | reinitEvent, err := NewSSEAgentReinitReceiver(res.Body).Receive(ctx) |
| 823 | if err != nil { |
| 824 | return nil, xerrors.Errorf("listening for reinitialization events: %w", err) |
| 825 | } |
| 826 | return reinitEvent, nil |
| 827 | } |
| 828 | |
| 829 | // WaitForReinitLoop polls the /reinit SSE endpoint in a retry loop and |
| 830 | // forwards received reinitialization events to the returned channel. The |