-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobqueue.go
74 lines (58 loc) · 1.59 KB
/
jobqueue.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
// Copyright (c) 2024 g41797
// SPDX-License-Identifier: MIT
package jobnik
import (
"fmt"
"log"
"strings"
"sync"
"github.com/g41797/sputnik"
)
type Submitter interface {
// Submit job to queue
// Returns JobStatus for further job tracking
Submit(jo JobOrder) (JobStatus, error)
// Returns current job status
// Lack of the job returned as Unknown JobState
// error returned for failed access to job queue
Check(js JobStatus) (JobStatus, error)
}
type Receiver interface {
// Register receive callback
OnReceive(rcv func(j Job)) error
// Allows to receive next job from the queue
// Not accumulated - 100 Acks allow 1 receive :-(
// js may be nil (at lest it's nil for the first receive)
// Informs queue about changed job status (for not nil)
Ack(js JobStatus) error
}
type JobQueue interface {
sputnik.ServerConnector
Submitter
Receiver
Stop()
}
type JobQueueFactory func() (JobQueue, error)
// Store factory for further usage
// name of queue is stored in lower case
func RegisterJobQueueFactory(name string, fact JobQueueFactory) {
if len(name) == 0 {
log.Panic("empty JobQueue name")
}
if fact == nil {
log.Panicf("nil JobQueue factory for %s", name)
}
if _, exists := factories.LoadOrStore(strings.ToLower(name), fact); exists {
log.Panicf("JobQueue factory for %s already exists", name)
}
return
}
func CreateJobQueue(name string) (JobQueue, error) {
fact, exists := factories.Load(strings.ToLower(name))
if !exists {
return nil, fmt.Errorf("factory for %s does not exist", name)
}
tr, err := fact.(JobQueueFactory)()
return tr, err
}
var factories sync.Map