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
|
package background
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/mimecast/dtail/internal/io/logger"
)
type job struct {
cancel context.CancelFunc
wg *sync.WaitGroup
}
// Background specifies a job or command run in background on server side.
// This does not require an active DTail client SSH connection/session.
type Background struct {
mutex *sync.Mutex
jobs map[string]job
}
// New returns a new background manager.
func New() Background {
return Background{
jobs: make(map[string]job),
mutex: &sync.Mutex{},
}
}
// Add a background job.
func (b Background) Add(userName, jobName string, cancel context.CancelFunc, wg *sync.WaitGroup) error {
key := b.key(userName, jobName)
logger.Debug("background", "Add", key)
b.mutex.Lock()
defer b.mutex.Unlock()
if _, ok := b.jobs[key]; ok {
return errors.New("job already exists")
}
b.jobs[key] = job{cancel, wg}
// Clean up background job database.
go func() {
wg.Wait()
b.cancel(key)
}()
return nil
}
// Cancel a background job.
func (b Background) Cancel(userName, jobName string) error {
key := b.key(userName, jobName)
logger.Debug("background", "Cancel", key)
return b.cancel(key)
}
func (b Background) cancel(key string) error {
job, ok := b.get(key)
logger.Debug("background", "cancel", key, job, ok)
if !ok {
return errors.New("no job to cancel")
}
logger.Debug("background", "cancel", "run job.cancel()")
job.cancel()
logger.Debug("background", "cancel", "run job.wg.Wait()")
job.wg.Wait()
logger.Debug("background", "cancel", "run b.delete(key)")
b.delete(key)
return nil
}
// ListJobsC returns a channel listing all jobs of the given user.
func (b Background) ListJobsC(userName string) <-chan string {
logger.Debug("background", "ListJobC", userName)
ch := make(chan string)
go func() {
defer close(ch)
b.mutex.Lock()
defer b.mutex.Unlock()
for k, _ := range b.jobs {
logger.Debug("ListJobsC", k, userName)
if strings.HasPrefix(k, fmt.Sprintf("%s.", userName)) {
ch <- k
}
}
}()
return ch
}
func (b Background) get(key string) (job, bool) {
logger.Debug("background", "get", key)
b.mutex.Lock()
defer b.mutex.Unlock()
job, ok := b.jobs[key]
return job, ok
}
func (b Background) delete(key string) {
logger.Debug("background", "delete", key)
b.mutex.Lock()
defer b.mutex.Unlock()
delete(b.jobs, key)
}
func (Background) key(userName, jobName string) string {
return fmt.Sprintf("%s.%s", userName, jobName)
}
|