-
Notifications
You must be signed in to change notification settings - Fork 21
/
cqmgr.go
701 lines (630 loc) · 21.5 KB
/
cqmgr.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package core
import (
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
e "github.com/MG-RAST/AWE/lib/errors"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"github.com/MG-RAST/AWE/lib/user"
"github.com/MG-RAST/AWE/vendor/github.com/MG-RAST/golib/mgo/bson"
"os"
"strings"
"sync"
"time"
)
type CQMgr struct {
clientLock sync.RWMutex
clientMap map[string]*Client
workQueue *WQueue
coReq chan CoReq //workunit checkout request (WorkController -> qmgr.Handler)
coAck chan CoAck //workunit checkout item including data and err (qmgr.Handler -> WorkController)
feedback chan Notice //workunit execution feedback (WorkController -> qmgr.Handler)
coSem chan int //semaphore for checkout (mutual exclusion between different clients)
}
func NewCQMgr() *CQMgr {
return &CQMgr{
clientMap: map[string]*Client{},
workQueue: NewWQueue(),
coReq: make(chan CoReq),
coAck: make(chan CoAck),
feedback: make(chan Notice),
coSem: make(chan int, 1), //non-blocking buffered channel
}
}
//--------mgr methods-------
func (qm *CQMgr) ClientHandle() {
for {
select {
case coReq := <-qm.coReq:
logger.Debug(2, fmt.Sprintf("qmgr: workunit checkout request received, Req=%v\n", coReq))
works, err := qm.popWorks(coReq)
ack := CoAck{workunits: works, err: err}
qm.coAck <- ack
case notice := <-qm.feedback:
logger.Debug(2, fmt.Sprintf("qmgr: workunit feedback received, workid=%s, status=%s, clientid=%s\n", notice.WorkId, notice.Status, notice.ClientId))
if err := qm.handleWorkStatusChange(notice); err != nil {
logger.Error("handleWorkStatusChange(): " + err.Error())
}
}
}
}
// show functions used in debug
func (qm *CQMgr) ShowWorkQueue() {
fmt.Printf("current queuing workunits (%d):\n", qm.workQueue.Len())
for _, id := range qm.workQueue.List() {
fmt.Printf("workunit id: %s\n", id)
}
return
}
//--------accessor methods-------
func (qm *CQMgr) copyClient(a *Client) (b *Client) {
b = new(Client)
*b = *a
return
}
func (qm *CQMgr) PutClient(client *Client) {
qm.clientLock.Lock()
qm.clientMap[client.Id] = client
qm.clientLock.Unlock()
}
func (qm *CQMgr) GetClient(id string) (*Client, bool) {
qm.clientLock.RLock()
defer qm.clientLock.RUnlock()
if client, ok := qm.clientMap[id]; ok {
copy := qm.copyClient(client)
return copy, true
}
return nil, false
}
func (qm *CQMgr) GetAllClients() (clients []*Client) {
qm.clientLock.RLock()
defer qm.clientLock.RUnlock()
for _, client := range qm.clientMap {
copy := qm.copyClient(client)
clients = append(clients, copy)
}
return
}
func (qm *CQMgr) RemoveClient(id string) {
qm.clientLock.Lock()
delete(qm.clientMap, id)
qm.clientLock.Unlock()
}
func (qm *CQMgr) DeleteClient(id string) (err error) {
err = qm.ClientStatusChange(id, CLIENT_STAT_DELETED)
return
}
func (qm *CQMgr) ClientStatusChange(id string, new_status string) (err error) {
qm.clientLock.Lock()
defer qm.clientLock.Unlock()
if client, ok := qm.clientMap[id]; ok {
client.Status = new_status
return
}
return errors.New(e.ClientNotFound)
}
func (qm *CQMgr) HasClient(id string) (has bool) {
qm.clientLock.RLock()
defer qm.clientLock.RUnlock()
if _, ok := qm.clientMap[id]; ok {
has = true
} else {
has = false
}
return
}
func (qm *CQMgr) ListClients() (ids []string) {
qm.clientLock.RLock()
defer qm.clientLock.RUnlock()
for id, _ := range qm.clientMap {
ids = append(ids, id)
}
return
}
//--------client methods-------
func (qm *CQMgr) ClientChecker() {
for {
time.Sleep(30 * time.Second)
logger.Debug(3, "time to update client list....\n")
for _, client := range qm.GetAllClients() {
if client.Tag == true {
client.Tag = false
total_minutes := int(time.Now().Sub(client.RegTime).Minutes())
hours := total_minutes / 60
minutes := total_minutes % 60
client.Serve_time = fmt.Sprintf("%dh%dm", hours, minutes)
if len(client.Current_work) > 0 {
client.Idle_time = 0
} else {
client.Idle_time += 30
}
qm.PutClient(client)
} else {
if ok := qm.HasClient(client.Id); !ok {
continue
}
//now client must be gone as tag set to false 30 seconds ago and no heartbeat received thereafter
logger.Event(event.CLIENT_UNREGISTER, "clientid="+client.Id+";name="+client.Name)
//requeue unfinished workunits associated with the failed client
qm.ReQueueWorkunitByClient(client.Id)
//delete the client from client map
qm.RemoveClient(client.Id)
}
}
}
}
func (qm *CQMgr) ClientHeartBeat(id string, cg *ClientGroup) (hbmsg HBmsg, err error) {
hbmsg = make(map[string]string, 1)
if client, ok := qm.GetClient(id); ok {
// If the name of the clientgroup (from auth token) does not match the name in the client retrieved, throw an error
if cg != nil && client.Group != cg.Name {
return nil, errors.New(e.ClientGroupBadName)
}
client.Tag = true
qm.PutClient(client)
logger.Debug(3, "HeartBeatFrom:"+"clientid="+id+",name="+client.Name)
//get suspended workunit that need the client to discard
workids := qm.getWorkByClient(id)
suspended := []string{}
for _, work := range qm.workQueue.GetSet(workids) {
if work.State == WORK_STAT_SUSPEND {
suspended = append(suspended, work.Id)
}
}
if len(suspended) > 0 {
hbmsg["discard"] = strings.Join(suspended, ",")
}
if client.Status == CLIENT_STAT_DELETED {
hbmsg["stop"] = id
}
//hbmsg["discard"] = strings.Join(workids, ",")
return hbmsg, nil
}
return hbmsg, errors.New(e.ClientNotFound)
}
func (qm *CQMgr) RegisterNewClient(files FormFiles, cg *ClientGroup) (client *Client, err error) {
if _, ok := files["profile"]; ok {
client, err = NewProfileClient(files["profile"].Path)
os.Remove(files["profile"].Path)
} else {
client = NewClient()
}
if err != nil {
return nil, err
}
// If clientgroup is nil at this point, create a publicly owned clientgroup, with the provided group name (if one doesn't exist with the same name)
if cg == nil {
// See if clientgroup already exists with this name
// If it does and it does not have "public" execution rights, throw error
// If it doesn't, create one owned by public, and continue with client registration
// Otherwise proceed with client registration.
cg, _ = LoadClientGroupByName(client.Group)
if cg != nil {
rights := cg.Acl.Check("public")
if rights["execute"] == false {
return nil, errors.New("Clientgroup with the group specified by your client exists, but you cannot register with it, without clientgroup token.")
}
} else {
u := &user.User{Uuid: "public"}
cg, err = CreateClientGroup(client.Group, u)
if err != nil {
return nil, err
}
}
}
// If the name of the clientgroup (from auth token) does not match the name in the client profile, throw an error
if cg != nil && client.Group != cg.Name {
return nil, errors.New(e.ClientGroupBadName)
}
qm.PutClient(client)
if len(client.Current_work) > 0 { //re-registered client
// move already checked-out workunit from waiting queue (workMap) to checked-out list (coWorkMap)
for workid, _ := range client.Current_work {
if qm.workQueue.Has(workid) {
qm.workQueue.StatusChange(workid, WORK_STAT_CHECKOUT)
}
}
}
return
}
func (qm *CQMgr) GetClientByUser(id string, u *user.User) (client *Client, err error) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
if client, ok := qm.GetClient(id); ok {
if val, exists := filtered_clientgroups[client.Group]; exists != true || val != true {
return client, nil
}
}
return nil, errors.New(e.ClientNotFound)
}
func (qm *CQMgr) GetAllClientsByUser(u *user.User) (clients []*Client) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
rights := cg.Acl.Check(u.Uuid)
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || rights["read"] == true || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
for _, client := range qm.GetAllClients() {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true {
clients = append(clients, client)
}
}
return clients
}
func (qm *CQMgr) DeleteClientByUser(id string, u *user.User) (err error) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
if client, ok := qm.GetClient(id); ok {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true {
err = qm.DeleteClient(id)
return
}
return errors.New(e.UnAuth)
}
return errors.New(e.ClientNotFound)
}
func (qm *CQMgr) SuspendClient(id string) (err error) {
if client, ok := qm.GetClient(id); ok {
if client.Status == CLIENT_STAT_ACTIVE_IDLE || client.Status == CLIENT_STAT_ACTIVE_BUSY {
if err = qm.ClientStatusChange(id, CLIENT_STAT_SUSPEND); err != nil {
return
}
qm.ReQueueWorkunitByClient(id)
return
}
return errors.New(e.ClientNotActive)
}
return errors.New(e.ClientNotFound)
}
func (qm *CQMgr) SuspendAllClients() (count int) {
for _, id := range qm.ListClients() {
if err := qm.SuspendClient(id); err == nil {
count += 1
}
}
return count
}
func (qm *CQMgr) SuspendClientByUser(id string, u *user.User) (err error) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
if client, ok := qm.GetClient(id); ok {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true {
if client.Status == CLIENT_STAT_ACTIVE_IDLE || client.Status == CLIENT_STAT_ACTIVE_BUSY {
if err = qm.ClientStatusChange(id, CLIENT_STAT_SUSPEND); err != nil {
return
}
qm.ReQueueWorkunitByClient(id)
return
}
return errors.New(e.ClientNotActive)
}
return errors.New(e.UnAuth)
}
return errors.New(e.ClientNotFound)
}
func (qm *CQMgr) SuspendAllClientsByUser(u *user.User) (count int) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
for _, client := range qm.GetAllClients() {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true && (client.Status == CLIENT_STAT_ACTIVE_IDLE || client.Status == CLIENT_STAT_ACTIVE_BUSY) {
qm.SuspendClient(client.Id)
count += 1
}
}
return count
}
func (qm *CQMgr) ResumeClient(id string) (err error) {
if client, ok := qm.GetClient(id); ok {
if client.Status == CLIENT_STAT_SUSPEND {
err = qm.ClientStatusChange(id, CLIENT_STAT_ACTIVE_IDLE)
return
}
return errors.New(e.ClientNotSuspended)
}
return errors.New(e.ClientNotFound)
}
func (qm *CQMgr) ResumeClientByUser(id string, u *user.User) (err error) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
if client, ok := qm.GetClient(id); ok {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true {
if client.Status == CLIENT_STAT_SUSPEND {
err = qm.ClientStatusChange(id, CLIENT_STAT_ACTIVE_IDLE)
return
}
return errors.New(e.ClientNotSuspended)
}
return errors.New(e.UnAuth)
}
return errors.New(e.ClientNotFound)
}
func (qm *CQMgr) ResumeSuspendedClients() (count int) {
for _, client := range qm.GetAllClients() {
if client.Status == CLIENT_STAT_SUSPEND {
qm.ClientStatusChange(client.Id, CLIENT_STAT_ACTIVE_IDLE)
count += 1
}
}
return count
}
func (qm *CQMgr) ResumeSuspendedClientsByUser(u *user.User) (count int) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
for _, client := range qm.GetAllClients() {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true && client.Status == CLIENT_STAT_SUSPEND {
qm.ClientStatusChange(client.Id, CLIENT_STAT_ACTIVE_IDLE)
count += 1
}
}
return count
}
func (qm *CQMgr) UpdateSubClients(id string, count int) {
if client, ok := qm.GetClient(id); ok {
client.SubClients = count
qm.PutClient(client)
}
}
func (qm *CQMgr) UpdateSubClientsByUser(id string, count int, u *user.User) {
// Get all clientgroups that user owns or that are publicly owned, or all if user is admin
q := bson.M{}
clientgroups := new(ClientGroups)
dbFindClientGroups(q, clientgroups)
filtered_clientgroups := map[string]bool{}
for _, cg := range *clientgroups {
if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || u.Admin == true || cg.Acl.Owner == "public")) ||
(u.Uuid == "public" && conf.CLIENT_AUTH_REQ == false && cg.Acl.Owner == "public") {
filtered_clientgroups[cg.Name] = true
}
}
if client, ok := qm.GetClient(id); ok {
if val, exists := filtered_clientgroups[client.Group]; exists == true && val == true {
client.SubClients = count
qm.PutClient(client)
}
}
}
//--------end client methods-------
//-------start of workunit methods---
func (qm *CQMgr) CheckoutWorkunits(req_policy string, client_id string, num int) (workunits []*Workunit, err error) {
//precheck if the client is registered
client, hasClient := qm.GetClient(client_id)
if !hasClient {
return nil, errors.New(e.ClientNotFound)
}
if client.Status == CLIENT_STAT_SUSPEND {
return nil, errors.New(e.ClientSuspended)
}
if client.Status == CLIENT_STAT_DELETED {
qm.RemoveClient(client_id)
return nil, errors.New(e.ClientDeleted)
}
logger.Debug(3, fmt.Sprintf("lock semaphore in CheckoutWorkunits() for client: %s", client_id))
//lock semaphore, at one time only one client's checkout request can be served
qm.LockSemaphore()
defer qm.UnlockSemaphore()
req := CoReq{policy: req_policy, fromclient: client_id, count: num}
qm.coReq <- req
ack := <-qm.coAck
client, hasClient = qm.GetClient(client_id)
if !hasClient {
return nil, errors.New(e.ClientNotFound)
}
if ack.err == nil {
for _, work := range ack.workunits {
client.Total_checkout += 1
client.Current_work[work.Id] = true
}
if client.Status == CLIENT_STAT_ACTIVE_IDLE {
client.Status = CLIENT_STAT_ACTIVE_BUSY
}
}
qm.PutClient(client)
logger.Debug(3, fmt.Sprintf("unlock semaphore in CheckoutWorkunits() for client: %s", client_id))
return ack.workunits, ack.err
}
func (qm *CQMgr) LockSemaphore() {
qm.coSem <- 1
}
func (qm *CQMgr) UnlockSemaphore() {
<-qm.coSem
}
func (qm *CQMgr) GetWorkById(id string) (workunit *Workunit, err error) {
workunit, ok := qm.workQueue.Get(id)
if !ok {
err = errors.New(fmt.Sprintf("no workunit found with id %s", id))
}
return
}
func (qm *CQMgr) NotifyWorkStatus(notice Notice) {
qm.feedback <- notice
return
}
func (qm *CQMgr) popWorks(req CoReq) (works []*Workunit, err error) {
logger.Debug(3, fmt.Sprintf("starting popWorks() for client: %s\n", req.fromclient))
filtered, err := qm.filterWorkByClient(req.fromclient)
if err != nil {
return
}
logger.Debug(2, fmt.Sprintf("popWorks filtered: %d (0 meansNoEligibleWorkunitFound)", filtered))
if len(filtered) == 0 {
return nil, errors.New(e.NoEligibleWorkunitFound)
}
works, err = qm.workQueue.selectWorkunits(filtered, req.policy, req.count)
if err == nil { //get workunits successfully, put them into coWorkMap
for _, work := range works {
work.Client = req.fromclient
work.CheckoutTime = time.Now()
qm.workQueue.Put(work)
qm.workQueue.StatusChange(work.Id, WORK_STAT_CHECKOUT)
}
}
logger.Debug(3, fmt.Sprintf("done with popWorks() for client: %s\n", req.fromclient))
return
}
func (qm *CQMgr) filterWorkByClient(clientid string) (ids []string, err error) {
logger.Debug(3, fmt.Sprintf("starting filterWorkByClient() for client: %s\n", clientid))
client, ok := qm.GetClient(clientid)
if !ok {
err_msg := fmt.Sprintf("error: unregistered client %s trying to checkout workunit, most likely cause is client disappeared after request to checkout workunit combined with slow response to workunit checkout request", clientid)
fmt.Fprintln(os.Stderr, err_msg)
logger.Error(err_msg)
return nil, errors.New(e.ClientNotFound)
}
for _, id := range qm.workQueue.WaitList() {
work, ok := qm.workQueue.Get(id)
if !ok {
logger.Error(fmt.Sprintf("error: workunit %s is in wait queue but not in workMap", id))
continue
}
//skip works that are in the client's skip-list
if contains(client.Skip_work, work.Id) {
logger.Debug(2, fmt.Sprintf("2) contains(client.Skip_work, work.Id) %s", id))
continue
}
//skip works that have dedicate client groups which this client doesn't belong to
if len(work.Info.ClientGroups) > 0 {
eligible_groups := strings.Split(work.Info.ClientGroups, ",")
if !contains(eligible_groups, client.Group) {
logger.Debug(2, fmt.Sprintf("3) !contains(eligible_groups, client.Group) %s", id))
continue
}
}
//append works whos apps are supported by the client
if contains(client.Apps, work.Cmd.Name) || contains(client.Apps, conf.ALL_APP) {
ids = append(ids, id)
} else {
logger.Debug(2, fmt.Sprintf("3) contains(client.Apps, work.Cmd.Name) || contains(client.Apps, conf.ALL_APP) %s", id))
}
}
logger.Debug(3, fmt.Sprintf("done with filterWorkByClient() for client: %s\n", clientid))
return ids, nil
}
func (qm *CQMgr) getWorkByClient(clientid string) (ids []string) {
if client, ok := qm.GetClient(clientid); ok {
for id, _ := range client.Current_work {
ids = append(ids, id)
}
}
return
}
//handle feedback from a client about the execution of a workunit
func (qm *CQMgr) handleWorkStatusChange(notice Notice) (err error) {
//to be implemented for proxy or server
return
}
func (qm *CQMgr) FetchDataToken(workid string, clientid string) (token string, err error) {
//to be implemented for proxy or server
return
}
func (qm *CQMgr) ShowWorkunits(status string) (workunits []*Workunit) {
for _, work := range qm.workQueue.GetAll() {
if work.State == status || status == "" {
workunits = append(workunits, work)
}
}
return workunits
}
func (qm *CQMgr) ShowWorkunitsByUser(status string, u *user.User) (workunits []*Workunit) {
// Only returns workunits of jobs that the user has read access to or is the owner of. If user is admin, return all.
for _, work := range qm.workQueue.GetAll() {
// skip loading jobs from db if user is admin
if u.Admin == true {
if work.State == status || status == "" {
workunits = append(workunits, work)
}
} else {
if jobid, err := GetJobIdByWorkId(work.Id); err == nil {
if job, err := LoadJob(jobid); err == nil {
rights := job.Acl.Check(u.Uuid)
if job.Acl.Owner == u.Uuid || rights["read"] == true {
if work.State == status || status == "" {
workunits = append(workunits, work)
}
}
}
}
}
}
return workunits
}
func (qm *CQMgr) EnqueueWorkunit(work *Workunit) (err error) {
err = qm.workQueue.Add(work)
return
}
func (qm *CQMgr) ReQueueWorkunitByClient(clientid string) (err error) {
workids := qm.getWorkByClient(clientid)
for _, workid := range workids {
if qm.workQueue.Has(workid) {
jobid, _ := GetJobIdByWorkId(workid)
if job, err := LoadJob(jobid); err == nil {
if contains(JOB_STATS_ACTIVE, job.State) { //only requeue workunits belonging to active jobs (rule out suspended jobs)
qm.workQueue.StatusChange(workid, WORK_STAT_QUEUED)
logger.Event(event.WORK_REQUEUE, "workid="+workid)
}
}
}
}
return
}
//---end of workunit methods