loop periodically flushes every tick. If loop is called after Close, it will exit immediately and log an error.
()
| 153 | // loop periodically flushes every tick. |
| 154 | // If loop is called after Close, it will exit immediately and log an error. |
| 155 | func (tr *UsageTracker) loop() { |
| 156 | select { |
| 157 | case <-tr.doneCh: |
| 158 | tr.log.Error(context.Background(), "developer error: Loop called after Close") |
| 159 | return |
| 160 | default: |
| 161 | } |
| 162 | defer func() { |
| 163 | close(tr.doneCh) |
| 164 | tr.log.Debug(context.Background(), "workspace usage tracker loop exited") |
| 165 | }() |
| 166 | for { |
| 167 | select { |
| 168 | case <-tr.stopCh: |
| 169 | return |
| 170 | case now, ok := <-tr.tickCh: |
| 171 | if !ok { |
| 172 | return |
| 173 | } |
| 174 | // NOTE: we do not update last_used_at with the time at which each workspace was added. |
| 175 | // Instead, we update with the time of the flush. If the BatchUpdateWorkspacesLastUsedAt |
| 176 | // query can be rewritten to update each id with a corresponding last_used_at timestamp |
| 177 | // then we could capture the exact usage time of each workspace. For now however, as |
| 178 | // we perform this query at a regular interval, the time of the flush is 'close enough' |
| 179 | // for the purposes of both dormancy (and for autostop, in future). |
| 180 | tr.flush(now.UTC()) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Close stops Tracker and returns once Loop has exited. |
| 186 | // After calling Close(), Loop must not be called. |
no test coverage detected