9
9
"errors"
10
10
"io/fs"
11
11
"io/ioutil"
12
- "os"
13
12
"path/filepath"
14
13
"regexp"
15
14
"strconv"
@@ -18,7 +17,6 @@ import (
18
17
"time"
19
18
20
19
"github.com/shirou/gopsutil/process"
21
- "golang.org/x/xerrors"
22
20
23
21
"github.com/gitpod-io/gitpod/common-go/log"
24
22
)
@@ -39,6 +37,9 @@ const (
39
37
ProcessCodeServerHelper ProcessType = "vscode-server-helper"
40
38
// ProcessDefault referes to any process that is not one of the above
41
39
ProcessDefault ProcessType = "default"
40
+
41
+ // Repeat applying until this number of processes is reached
42
+ NumberOfProcessesToStopApllying = 5
42
43
)
43
44
44
45
type ProcessPriorityV2 struct {
@@ -51,19 +52,28 @@ func (c *ProcessPriorityV2) Type() Version { return Version2 }
51
52
func (c * ProcessPriorityV2 ) Apply (ctx context.Context , opts * PluginOptions ) error {
52
53
fullCgroupPath := filepath .Join (opts .BasePath , opts .CgroupPath )
53
54
54
- go func () {
55
- time .Sleep (10 * time .Second )
55
+ t := time .NewTicker (10 * time .Second )
56
+ defer t .Stop ()
57
+
58
+ for {
59
+ select {
60
+ case <- ctx .Done ():
61
+ return nil
62
+ case <- t .C :
63
+ }
56
64
57
65
data , err := ioutil .ReadFile (filepath .Join (fullCgroupPath , "workspace" , "user" , "cgroup.procs" ))
58
66
if errors .Is (err , fs .ErrNotExist ) {
59
- log . WithField ( "path" , fullCgroupPath ). WithError ( err ). Warn ( " the target cgroup has gone" )
60
- return
67
+ // the target cgroup/workspace has gone
68
+ return nil
61
69
} else if err != nil {
62
70
log .WithField ("path" , fullCgroupPath ).WithError (err ).Errorf ("cannot read cgroup.procs file" )
63
- return
71
+ return err
64
72
}
65
73
66
- for _ , line := range strings .Split (string (data ), "\n " ) {
74
+ var countRunningProcess int
75
+ lines := strings .Split (string (data ), "\n " )
76
+ for _ , line := range lines {
67
77
line = strings .TrimSpace (line )
68
78
if len (line ) == 0 {
69
79
continue
@@ -95,14 +105,17 @@ func (c *ProcessPriorityV2) Apply(ctx context.Context, opts *PluginOptions) erro
95
105
continue
96
106
}
97
107
108
+ countRunningProcess += 1
98
109
err = syscall .Setpriority (syscall .PRIO_PROCESS , int (pid ), priority )
99
110
if err != nil {
100
111
log .WithError (err ).WithField ("pid" , pid ).WithField ("priority" , priority ).Warn ("cannot set process priority" )
101
112
}
102
113
}
103
- }()
104
114
105
- return nil
115
+ if countRunningProcess >= NumberOfProcessesToStopApllying {
116
+ return nil
117
+ }
118
+ }
106
119
}
107
120
108
121
var (
0 commit comments