| 46 | } |
| 47 | |
| 48 | func (f *FunctionStoreImpl) Load() error { |
| 49 | f.mu.Lock() |
| 50 | defer f.mu.Unlock() |
| 51 | f.loadingFunctions = make(map[string]*model.Function) |
| 52 | info, err := os.Stat(f.path) |
| 53 | if err != nil { |
| 54 | if os.IsNotExist(err) { |
| 55 | slog.Info("the path to the function store does not exist. skip loading functions") |
| 56 | return nil |
| 57 | } |
| 58 | return errors.Wrapf(err, "the path to the function store %s is invalid", f.path) |
| 59 | } |
| 60 | if !info.IsDir() { |
| 61 | err = f.loadFile(f.path) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | } else { |
| 66 | err = filepath.Walk(f.path, func(path string, info os.FileInfo, err error) error { |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | if strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml") { |
| 72 | err := f.loadFile(path) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | } |
| 77 | return nil |
| 78 | }) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | for key, value := range f.loadingFunctions { |
| 85 | if _, exists := f.loadedFunctions[key]; !exists { |
| 86 | err := f.fm.StartFunction(value) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | for key, value := range f.loadedFunctions { |
| 94 | if _, exists := f.loadingFunctions[key]; !exists { |
| 95 | err := f.fm.DeleteFunction(value.Namespace, value.Name) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | f.loadedFunctions = f.loadingFunctions |
| 103 | slog.Info("functions loaded", "loadedFunctionsCount", len(f.loadedFunctions)) |
| 104 | return nil |
| 105 | } |