rule_query.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build rpc || !core
  15. // +build rpc !core
  16. package processor
  17. import (
  18. "github.com/lf-edge/ekuiper/internal/topo"
  19. "github.com/lf-edge/ekuiper/internal/topo/node"
  20. "github.com/lf-edge/ekuiper/internal/topo/planner"
  21. "github.com/lf-edge/ekuiper/pkg/infra"
  22. )
  23. func (p *RuleProcessor) ExecQuery(ruleid, sql string) (*topo.Topo, error) {
  24. if tp, err := planner.PlanSQLWithSourcesAndSinks(p.getDefaultRule(ruleid, sql), nil, []*node.SinkNode{node.NewSinkNode("sink_memory_log", "logToMemory", nil)}); err != nil {
  25. return nil, err
  26. } else {
  27. go func() {
  28. err := infra.SafeRun(func() error {
  29. select {
  30. case err := <-tp.Open():
  31. if err != nil {
  32. tp.GetContext().SetError(err)
  33. tp.Cancel()
  34. return err
  35. }
  36. }
  37. return nil
  38. })
  39. if err != nil {
  40. log.Infof("closing query for error: %v", err)
  41. } else {
  42. log.Info("closing query")
  43. }
  44. }()
  45. return tp, nil
  46. }
  47. }