|
5 | 5 | package activity
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "sync" |
| 8 | + "context" |
9 | 9 | "time"
|
10 | 10 |
|
| 11 | + "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/apimachinery/pkg/util/wait" |
| 15 | + "k8s.io/client-go/util/retry" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + |
| 18 | + "github.com/gitpod-io/gitpod/common-go/log" |
11 | 19 | workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
|
12 | 20 | )
|
13 | 21 |
|
14 | 22 | // WorkspaceActivity is used to track the last user activity per workspace. This is
|
15 | 23 | // stored in memory instead of on the Workspace resource to limit load on the k8s API,
|
16 | 24 | // as this value will update often for each workspace.
|
17 | 25 | type WorkspaceActivity struct {
|
| 26 | + client client.Client |
| 27 | + namespace string |
| 28 | + |
18 | 29 | ManagerStartedAt time.Time
|
19 |
| - m sync.Map |
20 | 30 | }
|
21 | 31 |
|
22 |
| -func NewWorkspaceActivity() *WorkspaceActivity { |
| 32 | +func NewWorkspaceActivity(namespace string, client client.Client) *WorkspaceActivity { |
23 | 33 | return &WorkspaceActivity{
|
24 | 34 | ManagerStartedAt: time.Now().UTC(),
|
| 35 | + client: client, |
| 36 | + namespace: namespace, |
25 | 37 | }
|
26 | 38 | }
|
27 | 39 |
|
| 40 | +var ( |
| 41 | + // retryParams are custom backoff parameters used to modify a workspace. |
| 42 | + // These params retry more quickly than the default retry.DefaultBackoff. |
| 43 | + retryParams = wait.Backoff{ |
| 44 | + Steps: 10, |
| 45 | + Duration: 10 * time.Millisecond, |
| 46 | + Factor: 2.0, |
| 47 | + Jitter: 0.2, |
| 48 | + } |
| 49 | +) |
| 50 | + |
28 | 51 | func (w *WorkspaceActivity) Store(workspaceId string, lastActivity time.Time) {
|
29 |
| - w.m.Store(workspaceId, &lastActivity) |
| 52 | + ctx, cancel := context.WithCancel(context.Background()) |
| 53 | + defer cancel() |
| 54 | + |
| 55 | + var ws workspacev1.Workspace |
| 56 | + err := w.client.Get(ctx, types.NamespacedName{Namespace: w.namespace, Name: workspaceId}, &ws) |
| 57 | + if err != nil { |
| 58 | + log.Error(err, "cannot store workspace last activity") |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + lastActivityStatus := metav1.NewTime(lastActivity) |
| 63 | + ws.Status.LastActivity = &lastActivityStatus |
| 64 | + |
| 65 | + err = retry.RetryOnConflict(retryParams, func() error { |
| 66 | + var ws workspacev1.Workspace |
| 67 | + err := w.client.Get(ctx, types.NamespacedName{Namespace: w.namespace, Name: workspaceId}, &ws) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + return w.client.Status().Update(ctx, &ws) |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + log.Error(err, "cannot update workspace status") |
| 76 | + } |
30 | 77 | }
|
31 | 78 |
|
32 | 79 | func (w *WorkspaceActivity) GetLastActivity(ws *workspacev1.Workspace) *time.Time {
|
33 |
| - lastActivity, ok := w.m.Load(ws.Name) |
34 |
| - if ok { |
35 |
| - return lastActivity.(*time.Time) |
| 80 | + ctx, cancel := context.WithCancel(context.Background()) |
| 81 | + defer cancel() |
| 82 | + |
| 83 | + var workspace workspacev1.Workspace |
| 84 | + err := w.client.Get(ctx, types.NamespacedName{ |
| 85 | + Namespace: ws.Namespace, |
| 86 | + Name: ws.Name, |
| 87 | + }, &workspace) |
| 88 | + if err != nil { |
| 89 | + if !errors.IsNotFound(err) { |
| 90 | + log.Error(err, "unable to fetch workspace") |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + if workspace.Status.LastActivity != nil { |
| 97 | + return &workspace.Status.LastActivity.Time |
36 | 98 | }
|
37 | 99 |
|
38 | 100 | // In case we don't have a record of the workspace's last activity, check for the FirstUserActivity condition
|
39 | 101 | // to see if the lastActivity got lost on a manager restart.
|
40 |
| - if ws.IsConditionTrue(workspacev1.WorkspaceConditionFirstUserActivity) { |
| 102 | + if workspace.IsConditionTrue(workspacev1.WorkspaceConditionFirstUserActivity) { |
41 | 103 | // Manager was restarted, consider the workspace's last activity to be the time the manager restarted.
|
42 | 104 | return &w.ManagerStartedAt
|
43 | 105 | }
|
|
0 commit comments