MCPcopy Index your code
hub / github.com/coder/coder / WaitForReinit

Method WaitForReinit

codersdk/agentsdk/agentsdk.go:793–827  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

791// - ping: ignored, keepalive
792// - prebuild claimed: a prebuilt workspace is claimed, so the agent must reinitialize.
793func (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

Callers 2

TestReinitFunction · 0.80
WaitForReinitLoopFunction · 0.80

Calls 11

ReadBodyAsErrorFunction · 0.92
EncodeMethod · 0.80
ReceiveMethod · 0.80
ParseMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45
SessionTokenMethod · 0.45

Tested by 1

TestReinitFunction · 0.64