forked from forgejo/forgejo
Remove dependency on queue from setting
This commit is contained in:
parent
8798a61ba4
commit
030b6d91c8
7 changed files with 91 additions and 66 deletions
|
@ -123,8 +123,8 @@ func RegisteredTypesAsString() []string {
|
|||
return types
|
||||
}
|
||||
|
||||
// CreateQueue takes a queue Type and HandlerFunc some options and possibly an exemplar and returns a Queue or an error
|
||||
func CreateQueue(queueType Type, handlerFunc HandlerFunc, opts, exemplar interface{}) (Queue, error) {
|
||||
// NewQueue takes a queue Type and HandlerFunc some options and possibly an exemplar and returns a Queue or an error
|
||||
func NewQueue(queueType Type, handlerFunc HandlerFunc, opts, exemplar interface{}) (Queue, error) {
|
||||
newFn, ok := queuesMap[queueType]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Unsupported queue type: %v", queueType)
|
||||
|
|
|
@ -59,7 +59,7 @@ func (q *delayedStarter) setInternal(atShutdown func(context.Context, func()), h
|
|||
q.lock.Unlock()
|
||||
log.Fatal("Timedout creating queue %v with cfg %v in %s", q.underlying, q.cfg, q.name)
|
||||
default:
|
||||
queue, err := CreateQueue(q.underlying, handle, q.cfg, exemplar)
|
||||
queue, err := NewQueue(q.underlying, handle, q.cfg, exemplar)
|
||||
if err == nil {
|
||||
q.internal = queue
|
||||
q.lock.Unlock()
|
||||
|
@ -101,7 +101,7 @@ func NewWrappedQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, erro
|
|||
}
|
||||
config := configInterface.(WrappedQueueConfiguration)
|
||||
|
||||
queue, err := CreateQueue(config.Underlying, handle, config.Config, exemplar)
|
||||
queue, err := NewQueue(config.Underlying, handle, config.Config, exemplar)
|
||||
if err == nil {
|
||||
// Just return the queue there is no need to wrap
|
||||
return queue, nil
|
||||
|
|
75
modules/queue/setting.go
Normal file
75
modules/queue/setting.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package queue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
func validType(t string) (Type, error) {
|
||||
if len(t) == 0 {
|
||||
return PersistableChannelQueueType, nil
|
||||
}
|
||||
for _, typ := range RegisteredTypes() {
|
||||
if t == string(typ) {
|
||||
return typ, nil
|
||||
}
|
||||
}
|
||||
return PersistableChannelQueueType, fmt.Errorf("Unknown queue type: %s defaulting to %s", t, string(PersistableChannelQueueType))
|
||||
}
|
||||
|
||||
// CreateQueue for name with provided handler and exemplar
|
||||
func CreateQueue(name string, handle HandlerFunc, exemplar interface{}) Queue {
|
||||
q := setting.GetQueueSettings(name)
|
||||
opts := make(map[string]interface{})
|
||||
opts["Name"] = name
|
||||
opts["QueueLength"] = q.Length
|
||||
opts["BatchLength"] = q.BatchLength
|
||||
opts["DataDir"] = q.DataDir
|
||||
opts["Addresses"] = q.Addresses
|
||||
opts["Network"] = q.Network
|
||||
opts["Password"] = q.Password
|
||||
opts["DBIndex"] = q.DBIndex
|
||||
opts["QueueName"] = q.QueueName
|
||||
opts["Workers"] = q.Workers
|
||||
opts["MaxWorkers"] = q.MaxWorkers
|
||||
opts["BlockTimeout"] = q.BlockTimeout
|
||||
opts["BoostTimeout"] = q.BoostTimeout
|
||||
opts["BoostWorkers"] = q.BoostWorkers
|
||||
|
||||
typ, err := validType(q.Type)
|
||||
if err != nil {
|
||||
log.Error("Invalid type %s provided for queue named %s defaulting to %s", q.Type, name, string(typ))
|
||||
}
|
||||
|
||||
cfg, err := json.Marshal(opts)
|
||||
if err != nil {
|
||||
log.Error("Unable to marshall generic options: %v Error: %v", opts, err)
|
||||
log.Error("Unable to create queue for %s", name, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
returnable, err := NewQueue(typ, handle, cfg, exemplar)
|
||||
if q.WrapIfNecessary && err != nil {
|
||||
log.Warn("Unable to create queue for %s: %v", name, err)
|
||||
log.Warn("Attempting to create wrapped queue")
|
||||
returnable, err = NewQueue(WrappedQueueType, handle, WrappedQueueConfiguration{
|
||||
Underlying: Type(q.Type),
|
||||
Timeout: q.Timeout,
|
||||
MaxAttempts: q.MaxAttempts,
|
||||
Config: cfg,
|
||||
QueueLength: q.Length,
|
||||
}, exemplar)
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("Unable to create queue for %s: %v", name, err)
|
||||
return nil
|
||||
}
|
||||
return returnable
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue