mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
* Add startup script logs to the database * Add coderd endpoints for startup script logs * Push startup script logs from agent * Pull startup script logs on frontend * Rename queries * Add constraint * Start creating log sending loop * Add log sending to the agent * Add tests for streaming logs * Shorten notify channel name * Add FE * Improve bulk log performance * Finish UI display * Fix startup log visibility * Add warning for overflow * Fix agent queue logs overflow * Display staartup logs in a virtual DOM for performance * Fix agent queue with loads of logs * Fix authorize test * Remove faulty test * Fix startup and shutdown reporting error * Fix gen * Fix comments * Periodically purge old database entries * Add test fixture for migration * Add Storybook * Check if there are logs when displaying features * Fix startup component overflow gap * Fix startup log wrapping --------- Co-authored-by: Asher <ash@coder.com>
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package dbpurge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"time"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"cdr.dev/slog"
|
|
"github.com/coder/coder/coderd/database"
|
|
)
|
|
|
|
// New creates a new periodically purging database instance.
|
|
// It is the caller's responsibility to call Close on the returned instance.
|
|
//
|
|
// This is for cleaning up old, unused resources from the database that take up space.
|
|
func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
|
|
closed := make(chan struct{})
|
|
ctx, cancelFunc := context.WithCancel(ctx)
|
|
go func() {
|
|
defer close(closed)
|
|
ticker := time.NewTicker(24 * time.Hour)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
}
|
|
|
|
var eg errgroup.Group
|
|
eg.Go(func() error {
|
|
return db.DeleteOldWorkspaceAgentStartupLogs(ctx)
|
|
})
|
|
eg.Go(func() error {
|
|
return db.DeleteOldWorkspaceAgentStats(ctx)
|
|
})
|
|
err := eg.Wait()
|
|
if err != nil {
|
|
if errors.Is(err, context.Canceled) {
|
|
return
|
|
}
|
|
logger.Error(ctx, "failed to purge old database entries", slog.Error(err))
|
|
}
|
|
}
|
|
}()
|
|
return &instance{
|
|
cancel: cancelFunc,
|
|
closed: closed,
|
|
}
|
|
}
|
|
|
|
type instance struct {
|
|
cancel context.CancelFunc
|
|
closed chan struct{}
|
|
}
|
|
|
|
func (i *instance) Close() error {
|
|
i.cancel()
|
|
<-i.closed
|
|
return nil
|
|
}
|