github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/queue/setting.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package queue
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/gitbundle/modules/json"
    13  	"github.com/gitbundle/modules/log"
    14  	"github.com/gitbundle/modules/setting"
    15  )
    16  
    17  func validType(t string) (Type, error) {
    18  	if len(t) == 0 {
    19  		return PersistableChannelQueueType, nil
    20  	}
    21  	for _, typ := range RegisteredTypes() {
    22  		if t == string(typ) {
    23  			return typ, nil
    24  		}
    25  	}
    26  	return PersistableChannelQueueType, fmt.Errorf("unknown queue type: %s defaulting to %s", t, string(PersistableChannelQueueType))
    27  }
    28  
    29  func getQueueSettings(name string) (setting.QueueSettings, []byte) {
    30  	q := setting.GetQueueSettings(name)
    31  	cfg, err := json.Marshal(q)
    32  	if err != nil {
    33  		log.Error("Unable to marshall generic options: %v Error: %v", q, err)
    34  		log.Error("Unable to create queue for %s", name, err)
    35  		return q, []byte{}
    36  	}
    37  	return q, cfg
    38  }
    39  
    40  // CreateQueue for name with provided handler and exemplar
    41  func CreateQueue(name string, handle HandlerFunc, exemplar interface{}) Queue {
    42  	q, cfg := getQueueSettings(name)
    43  	if len(cfg) == 0 {
    44  		return nil
    45  	}
    46  
    47  	typ, err := validType(q.Type)
    48  	if err != nil {
    49  		log.Error("Invalid type %s provided for queue named %s defaulting to %s", q.Type, name, string(typ))
    50  	}
    51  
    52  	returnable, err := NewQueue(typ, handle, cfg, exemplar)
    53  	if q.WrapIfNecessary && err != nil {
    54  		log.Warn("Unable to create queue for %s: %v", name, err)
    55  		log.Warn("Attempting to create wrapped queue")
    56  		returnable, err = NewQueue(WrappedQueueType, handle, WrappedQueueConfiguration{
    57  			Underlying:  typ,
    58  			Timeout:     q.Timeout,
    59  			MaxAttempts: q.MaxAttempts,
    60  			Config:      cfg,
    61  			QueueLength: q.QueueLength,
    62  			Name:        name,
    63  		}, exemplar)
    64  	}
    65  	if err != nil {
    66  		log.Error("Unable to create queue for %s: %v", name, err)
    67  		return nil
    68  	}
    69  
    70  	// Sanity check configuration
    71  	if q.Workers == 0 && (q.BoostTimeout == 0 || q.BoostWorkers == 0 || q.MaxWorkers == 0) {
    72  		log.Warn("Queue: %s is configured to be non-scaling and have no workers\n - this configuration is likely incorrect and could cause GitBundle to block", q.Name)
    73  		if pausable, ok := returnable.(Pausable); ok {
    74  			log.Warn("Queue: %s is being paused to prevent data-loss, add workers manually and unpause.", q.Name)
    75  			pausable.Pause()
    76  		}
    77  	}
    78  
    79  	return returnable
    80  }
    81  
    82  // CreateUniqueQueue for name with provided handler and exemplar
    83  func CreateUniqueQueue(name string, handle HandlerFunc, exemplar interface{}) UniqueQueue {
    84  	q, cfg := getQueueSettings(name)
    85  	if len(cfg) == 0 {
    86  		return nil
    87  	}
    88  
    89  	if len(q.Type) > 0 && q.Type != "dummy" && q.Type != "immediate" && !strings.HasPrefix(q.Type, "unique-") {
    90  		q.Type = "unique-" + q.Type
    91  	}
    92  
    93  	typ, err := validType(q.Type)
    94  	if err != nil || typ == PersistableChannelQueueType {
    95  		typ = PersistableChannelUniqueQueueType
    96  		if err != nil {
    97  			log.Error("Invalid type %s provided for queue named %s defaulting to %s", q.Type, name, string(typ))
    98  		}
    99  	}
   100  
   101  	returnable, err := NewQueue(typ, handle, cfg, exemplar)
   102  	if q.WrapIfNecessary && err != nil {
   103  		log.Warn("Unable to create unique queue for %s: %v", name, err)
   104  		log.Warn("Attempting to create wrapped queue")
   105  		returnable, err = NewQueue(WrappedUniqueQueueType, handle, WrappedUniqueQueueConfiguration{
   106  			Underlying:  typ,
   107  			Timeout:     q.Timeout,
   108  			MaxAttempts: q.MaxAttempts,
   109  			Config:      cfg,
   110  			QueueLength: q.QueueLength,
   111  		}, exemplar)
   112  	}
   113  	if err != nil {
   114  		log.Error("Unable to create unique queue for %s: %v", name, err)
   115  		return nil
   116  	}
   117  
   118  	// Sanity check configuration
   119  	if q.Workers == 0 && (q.BoostTimeout == 0 || q.BoostWorkers == 0 || q.MaxWorkers == 0) {
   120  		log.Warn("Queue: %s is configured to be non-scaling and have no workers\n - this configuration is likely incorrect and could cause GitBundle to block", q.Name)
   121  		if pausable, ok := returnable.(Pausable); ok {
   122  			log.Warn("Queue: %s is being paused to prevent data-loss, add workers manually and unpause.", q.Name)
   123  			pausable.Pause()
   124  		}
   125  	}
   126  
   127  	return returnable.(UniqueQueue)
   128  }