Attempt to poll workspace autostop. We write a per-workspace lockfile to avoid spamming the user with notifications in case of multiple instances of the CLI running simultaneously.
(ctx context.Context, client *codersdk.Client, workspace codersdk.Workspace)
| 1138 | // avoid spamming the user with notifications in case of multiple instances |
| 1139 | // of the CLI running simultaneously. |
| 1140 | func tryPollWorkspaceAutostop(ctx context.Context, client *codersdk.Client, workspace codersdk.Workspace) (stop func()) { |
| 1141 | lock := flock.New(filepath.Join(os.TempDir(), "coder-autostop-notify-"+workspace.ID.String())) |
| 1142 | conditionCtx, cancelCondition := context.WithCancel(ctx) |
| 1143 | condition := notifyCondition(conditionCtx, client, workspace.ID, lock) |
| 1144 | notifier := notify.New(condition, workspacePollInterval, autostopNotifyCountdown) |
| 1145 | return func() { |
| 1146 | // With many "ssh" processes running, `lock.TryLockContext` can be hanging until the context canceled. |
| 1147 | // Without this cancellation, a CLI process with failed remote-forward could be hanging indefinitely. |
| 1148 | cancelCondition() |
| 1149 | notifier.Close() |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | // Notify the user if the workspace is due to shutdown. |
| 1154 | func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID uuid.UUID, lock *flock.Flock) notify.Condition { |