-
Notifications
You must be signed in to change notification settings - Fork 167
/
subscriber.go
299 lines (259 loc) · 8.76 KB
/
subscriber.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package tasks
import (
"context"
"sync"
"time"
"github.com/cskr/pubsub"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/jobs"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/service/context"
"github.com/pydio/cells/common/service/defaults"
"github.com/pydio/cells/common/utils"
)
const (
PubSubTopicTaskStatuses = "tasks"
PubSubTopicControl = "control"
)
var (
PubSub *pubsub.PubSub
)
// Subscriber handles incoming events, applies selectors if any
// and generates all ActionMessage to trigger actions
type Subscriber struct {
Client client.Client
MainQueue chan Runnable
UpdateTasksChan chan *jobs.Task
JobsDefinitions map[string]*jobs.Job
Dispatchers map[string]*Dispatcher
jobsLock *sync.RWMutex
RootContext context.Context
}
// NewSubscriber creates a multiplexer for tasks managements and messages
// by maintaining a map of dispacher, one for each job definition.
func NewSubscriber(parentContext context.Context, client client.Client, server server.Server) *Subscriber {
s := &Subscriber{
Client: client,
JobsDefinitions: make(map[string]*jobs.Job),
MainQueue: make(chan Runnable),
UpdateTasksChan: make(chan *jobs.Task),
Dispatchers: make(map[string]*Dispatcher),
jobsLock: &sync.RWMutex{},
}
PubSub = pubsub.New(0)
s.RootContext = context.WithValue(parentContext, common.PYDIO_CONTEXT_USER_KEY, common.PYDIO_SYSTEM_USERNAME)
server.Subscribe(server.NewSubscriber(common.TOPIC_JOB_CONFIG_EVENT, s.jobsChangeEvent))
server.Subscribe(server.NewSubscriber(common.TOPIC_TREE_CHANGES, s.nodeEvent))
server.Subscribe(server.NewSubscriber(common.TOPIC_META_CHANGES, s.nodeEvent))
server.Subscribe(server.NewSubscriber(common.TOPIC_TIMER_EVENT, s.timerEvent))
s.ListenToMainQueue()
s.TaskChannelSubscription()
return s
}
// Init subscriber with current list of jobs from Jobs service
func (s *Subscriber) Init() error {
go func() {
<-time.After(1 * time.Second)
// Load Jobs Definitions
jobClients := jobs.NewJobServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_JOBS, s.Client)
streamer, e := jobClients.ListJobs(s.RootContext, &jobs.ListJobsRequest{})
if e != nil {
return
}
s.jobsLock.Lock()
defer s.jobsLock.Unlock()
for {
resp, er := streamer.Recv()
if er != nil {
break
}
if resp == nil {
continue
}
if resp.Job.Inactive {
continue
}
s.JobsDefinitions[resp.Job.ID] = resp.Job
s.GetDispatcherForJob(resp.Job)
}
}()
return nil
}
// ListenToMainQueue starts a go routine that listens to the Event Bus
func (s *Subscriber) ListenToMainQueue() {
go func() {
for {
select {
case runnable := <-s.MainQueue:
dispatcher := s.GetDispatcherForJob(runnable.Task.Job)
dispatcher.JobQueue <- runnable
}
}
}()
}
// TaskChannelSubscription uses PubSub library to receive update messages from tasks
func (s *Subscriber) TaskChannelSubscription() {
ch := PubSub.Sub(PubSubTopicTaskStatuses)
s.chanToStream(ch)
}
func (s *Subscriber) chanToStream(ch chan interface{}, requeue ...*jobs.Task) {
go func() {
log.Logger(s.RootContext).Debug("Connecting with TaskStreamer Client")
taskClient := jobs.NewJobServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_JOBS, defaults.NewClient())
ctx, cancel := context.WithTimeout(s.RootContext, 120*time.Second)
defer cancel()
streamer, e := taskClient.PutTaskStream(ctx)
if e != nil {
log.Logger(s.RootContext).Error("Streamer PutTaskStream", zap.Error(e))
<-time.After(10 * time.Second)
s.chanToStream(ch)
return
}
defer streamer.Close()
if len(requeue) > 0 {
streamer.Send(&jobs.PutTaskRequest{Task: requeue[0]})
streamer.Recv()
}
for {
select {
case val := <-ch:
if task, ok := val.(*jobs.Task); ok {
e := streamer.Send(&jobs.PutTaskRequest{Task: task})
if e != nil {
log.Logger(s.RootContext).Debug("Cannot post task - break and reconnect streamer", zap.Error(e))
<-time.After(1 * time.Second)
s.chanToStream(ch, task)
return
}
_, e = streamer.Recv()
if e != nil {
log.Logger(s.RootContext).Error("Error while posting task", zap.Error(e))
}
} else {
log.Logger(s.RootContext).Error("Could not cast value to jobs.Task", zap.Any("val", val))
}
}
}
}()
}
// GetDispatcherForJob creates a new dispatcher for a job
func (s *Subscriber) GetDispatcherForJob(job *jobs.Job) *Dispatcher {
if d, exists := s.Dispatchers[job.ID]; exists {
return d
}
maxWorkers := DefaultMaximumWorkers
if job.MaxConcurrency > 0 {
maxWorkers = int(job.MaxConcurrency)
}
dispatcher := NewDispatcher(maxWorkers)
s.Dispatchers[job.ID] = dispatcher
dispatcher.Run()
return dispatcher
}
// Job Configuration was updated, react accordingly
func (s *Subscriber) jobsChangeEvent(ctx context.Context, msg *jobs.JobChangeEvent) error {
s.jobsLock.Lock()
defer s.jobsLock.Unlock()
// Update config
if msg.JobRemoved != "" {
if _, ok := s.JobsDefinitions[msg.JobRemoved]; ok {
delete(s.JobsDefinitions, msg.JobRemoved)
}
// TODO: Shall we stop everything when changing config?
if dispatcher, ok := s.Dispatchers[msg.JobRemoved]; ok {
dispatcher.Stop()
delete(s.Dispatchers, msg.JobRemoved)
}
}
if msg.JobUpdated != nil {
s.JobsDefinitions[msg.JobUpdated.ID] = msg.JobUpdated
// TODO: Shall we stop everything when changing config? Or wait that it's idle for next time?
if dispatcher, ok := s.Dispatchers[msg.JobUpdated.ID]; ok {
dispatcher.Stop()
delete(s.Dispatchers, msg.JobUpdated.ID)
if !msg.JobUpdated.Inactive {
s.GetDispatcherForJob(msg.JobUpdated)
}
}
}
return nil
}
// Reacts to a trigger sent by the timer service
func (s *Subscriber) timerEvent(ctx context.Context, event *jobs.JobTriggerEvent) error {
jobId := event.JobID
// Load Job Data, build selectors
s.jobsLock.Lock()
defer s.jobsLock.Unlock()
j, ok := s.JobsDefinitions[jobId]
if !ok {
// Try to load definition directly for JobsService
jobClients := jobs.NewJobServiceClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_JOBS, s.Client)
resp, e := jobClients.GetJob(ctx, &jobs.GetJobRequest{JobID: jobId})
if e != nil || resp.Job == nil {
return nil
}
j = resp.Job
}
if j.Inactive {
return nil
}
// This timer event probably comes without user in context at that point
if u, _ := utils.FindUserNameInContext(ctx); u == "" {
ctx = metadata.NewContext(ctx, metadata.Metadata{common.PYDIO_CONTEXT_USER_KEY: common.PYDIO_SYSTEM_USERNAME})
ctx = context.WithValue(ctx, common.PYDIO_CONTEXT_USER_KEY, common.PYDIO_SYSTEM_USERNAME)
}
ctx = servicecontext.WithServiceName(ctx, servicecontext.GetServiceName(s.RootContext))
ctx = servicecontext.WithServiceColor(ctx, servicecontext.GetServiceColor(s.RootContext))
log.Logger(ctx).Info("Run Job " + jobId + " on timer event " + event.Schedule.String())
task := NewTaskFromEvent(ctx, j, event)
go task.EnqueueRunnables(s.Client, s.MainQueue)
return nil
}
// Reacts to a trigger linked to a nodeChange event.
func (s *Subscriber) nodeEvent(ctx context.Context, event *tree.NodeChangeEvent) error {
s.jobsLock.Lock()
defer s.jobsLock.Unlock()
ctx = servicecontext.WithServiceName(ctx, servicecontext.GetServiceName(s.RootContext))
ctx = servicecontext.WithServiceColor(ctx, servicecontext.GetServiceColor(s.RootContext))
for jobId, jobData := range s.JobsDefinitions {
if jobData.Inactive {
continue
}
for _, eName := range jobData.EventNames {
if eType, ok := jobs.ParseNodeChangeEventName(eName); ok {
if event.Type == eType {
log.Logger(ctx).Debug("Run Job " + jobId + " on event " + eName)
task := NewTaskFromEvent(ctx, jobData, event)
go task.EnqueueRunnables(s.Client, s.MainQueue)
}
} else {
log.Logger(ctx).Error("Scheduler cannot parse event name on node event: " + eName)
}
}
}
return nil
}