FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stackrox/pkg/backgroundtasks/manager_impl.go at master · stackrox/stackrox · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stackrox
/
stackrox
Public
Notifications
You must be signed in to change notification settings
Fork
191
Star
1.3k
Code
Issues
54
Pull requests
604
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
stackrox
/
pkg
/
backgroundtasks
/
manager_impl.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
231 lines (191 loc) · 5.55 KB
Breadcrumbs
stackrox
/
pkg
/
backgroundtasks
/
manager_impl.go
Copy path
File metadata and controls
231 lines (191 loc) · 5.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package
backgroundtasks
import
(
"context"
"errors"
"fmt"
"time"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/pkg/uuid"
)
var
cancelExecutionMarker
=
func
() {}
type
taskWrapper
struct
{
// Task id.
id
string
// Function to execute.
exec
Task
// Metadata related to task.
metadata
map
[
string
]
interface
{}
// Results from execution of task.
result
interface
{}
// Errors from execution of task.
err
error
// Cancels the execution of task.
cancel
context.
CancelFunc
}
func
(
t
*
taskWrapper
)
execute
(
ctx
context.
Context
) (
res
interface
{},
err
error
) {
if
err
=
ctx
.
Err
();
err
!=
nil
{
return
}
panicked
:=
true
defer
func
() {
if
p
:=
recover
();
p
!=
nil
||
panicked
{
err
=
fmt
.
Errorf
(
"Panic during execution of task: %v"
,
p
)
}
}()
res
,
err
=
t
.
exec
(
ctx
)
panicked
=
false
return
}
type
managerImpl
struct
{
existingTasks
map
[
string
]
*
taskWrapper
completedTasks
map
[
string
]time.
Time
pendingTasks
chan
*
taskWrapper
existingTasksMutex
sync.
Mutex
completedTasksMutex
sync.
Mutex
cancelFuncTasksMutex
*
concurrency.
KeyedMutex
parallel
chan
struct
{}
completedTaskExpiryTime
time.
Duration
cleanUpInterval
time.
Duration
}
// WithExpirationCompletedTasks sets the duration after which completed tasks are cleaned up.
func
WithExpirationCompletedTasks
(
t
time.
Duration
)
ManagerOption
{
return
func
(
m
*
managerImpl
) {
m
.
completedTaskExpiryTime
=
t
}
}
// WithMaxTasksInParallel sets the max tasks that are allowed to run in parallel.
func
WithMaxTasksInParallel
(
p
int
)
ManagerOption
{
return
func
(
m
*
managerImpl
) {
m
.
parallel
=
make
(
chan
struct
{},
p
)
m
.
cancelFuncTasksMutex
=
concurrency
.
NewKeyedMutex
(
uint32
(
cap
(
m
.
parallel
)
+
cap
(
m
.
pendingTasks
)))
}
}
// WithMaxPendingTaskQueueSize sets the max pending job queue size.
func
WithMaxPendingTaskQueueSize
(
sz
int
)
ManagerOption
{
return
func
(
m
*
managerImpl
) {
m
.
pendingTasks
=
make
(
chan
*
taskWrapper
,
sz
)
m
.
cancelFuncTasksMutex
=
concurrency
.
NewKeyedMutex
(
uint32
(
cap
(
m
.
parallel
)
+
cap
(
m
.
pendingTasks
)))
}
}
// WithCleanUpInterval sets the duration after which the manager wakes up to perform it's clean up jobs.
func
WithCleanUpInterval
(
t
time.
Duration
)
ManagerOption
{
return
func
(
m
*
managerImpl
) {
m
.
cleanUpInterval
=
t
}
}
func
(
m
*
managerImpl
)
applyDefaults
() {
m
.
parallel
=
make
(
chan
struct
{},
8
)
m
.
completedTaskExpiryTime
=
20
*
time
.
Minute
m
.
pendingTasks
=
make
(
chan
*
taskWrapper
,
256
)
m
.
cleanUpInterval
=
5
*
time
.
Minute
m
.
existingTasks
=
make
(
map
[
string
]
*
taskWrapper
)
m
.
completedTasks
=
make
(
map
[
string
]time.
Time
)
m
.
cancelFuncTasksMutex
=
concurrency
.
NewKeyedMutex
(
uint32
(
cap
(
m
.
parallel
)
+
cap
(
m
.
pendingTasks
)))
}
// AddTask adds a task to its pending task list to be run as a background process and returns a jobid.
func
(
m
*
managerImpl
)
AddTask
(
metadata
map
[
string
]
interface
{},
task
Task
) (
string
,
error
) {
t
:=
&
taskWrapper
{
exec
:
task
,
metadata
:
metadata
,
}
id
:=
uuid
.
NewV4
().
String
()
t
.
id
=
id
select
{
case
m
.
pendingTasks
<-
t
:
default
:
return
""
,
errors
.
New
(
"Cannot add task: queue full."
)
}
m
.
existingTasksMutex
.
Lock
()
defer
m
.
existingTasksMutex
.
Unlock
()
m
.
existingTasks
[
id
]
=
t
return
id
,
nil
}
// Start initiates the manager to accept tasks and monitor the execution.
func
(
m
*
managerImpl
)
Start
() {
parentCtx
:=
context
.
Background
()
go
func
() {
for
{
m
.
parallel
<-
struct
{}{}
t
:=
<-
m
.
pendingTasks
ctx
,
cancel
:=
context
.
WithCancel
(
parentCtx
)
m
.
cancelFuncTasksMutex
.
DoWithLock
(
t
.
id
,
func
() {
if
t
.
cancel
!=
nil
{
cancel
()
}
t
.
cancel
=
cancel
})
m
.
startTaskExec
(
ctx
,
t
)
}
}()
go
func
() {
for
{
select
{
case
<-
parentCtx
.
Done
():
return
case
<-
time
.
After
(
m
.
cleanUpInterval
):
m
.
cleanupExpiredCompletedTasks
()
}
}
}()
}
func
(
m
*
managerImpl
)
startTaskExec
(
ctx
context.
Context
,
t
*
taskWrapper
) {
go
func
(
t
*
taskWrapper
) {
res
,
err
:=
t
.
execute
(
ctx
)
<-
m
.
parallel
m
.
completedTasksMutex
.
Lock
()
defer
m
.
completedTasksMutex
.
Unlock
()
m
.
completedTasks
[
t
.
id
]
=
time
.
Now
()
t
.
result
=
res
t
.
err
=
err
}(
t
)
}
func
(
m
*
managerImpl
)
cleanupExpiredCompletedTasks
() {
m
.
completedTasksMutex
.
Lock
()
defer
m
.
completedTasksMutex
.
Unlock
()
// check expired completed tasks.
currentTime
:=
time
.
Now
()
var
removeIds
[]
string
for
id
,
t
:=
range
m
.
completedTasks
{
if
t
.
Add
(
m
.
completedTaskExpiryTime
).
Before
(
currentTime
) {
removeIds
=
append
(
removeIds
,
id
)
}
}
m
.
existingTasksMutex
.
Lock
()
defer
m
.
existingTasksMutex
.
Unlock
()
for
_
,
id
:=
range
removeIds
{
delete
(
m
.
completedTasks
,
id
)
delete
(
m
.
existingTasks
,
id
)
}
}
// GetTaskStatus returns the status of the jobid.
func
(
m
*
managerImpl
)
GetTaskStatusAndMetadata
(
taskID
string
) (
map
[
string
]
interface
{},
interface
{},
bool
,
error
) {
m
.
completedTasksMutex
.
Lock
()
defer
m
.
completedTasksMutex
.
Unlock
()
m
.
existingTasksMutex
.
Lock
()
defer
m
.
existingTasksMutex
.
Unlock
()
t
,
ok
:=
m
.
existingTasks
[
taskID
]
if
!
ok
{
return
nil
,
nil
,
false
,
errors
.
New
(
"id does not exist."
)
}
_
,
completed
:=
m
.
completedTasks
[
taskID
]
return
t
.
metadata
,
t
.
result
,
completed
,
t
.
err
}
func
(
m
*
managerImpl
)
CancelTask
(
taskID
string
)
error
{
m
.
existingTasksMutex
.
Lock
()
defer
m
.
existingTasksMutex
.
Unlock
()
t
,
ok
:=
m
.
existingTasks
[
taskID
]
if
!
ok
{
return
errors
.
New
(
"id does not exist."
)
}
// We defer unlock of existingTasksMutex so that it cant be cleaned up, and we know that it exists throughout the
// completion of this fn.
m
.
cancelFuncTasksMutex
.
Lock
(
taskID
)
defer
m
.
cancelFuncTasksMutex
.
Unlock
(
taskID
)
if
t
.
cancel
==
nil
{
t
.
cancel
=
cancelExecutionMarker
return
nil
}
t
.
cancel
()
return
nil
}
Back
|
FazBrowse Home
|
New Git URL