github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/job/workers_list.go (about)

     1  package job
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cozy/cozy-stack/pkg/config/config"
     7  	"github.com/cozy/cozy-stack/pkg/logger"
     8  )
     9  
    10  // WorkersList is a map associating a worker type with its acutal
    11  // configuration.
    12  type WorkersList []*WorkerConfig
    13  
    14  // workersList is the list of available workers with their associated to
    15  // function.
    16  var workersList WorkersList
    17  
    18  // GetWorkersList returns a list of all activated workers, configured
    19  // as defined by the configuration file.
    20  func GetWorkersList() ([]*WorkerConfig, error) {
    21  	jobsConf := config.GetConfig().Jobs
    22  	workersConfs := jobsConf.Workers
    23  	workers := make(WorkersList, 0, len(workersList))
    24  
    25  	for _, w := range workersList {
    26  		if config.GetConfig().Jobs.NoWorkers {
    27  			w = w.Clone()
    28  			w.Concurrency = 0
    29  		} else {
    30  			found := false
    31  			for _, c := range workersConfs {
    32  				if c.WorkerType == w.WorkerType {
    33  					w = applyWorkerConfig(w, c)
    34  					if found {
    35  						logger.WithNamespace("workers_list").Warnf(
    36  							"Configuration for the worker %q that is defined more than once",
    37  							c.WorkerType)
    38  					}
    39  					found = true
    40  				}
    41  			}
    42  			if jobsConf.AllowList && !found {
    43  				zero := 0
    44  				w = applyWorkerConfig(w, config.Worker{Concurrency: &zero})
    45  			}
    46  		}
    47  		workers = append(workers, w)
    48  	}
    49  
    50  	for _, c := range workersConfs {
    51  		_, found := findWorkerByType(c.WorkerType)
    52  		if !found {
    53  			logger.WithNamespace("workers_list").Warnf(
    54  				"Defined configuration for the worker %q that does not exist",
    55  				c.WorkerType)
    56  		}
    57  	}
    58  
    59  	return workers, nil
    60  }
    61  
    62  // GetWorkersNamesList returns the names of the configured workers
    63  func GetWorkersNamesList() []string {
    64  	workers, _ := GetWorkersList()
    65  	workerNames := make([]string, len(workers))
    66  
    67  	for i, w := range workers {
    68  		workerNames[i] = w.WorkerType
    69  	}
    70  	return workerNames
    71  }
    72  
    73  func applyWorkerConfig(w *WorkerConfig, c config.Worker) *WorkerConfig {
    74  	w = w.Clone()
    75  	if c.Concurrency != nil {
    76  		w.Concurrency = *c.Concurrency
    77  	}
    78  	if c.MaxExecCount != nil {
    79  		w.MaxExecCount = *c.MaxExecCount
    80  	}
    81  	if c.Timeout != nil {
    82  		w.Timeout = *c.Timeout
    83  	}
    84  	return w
    85  }
    86  
    87  func findWorkerByType(workerType string) (*WorkerConfig, bool) {
    88  	for _, w := range workersList {
    89  		if w.WorkerType == workerType {
    90  			return w, true
    91  		}
    92  	}
    93  	return nil, false
    94  }
    95  
    96  // AddWorker adds a new worker to global list of available workers.
    97  func AddWorker(conf *WorkerConfig) {
    98  	if conf.WorkerType == "" {
    99  		panic("Missing worker type field")
   100  	}
   101  	for _, w := range workersList {
   102  		if w.WorkerType == conf.WorkerType {
   103  			panic(fmt.Errorf("A worker with of type %q is already defined", conf.WorkerType))
   104  		}
   105  	}
   106  	workersList = append(workersList, conf)
   107  }