-
Notifications
You must be signed in to change notification settings - Fork 28
/
task.go
79 lines (66 loc) · 1.65 KB
/
task.go
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
// Copyright 2020-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"context"
"fmt"
"sync"
)
// taskCache is a runtime cache for tracking the
// asynchronous tasks which are in progress.
type taskCache struct {
m sync.RWMutex
cache map[string]context.CancelFunc // uuid to ctx
}
var liveTasks *taskCache
func init() {
liveTasks = &taskCache{
cache: make(map[string]context.CancelFunc),
}
}
func (at *taskCache) tasks() []string {
rv := make([]string, len(at.cache))
at.m.RLock()
for k := range at.cache {
rv = append(rv, k)
}
at.m.RUnlock()
return rv
}
func (at *taskCache) add(key string, cancel context.CancelFunc) {
at.m.Lock()
at.cache[key] = cancel
at.m.Unlock()
}
func (at *taskCache) taskCompleted(key string) {
at.m.Lock()
delete(at.cache, key)
at.m.Unlock()
}
func (at *taskCache) cancelTask(key string) error {
at.m.Lock()
defer at.m.Unlock()
if cancelFunc, ok := at.cache[key]; ok {
cancelFunc()
delete(at.cache, key)
return nil
}
return fmt.Errorf("task %s is no longer running", key)
}
func cancelTask(key string) error {
return liveTasks.cancelTask(key)
}
func updateTaskStart(key string, cancel context.CancelFunc) {
liveTasks.add(key, cancel)
}
func updateTaskFinish(key string) {
liveTasks.taskCompleted(key)
}
func backgroundTasks() []string {
return liveTasks.tasks()
}