-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver_runtime.go
More file actions
71 lines (62 loc) · 1.79 KB
/
Copy pathdriver_runtime.go
File metadata and controls
71 lines (62 loc) · 1.79 KB
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
package queue
import (
"context"
"fmt"
"github.com/goforj/queue/busruntime"
)
type driverQueueBackend interface {
Driver() Driver
Dispatch(ctx context.Context, job Job) error
Shutdown(ctx context.Context) error
}
type driverRuntimeQueueBackend interface {
driverQueueBackend
Register(jobType string, handler Handler)
StartWorkers(ctx context.Context) error
DrainWorkers(ctx context.Context) error
}
type driverWorkerBackend interface {
Register(jobType string, handler Handler)
StartWorkers(ctx context.Context) error
Shutdown(ctx context.Context) error
}
type driverWorkerFactory func(workers int) (driverWorkerBackend, error)
func newQueueFromDriver(cfg Config, observer Observer, backend driverQueueBackend, workerFactory driverWorkerFactory) (queueRuntime, error) {
if backend == nil {
return nil, fmt.Errorf("driver backend is nil")
}
cfg = cfg.normalize()
observer = ensureObserverSink(observer)
var q queueBackend
var runtime runtimeQueueBackend
if native, ok := backend.(driverRuntimeQueueBackend); ok {
runtime = driverRuntimeQueueBackendAdapter{native}
q = runtime
} else {
q = driverQueueBackendAdapter{backend}
}
common := &queueCommon{
inner: newObservedQueue(q, cfg.Driver, observer),
cfg: cfg,
driver: cfg.Driver,
observerSink: observer,
}
if runtime != nil {
return &nativeQueueRuntime{
common: common,
runtime: runtime,
nativeQueueRuntimeState: &nativeQueueRuntimeState{
registered: make(map[string]Handler),
continuation: busruntime.NewContinuationScope(),
},
}, nil
}
return &externalQueueRuntime{
common: common,
newWorker: workerFactory,
externalQueueRuntimeState: &externalQueueRuntimeState{
registered: make(map[string]Handler),
continuation: busruntime.NewContinuationScope(),
},
}, nil
}