github.com/kaydxh/golang@v0.0.131/pkg/pool/taskqueue/config.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package taskqueue
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  
    28  	redis_ "github.com/kaydxh/golang/pkg/database/redis"
    29  	queue_ "github.com/kaydxh/golang/pkg/pool/taskqueue/queue"
    30  	redisq_ "github.com/kaydxh/golang/pkg/pool/taskqueue/queue/redis"
    31  	viper_ "github.com/kaydxh/golang/pkg/viper"
    32  	"github.com/sirupsen/logrus"
    33  	"github.com/spf13/viper"
    34  )
    35  
    36  type Config struct {
    37  	Proto TaskQueue
    38  	opts  struct {
    39  		// If set, overrides params below
    40  		viper *viper.Viper
    41  	}
    42  }
    43  
    44  type completedConfig struct {
    45  	*Config
    46  	completeError error
    47  }
    48  
    49  type CompletedConfig struct {
    50  	// Embed a private pointer that cannot be instantiated outside of this package.
    51  	*completedConfig
    52  }
    53  
    54  func (c *completedConfig) New(ctx context.Context, opts ...PoolOption) (*Pool, error) {
    55  
    56  	logrus.Infof("Installing TaskQueue")
    57  
    58  	if c.completeError != nil {
    59  		return nil, c.completeError
    60  	}
    61  
    62  	if !c.Proto.GetEnabled() {
    63  		logrus.Warnf("TaskQueue disenabled")
    64  		return nil, nil
    65  	}
    66  
    67  	rs, err := c.install(ctx, opts...)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	logrus.Infof("Installed TaskQueue")
    72  
    73  	return rs, nil
    74  }
    75  
    76  func (c *completedConfig) install(ctx context.Context, opts ...PoolOption) (*Pool, error) {
    77  
    78  	config := &c.Proto
    79  
    80  	var queue queue_.Queue
    81  	switch config.GetQueueType() {
    82  	case TaskQueue_queue_type_redis:
    83  		db := redis_.GetDB()
    84  		if db == nil {
    85  			return nil, fmt.Errorf("db is not first installed")
    86  		}
    87  		queue = redisq_.NewQueue(db, queue_.QueueOptions{Name: "redis"})
    88  
    89  	default:
    90  		return nil, fmt.Errorf("queue type %v is not support", config.GetQueueType())
    91  	}
    92  
    93  	pool := NewPool(queue,
    94  		WithFetcherBurst(config.GetFetcherBurst()),
    95  		WithWorkerBurst(config.GetWorkerBurst()),
    96  		WithWorkTimeout(config.GetWorkTimeout().AsDuration()),
    97  		WithFetchTimeout(config.GetFetchTimeout().AsDuration()),
    98  		WithResultExpired(config.GetResultExpired().AsDuration()),
    99  	)
   100  	pool.ApplyOptions(opts...)
   101  
   102  	err := pool.Consume(ctx)
   103  	if err != nil {
   104  		logrus.Errorf("failed to start consume task, err: %v", err)
   105  		return nil, err
   106  	}
   107  
   108  	return pool, nil
   109  }
   110  
   111  // Complete set default ServerRunOptions.
   112  func (c *Config) Complete() CompletedConfig {
   113  	err := c.loadViper()
   114  	if err != nil {
   115  		return CompletedConfig{&completedConfig{
   116  			Config:        c,
   117  			completeError: err,
   118  		}}
   119  	}
   120  
   121  	return CompletedConfig{&completedConfig{Config: c}}
   122  }
   123  
   124  func (c *Config) loadViper() error {
   125  	if c.opts.viper != nil {
   126  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto)
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  func NewConfig(options ...ConfigOption) *Config {
   133  	c := &Config{}
   134  	c.ApplyOptions(options...)
   135  
   136  	return c
   137  }