Skip to content

Commit decde5c

Browse files
committed
Fix agent queue with loads of logs
1 parent 379f1f4 commit decde5c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

agent/agent.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,13 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) error {
666666
var flushLogsTimer *time.Timer
667667
var logMutex sync.Mutex
668668
var logsSending bool
669+
defer func() {
670+
logMutex.Lock()
671+
if flushLogsTimer != nil {
672+
flushLogsTimer.Stop()
673+
}
674+
logMutex.Unlock()
675+
}()
669676

670677
// sendLogs function uploads the queued logs to the server
671678
sendLogs := func() {
@@ -708,6 +715,7 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) error {
708715
// Reset logsSending flag
709716
logMutex.Lock()
710717
logsSending = false
718+
flushLogsTimer.Reset(100 * time.Millisecond)
711719
logMutex.Unlock()
712720
}
713721
// queueLog function appends a log to the queue and triggers sendLogs if necessary
@@ -720,8 +728,10 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) error {
720728

721729
// If there are more than 100 logs, send them immediately
722730
if len(queuedLogs) > 100 {
731+
// Don't early return after this, because we still want
732+
// to reset the timer just in case logs come in while
733+
// we're sending.
723734
go sendLogs()
724-
return
725735
}
726736
// Reset or set the flushLogsTimer to trigger sendLogs after 100 milliseconds
727737
if flushLogsTimer != nil {

cli/agent.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ func workspaceAgent() *cobra.Command {
117117
client := agentsdk.New(coderURL)
118118
client.SDK.Logger = logger
119119
// Set a reasonable timeout so requests can't hang forever!
120-
client.SDK.HTTPClient.Timeout = 10 * time.Second
120+
// The timeout needs to be reasonably long, because requests
121+
// with large payloads can take a bit. e.g. startup scripts
122+
// may take a while to insert.
123+
client.SDK.HTTPClient.Timeout = 30 * time.Second
121124

122125
// Enable pprof handler
123126
// This prevents the pprof import from being accidentally deleted.

site/src/components/Resources/AgentRow.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,17 @@ export const AgentRow: FC<AgentRowProps> = ({
9797
return logs
9898
}, [logsMachine.context.startupLogs, agent.startup_logs_overflowed])
9999
const [bottomOfLogs, setBottomOfLogs] = useState(true)
100+
// This is a layout effect to remove flicker when we're scrolling to the bottom.
100101
useLayoutEffect(() => {
102+
// If we're currently watching the bottom, we always want to stay at the bottom.
101103
if (bottomOfLogs && logListRef.current) {
102104
logListRef.current.scrollToItem(startupLogs.length - 1, "end")
103105
}
104106
}, [showStartupLogs, startupLogs, logListRef, bottomOfLogs])
107+
108+
// This is a bit of a hack on the react-window API to get the scroll position.
109+
// If we're scrolled to the bottom, we want to keep the list scrolled to the bottom.
110+
// This makes it feel similar to a terminal that auto-scrolls downwards!
105111
const handleLogScroll = useCallback(
106112
(props: ListOnScrollProps) => {
107113
if (

0 commit comments

Comments
 (0)