source.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 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. package runtime
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. // Error handling: wrap all error in a function to handle
  21. type PortableSource struct {
  22. symbolName string
  23. reg *PluginMeta
  24. topic string
  25. props map[string]interface{}
  26. }
  27. func NewPortableSource(symbolName string, reg *PluginMeta) *PortableSource {
  28. return &PortableSource{
  29. symbolName: symbolName,
  30. reg: reg,
  31. }
  32. }
  33. func (ps *PortableSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  34. ctx.GetLogger().Infof("Start running portable source %s with datasource %s and conf %+v", ps.symbolName, ps.topic, ps.props)
  35. pm := GetPluginInsManager()
  36. ins, err := pm.getOrStartProcess(ps.reg, PortbleConf)
  37. if err != nil {
  38. errCh <- err
  39. return
  40. }
  41. ctx.GetLogger().Infof("Plugin started successfully")
  42. // wait for plugin data
  43. dataCh, err := CreateSourceChannel(ctx)
  44. if err != nil {
  45. errCh <- err
  46. return
  47. }
  48. defer func() {
  49. ctx.GetLogger().Info("Closing source data channel")
  50. dataCh.Close()
  51. }()
  52. // Control: send message to plugin to ask starting symbol
  53. c := &Control{
  54. Meta: &Meta{
  55. RuleId: ctx.GetRuleId(),
  56. OpId: ctx.GetOpId(),
  57. InstanceId: ctx.GetInstanceId(),
  58. },
  59. SymbolName: ps.symbolName,
  60. PluginType: TYPE_SOURCE,
  61. DataSource: ps.topic,
  62. Config: ps.props,
  63. }
  64. err = ins.StartSymbol(ctx, c)
  65. if err != nil {
  66. errCh <- err
  67. return
  68. }
  69. defer ins.StopSymbol(ctx, c)
  70. for {
  71. var msg []byte
  72. msg, err = dataCh.Recv()
  73. if err != nil {
  74. errCh <- fmt.Errorf("cannot receive from mangos Socket: %s", err.Error())
  75. return
  76. }
  77. result := &api.DefaultSourceTuple{}
  78. e := json.Unmarshal(msg, result)
  79. if e != nil {
  80. ctx.GetLogger().Errorf("Invalid data format, cannot decode %s to json format with error %s", string(msg), e)
  81. continue
  82. }
  83. select {
  84. case consumer <- result:
  85. ctx.GetLogger().Debugf("send data to source node")
  86. case <-ctx.Done():
  87. ctx.GetLogger().Info("stop source")
  88. return
  89. }
  90. }
  91. }
  92. func (ps *PortableSource) Configure(topic string, props map[string]interface{}) error {
  93. ps.topic = topic
  94. ps.props = props
  95. return nil
  96. }
  97. func (ps *PortableSource) Close(ctx api.StreamContext) error {
  98. ctx.GetLogger().Infof("Closing source %s", ps.symbolName)
  99. return nil
  100. }