Files
mostlymatter/server/channels/app/product.go
Miguel de la Cruz 067e36c23c Enable products for tests (#22757)
* Enable products for channels tests

* increase unit test timeout; check IsConfigReadOnly

* make app-layers

* Avoid loading boards tempaltes between tests to improve speed

* Fix delete query to be compatible with both databases

* Avoid preserving the templates for boards store tests

* Run all tests in one command

* Revert "Run all tests in one command"

This reverts commit 0330f7cd8f96b47776770e8f80ba6ba18d98b8d1.

* concurrent pkg group tests in CI

* Revert "Revert "Run all tests in one command""

This reverts commit 73892fec772575ff054a99ba9e00a7b57ca74164.

* Revert "concurrent pkg group tests in CI"

This reverts commit 550fb6cdd4a7f14d9632f2626bc66f389173b5d9.

* try testing 3 subsets of packages concurrently to improve time taken

* Revert "try testing 3 subsets of packages concurrently to improve time taken"

This reverts commit 97475f3c4eafa8bbe047097e0841220884f68cdc.

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: wiggin77 <wiggin77@warpmail.net>
2023-04-18 13:58:33 +02:00

113 строки
3.0 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"errors"
"fmt"
"os"
"strings"
"github.com/mattermost/mattermost-server/server/v8/channels/product"
"github.com/mattermost/mattermost-server/server/v8/platform/shared/mlog"
)
func (s *Server) initializeProducts(
productMap map[string]product.Manifest,
serviceMap map[product.ServiceKey]any,
) error {
// create a product map to consume
pmap := make(map[string]struct{})
for name := range productMap {
if !s.shouldStart(name) {
continue
}
pmap[name] = struct{}{}
}
// We figure out the initialization order by trial and error fashion hence maxTry
// is the maximum possible trials of initialization attempts. The order is not
// determined elsewhere therefore we do a on the fly sorting here. Which means the
// initialization order will be resolved during the loop.
maxTry := len(pmap) * len(pmap)
for len(pmap) > 0 && maxTry != 0 {
initLoop:
for product := range pmap {
manifest := productMap[product]
// we have dependencies defined. Here we check if the serviceMap
// has all the dependencies registered. If not, we continue to the
// loop to let other products initialize and register their services
// if they have any.
for key := range manifest.Dependencies {
if _, ok := serviceMap[key]; !ok {
maxTry--
continue initLoop
}
}
// some products can register themselves/their services
initializer := manifest.Initializer
prod, err := initializer(serviceMap)
if err != nil {
return fmt.Errorf("error initializing product %q: %w", product, err)
}
s.products[product] = prod
// we remove this product from the map to not try to initialize it again
delete(pmap, product)
}
}
if maxTry == 0 && len(pmap) != 0 {
var products string
for p := range pmap {
products = strings.Join([]string{products, fmt.Sprintf("%q", p)}, " ")
}
return fmt.Errorf("could not initialize product(s) due to circular dependency: %s", products)
}
return nil
}
func (s *Server) shouldStart(product string) bool {
if s.skipProductsInit && product != "channels" {
s.Log().Warn("Skipping product start: disabled via server options", mlog.String("product", product))
return false
}
if product == "boards" {
if os.Getenv("MM_DISABLE_BOARDS") == "true" {
s.Log().Warn("Skipping Boards start: disabled via env var")
return false
}
}
if product == "playbooks" {
if os.Getenv("MM_DISABLE_PLAYBOOKS") == "true" {
s.Log().Warn("Skipping Playbooks start: disabled via env var")
return false
}
}
return true
}
func (s *Server) HasBoardProduct() (bool, error) {
prod, exists := s.services[product.BoardsKey]
if !exists {
return false, nil
}
if prod == nil {
return false, errors.New("board product is nil")
}
if _, ok := prod.(product.BoardsService); !ok {
return false, errors.New("board product key does not match its definition")
}
return true, nil
}
func (a *App) HasBoardProduct() (bool, error) {
return a.Srv().HasBoardProduct()
}