| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package functions |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/runtimecomp" |
| 10 | ) |
| 11 | |
| 12 | const functionsRuntimeComponentName = "functions.manager" |
| 13 | |
| 14 | func (m *Manager) SetRuntimeService(service runtimecomp.Service) { |
| 15 | if m == nil { |
| 16 | return |
| 17 | } |
| 18 | m.runtimeService = service |
| 19 | } |
| 20 | |
| 21 | func (m *Manager) registerRuntimeComponent() error { |
| 22 | if m == nil || m.runtimeService == nil || m.runtimeComponentRegistered { |
| 23 | return nil |
| 24 | } |
| 25 | if m.runtimeStore == nil { |
| 26 | return fmt.Errorf("nil runtime store") |
| 27 | } |
| 28 | |
| 29 | componentName := strings.TrimSpace(m.runtimeComponentName) |
| 30 | if componentName == "" { |
| 31 | componentName = functionsRuntimeComponentName |
| 32 | } |
| 33 | |
| 34 | cfg := runtimecomp.ComponentConfig{ |
| 35 | Name: componentName, |
| 36 | Store: m.runtimeStore, |
| 37 | UpdateEvery: 1, |
| 38 | Autogen: runtimecomp.AutogenPolicy{ |
| 39 | Enabled: true, |
| 40 | }, |
| 41 | Module: "functions", |
| 42 | JobName: "manager", |
| 43 | JobLabels: map[string]string{ |
| 44 | "component": "functions_manager", |
| 45 | }, |
| 46 | } |
| 47 | if err := m.runtimeService.RegisterComponent(cfg); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | m.runtimeComponentName = componentName |
| 52 | m.runtimeComponentRegistered = true |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | func (m *Manager) unregisterRuntimeComponent() { |
| 57 | if m == nil || m.runtimeService == nil || !m.runtimeComponentRegistered { |
| 58 | return |
| 59 | } |
| 60 | m.runtimeService.UnregisterComponent(m.runtimeComponentName) |
| 61 | m.runtimeComponentRegistered = false |
| 62 | } |