-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.go
48 lines (39 loc) · 1.2 KB
/
factory.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
package queue
import (
"fmt"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/exceptions"
"github.com/goal-web/supports/utils"
)
type Factory struct {
queues map[string]contracts.Queue
queueDrivers map[string]contracts.QueueDriver
config Config
serializer contracts.JobSerializer
}
func (factory *Factory) Connection(name ...string) contracts.Queue {
if len(name) > 0 {
return factory.Queue(name[0])
}
return factory.Queue(factory.config.Defaults.Connection)
}
func (factory *Factory) Extend(name string, driver contracts.QueueDriver) {
factory.queueDrivers[name] = driver
}
func (factory *Factory) Queue(name string) contracts.Queue {
if queue, exists := factory.queues[name]; exists {
return queue
}
config := factory.config.Connections[name]
driver := utils.GetStringField(config, "driver")
if config["default"] == nil {
config["default"] = factory.config.Defaults.Queue
}
if queueDriver, exists := factory.queueDrivers[driver]; exists {
factory.queues[name] = queueDriver(name, config, factory.serializer)
return factory.queues[name]
}
panic(DriverException{
Exception: exceptions.New(fmt.Sprintf("unsupported queue driver:%s", driver), config),
})
}