Explorar o código

feat(server): create init ruleset

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang %!s(int64=2) %!d(string=hai) anos
pai
achega
d111361cfa

+ 5 - 1
docs/en_US/operation/config/configuration_file.md

@@ -185,4 +185,8 @@ This section configures the portable plugin runtime.
       # The executable of python. Specify this if you have multiple python instances in your system
       # or other circumstance where the python executable cannot be successfully invoked through the default command.
       pythonBin: python
-```
+```
+
+## Ruleset Provision
+
+Support file based stream and rule provisioning on startup. Users can put a [ruleset](../restapi/ruleset.md#ruleset-format) file named `init.json` into `data` directory to initialize the ruleset. The ruleset will only be import on the first startup of eKuiper.

+ 5 - 1
docs/zh_CN/operation/config/configuration_file.md

@@ -167,4 +167,8 @@ GET http://localhost:9081/plugins/functions/prebuild
       # 配置 python 可执行文件的位置或命令。
       # 若系统中有多个 python 版本,可通过此配置指定具体的 python 地址。
       pythonBin: python
-```
+```
+
+## 初始化规则集
+
+支持基于文件的流和规则的启动时配置。用户可以将名为 `init.json` 的[规则集](../restapi/ruleset.md#规则集格式)文件放入 `data` 目录,以初始化规则集。该规则集只在eKuiper 第一次启动时被导入。

+ 22 - 0
internal/server/server.go

@@ -16,6 +16,7 @@ package server
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"github.com/lf-edge/ekuiper/internal/binder/function"
 	"github.com/lf-edge/ekuiper/internal/binder/io"
@@ -88,6 +89,7 @@ func StartUp(Version, LoadFileType string) {
 	ruleProcessor = processor.NewRuleProcessor()
 	streamProcessor = processor.NewStreamProcessor()
 	rulesetProcessor = processor.NewRulesetProcessor(ruleProcessor, streamProcessor)
+	initRuleset()
 
 	// register all extensions
 	for k, v := range components {
@@ -176,3 +178,23 @@ func StartUp(Version, LoadFileType string) {
 
 	os.Exit(0)
 }
+
+func initRuleset() error {
+	loc, err := conf.GetDataLoc()
+	if err != nil {
+		return err
+	}
+	signalFile := filepath.Join(loc, "initialized")
+	if _, err := os.Stat(signalFile); errors.Is(err, os.ErrNotExist) {
+		defer os.Create(signalFile)
+		content, err := os.ReadFile(filepath.Join(loc, "init.json"))
+		if err != nil {
+			conf.Log.Infof("fail to read init file: %v", err)
+			return nil
+		}
+		conf.Log.Infof("start to initialize ruleset")
+		_, counts, err := rulesetProcessor.Import(content)
+		conf.Log.Infof("initialzie %d streams, %d tables and %d rules", counts[0], counts[1], counts[2])
+	}
+	return nil
+}