Skip to content

Commit 1bb700f

Browse files
committed
Add log sending to the agent
1 parent 0c4d2c3 commit 1bb700f

File tree

8 files changed

+86
-219
lines changed

8 files changed

+86
-219
lines changed

agent/agent.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"cdr.dev/slog"
4242
"github.com/coder/coder/agent/usershell"
4343
"github.com/coder/coder/buildinfo"
44+
"github.com/coder/coder/coderd/database"
4445
"github.com/coder/coder/coderd/gitauth"
4546
"github.com/coder/coder/codersdk"
4647
"github.com/coder/coder/codersdk/agentsdk"
@@ -662,16 +663,23 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) error {
662663
}()
663664

664665
startupLogsReader, startupLogsWriter := io.Pipe()
666+
defer func() {
667+
_ = startupLogsReader.Close()
668+
_ = startupLogsWriter.Close()
669+
}()
665670
writer := io.MultiWriter(startupLogsWriter, fileWriter)
666671

667672
queuedLogs := make([]agentsdk.StartupLog, 0)
668673
var flushLogsTimer *time.Timer
669674
var logMutex sync.Mutex
670-
flushQueuedLogs := func() {
675+
var logsSending bool
676+
sendLogs := func() {
671677
logMutex.Lock()
672-
if flushLogsTimer != nil {
673-
flushLogsTimer.Stop()
678+
if logsSending {
679+
logMutex.Unlock()
680+
return
674681
}
682+
logsSending = true
675683
toSend := make([]agentsdk.StartupLog, len(queuedLogs))
676684
copy(toSend, queuedLogs)
677685
logMutex.Unlock()
@@ -683,9 +691,13 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) error {
683691
a.logger.Error(ctx, "upload startup logs", slog.Error(err))
684692
}
685693
if ctx.Err() != nil {
694+
logMutex.Lock()
695+
logsSending = false
696+
logMutex.Unlock()
686697
return
687698
}
688699
logMutex.Lock()
700+
logsSending = false
689701
queuedLogs = queuedLogs[len(toSend):]
690702
logMutex.Unlock()
691703
}
@@ -698,25 +710,25 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) error {
698710
return
699711
}
700712
if len(queuedLogs) > 100 {
701-
go flushQueuedLogs()
713+
go sendLogs()
702714
return
703715
}
716+
flushLogsTimer = time.AfterFunc(100*time.Millisecond, func() {
717+
sendLogs()
718+
})
704719
}
705-
go func() {
720+
err = a.trackConnGoroutine(func() {
706721
scanner := bufio.NewScanner(startupLogsReader)
707722
for scanner.Scan() {
708-
723+
queueLog(agentsdk.StartupLog{
724+
CreatedAt: database.Now(),
725+
Output: scanner.Text(),
726+
})
709727
}
710-
}()
711-
defer func() {
712-
713-
// err := a.client.AppendStartupLogs(ctx, agentsdk.InsertOrUpdateStartupLogsRequest{
714-
// Output: string(saver.Bytes()),
715-
// })
716-
// if err != nil {
717-
// a.logger.Error(ctx, "upload startup logs", slog.Error(err))
718-
// }
719-
}()
728+
})
729+
if err != nil {
730+
return xerrors.Errorf("track conn goroutine: %w", err)
731+
}
720732

721733
cmd, err := a.createCommand(ctx, script, nil)
722734
if err != nil {

agent/agent_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,17 +740,22 @@ func TestAgent_StartupScript(t *testing.T) {
740740
if runtime.GOOS == "windows" {
741741
t.Skip("This test doesn't work on Windows for some reason...")
742742
}
743-
content := "output\n"
743+
output := "something"
744+
command := "sh -c 'echo " + output + "'"
745+
if runtime.GOOS == "windows" {
746+
command = "cmd.exe /c echo " + output
747+
}
744748
//nolint:dogsled
745749
_, client, _, _, _ := setupAgent(t, agentsdk.Metadata{
746-
StartupScript: "echo " + content,
750+
StartupScript: command,
747751
}, 0)
748752
assert.Eventually(t, func() bool {
749753
got := client.getLifecycleStates()
750754
return len(got) > 0 && got[len(got)-1] == codersdk.WorkspaceAgentLifecycleReady
751755
}, testutil.WaitShort, testutil.IntervalMedium)
752756

753757
require.Len(t, client.getStartupLogs(), 1)
758+
require.Equal(t, output, client.getStartupLogs()[0].Output)
754759
}
755760

756761
func TestAgent_Lifecycle(t *testing.T) {

coderd/apidoc/docs.go

Lines changed: 15 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 15 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/workspaceagents.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (api *API) postWorkspaceAgentStartup(rw http.ResponseWriter, r *http.Reques
226226
// @Accept json
227227
// @Produce json
228228
// @Tags Agents
229-
// @Param request body agentsdk.InsertOrUpdateStartupLogsRequest true "Startup logs"
229+
// @Param request body []agentsdk.StartupLog true "Startup logs"
230230
// @Success 200
231231
// @Router /workspaceagents/me/startup-logs [patch]
232232
// @x-apidocgen {"skip": true}

coderd/wsconncache/wsconncache_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,7 @@ func (*client) PostAppHealth(_ context.Context, _ agentsdk.PostAppHealthsRequest
249249
func (*client) PostStartup(_ context.Context, _ agentsdk.PostStartupRequest) error {
250250
return nil
251251
}
252+
253+
func (*client) AppendStartupLogs(_ context.Context, _ []agentsdk.StartupLog) error {
254+
return nil
255+
}

0 commit comments

Comments
 (0)