@@ -110,24 +110,24 @@ func WithTickChannel(c chan time.Time) Option {
110
110
111
111
// Add marks the workspace with the given ID as having been used recently.
112
112
// Tracker will periodically flush this to its configured Store.
113
- func (wut * Tracker ) Add (workspaceID uuid.UUID ) {
114
- wut .m .Add (workspaceID )
113
+ func (tr * Tracker ) Add (workspaceID uuid.UUID ) {
114
+ tr .m .Add (workspaceID )
115
115
}
116
116
117
117
// flush updates last_used_at of all current workspace IDs.
118
118
// If this is held while a previous flush is in progress, it will
119
119
// deadlock until the previous flush has completed.
120
- func (wut * Tracker ) flush (now time.Time ) {
120
+ func (tr * Tracker ) flush (now time.Time ) {
121
121
// Copy our current set of IDs
122
- ids := wut .m .UniqueAndClear ()
122
+ ids := tr .m .UniqueAndClear ()
123
123
count := len (ids )
124
- if wut .flushCh != nil { // only used for testing
124
+ if tr .flushCh != nil { // only used for testing
125
125
defer func () {
126
- wut .flushCh <- count
126
+ tr .flushCh <- count
127
127
}()
128
128
}
129
129
if count == 0 {
130
- wut .log .Debug (context .Background (), "nothing to flush" )
130
+ tr .log .Debug (context .Background (), "nothing to flush" )
131
131
return
132
132
}
133
133
@@ -136,47 +136,47 @@ func (wut *Tracker) flush(now time.Time) {
136
136
defer cancel ()
137
137
// nolint: gocritic // system function
138
138
authCtx := dbauthz .AsSystemRestricted (ctx )
139
- wut .flushLock .Lock ()
140
- defer wut .flushLock .Unlock ()
141
- if err := wut .s .BatchUpdateWorkspaceLastUsedAt (authCtx , database.BatchUpdateWorkspaceLastUsedAtParams {
139
+ tr .flushLock .Lock ()
140
+ defer tr .flushLock .Unlock ()
141
+ if err := tr .s .BatchUpdateWorkspaceLastUsedAt (authCtx , database.BatchUpdateWorkspaceLastUsedAtParams {
142
142
LastUsedAt : now ,
143
143
IDs : ids ,
144
144
}); err != nil {
145
145
// A single failure to flush is likely not a huge problem. If the workspace is still connected at
146
146
// the next iteration, either another coderd instance will likely have this data or the CLI
147
147
// will tell us again that the workspace is in use.
148
- wut .flushErrors ++
149
- if wut .flushErrors > 1 {
150
- wut .log .Error (ctx , "multiple failures updating workspaces last_used_at" , slog .F ("count" , count ), slog .F ("consecutive_errors" , wut .flushErrors ), slog .Error (err ))
148
+ tr .flushErrors ++
149
+ if tr .flushErrors > 1 {
150
+ tr .log .Error (ctx , "multiple failures updating workspaces last_used_at" , slog .F ("count" , count ), slog .F ("consecutive_errors" , tr .flushErrors ), slog .Error (err ))
151
151
// TODO: if this keeps failing, it indicates a fundamental problem with the database connection.
152
152
// How to surface it correctly to admins besides just screaming into the logs?
153
153
} else {
154
- wut .log .Warn (ctx , "failed updating workspaces last_used_at" , slog .F ("count" , count ), slog .Error (err ))
154
+ tr .log .Warn (ctx , "failed updating workspaces last_used_at" , slog .F ("count" , count ), slog .Error (err ))
155
155
}
156
156
return
157
157
}
158
- wut .flushErrors = 0
159
- wut .log .Info (ctx , "updated workspaces last_used_at" , slog .F ("count" , count ), slog .F ("now" , now ))
158
+ tr .flushErrors = 0
159
+ tr .log .Info (ctx , "updated workspaces last_used_at" , slog .F ("count" , count ), slog .F ("now" , now ))
160
160
}
161
161
162
162
// Loop periodically flushes every tick.
163
163
// If Loop is called after Close, it will panic. Don't do this.
164
- func (wut * Tracker ) Loop () {
164
+ func (tr * Tracker ) Loop () {
165
165
// Calling Loop after Close() is an error.
166
166
select {
167
- case <- wut .doneCh :
167
+ case <- tr .doneCh :
168
168
panic ("developer error: Loop called after Close" )
169
169
default :
170
170
}
171
171
defer func () {
172
- wut .log .Debug (context .Background (), "workspace usage tracker loop exited" )
172
+ tr .log .Debug (context .Background (), "workspace usage tracker loop exited" )
173
173
}()
174
174
for {
175
175
select {
176
- case <- wut .stopCh :
177
- close (wut .doneCh )
176
+ case <- tr .stopCh :
177
+ close (tr .doneCh )
178
178
return
179
- case now , ok := <- wut .tickCh :
179
+ case now , ok := <- tr .tickCh :
180
180
if ! ok {
181
181
return
182
182
}
@@ -186,18 +186,18 @@ func (wut *Tracker) Loop() {
186
186
// then we could capture the exact usage time of each workspace. For now however, as
187
187
// we perform this query at a regular interval, the time of the flush is 'close enough'
188
188
// for the purposes of both dormancy (and for autostop, in future).
189
- wut .flush (now .UTC ())
189
+ tr .flush (now .UTC ())
190
190
}
191
191
}
192
192
}
193
193
194
194
// Close stops Tracker and returns once Loop has exited.
195
195
// After calling Close(), Loop must not be called.
196
- func (wut * Tracker ) Close () {
197
- wut .stopOnce .Do (func () {
198
- wut .stopCh <- struct {}{}
199
- wut .stopTick ()
200
- <- wut .doneCh
196
+ func (tr * Tracker ) Close () {
197
+ tr .stopOnce .Do (func () {
198
+ tr .stopCh <- struct {}{}
199
+ tr .stopTick ()
200
+ <- tr .doneCh
201
201
})
202
202
}
203
203
0 commit comments