sink.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2021-2023 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. "errors"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/errorx"
  20. )
  21. type PortableSink struct {
  22. symbolName string
  23. reg *PluginMeta
  24. props map[string]interface{}
  25. dataCh DataOutChannel
  26. clean func() error
  27. }
  28. func NewPortableSink(symbolName string, reg *PluginMeta) *PortableSink {
  29. return &PortableSink{
  30. symbolName: symbolName,
  31. reg: reg,
  32. }
  33. }
  34. func (ps *PortableSink) Configure(props map[string]interface{}) error {
  35. ps.props = props
  36. return nil
  37. }
  38. func (ps *PortableSink) Open(ctx api.StreamContext) error {
  39. ctx.GetLogger().Infof("Start running portable sink %s with conf %+v", ps.symbolName, ps.props)
  40. pm := GetPluginInsManager()
  41. ins, err := pm.getOrStartProcess(ps.reg, PortbleConf)
  42. if err != nil {
  43. return err
  44. }
  45. ctx.GetLogger().Infof("Plugin started successfully")
  46. // Control: send message to plugin to ask starting symbol
  47. c := &Control{
  48. Meta: Meta{
  49. RuleId: ctx.GetRuleId(),
  50. OpId: ctx.GetOpId(),
  51. InstanceId: ctx.GetInstanceId(),
  52. },
  53. SymbolName: ps.symbolName,
  54. PluginType: TYPE_SINK,
  55. Config: ps.props,
  56. }
  57. err = ins.StartSymbol(ctx, c)
  58. if err != nil {
  59. return err
  60. }
  61. // must start symbol firstly
  62. dataCh, err := CreateSinkChannel(ctx)
  63. if err != nil {
  64. _ = ins.StopSymbol(ctx, c)
  65. return err
  66. }
  67. ps.clean = func() error {
  68. ctx.GetLogger().Info("clean up sink")
  69. err1 := dataCh.Close()
  70. err2 := ins.StopSymbol(ctx, c)
  71. if err1 != nil {
  72. err1 = fmt.Errorf("%s:%v", "dataCh", err1)
  73. }
  74. if err2 != nil {
  75. err2 = fmt.Errorf("%s:%v", "symbol", err2)
  76. }
  77. return errors.Join(err1, err2)
  78. }
  79. ps.dataCh = dataCh
  80. return nil
  81. }
  82. func (ps *PortableSink) Collect(ctx api.StreamContext, item interface{}) error {
  83. ctx.GetLogger().Debugf("Receive %+v", item)
  84. if val, _, err := ctx.TransformOutput(item); err == nil {
  85. ctx.GetLogger().Debugf("Send %s", val)
  86. e := ps.dataCh.Send(val)
  87. if e != nil {
  88. return fmt.Errorf("%s:%s", errorx.IOErr, e)
  89. }
  90. return nil
  91. } else {
  92. ctx.GetLogger().Errorf("Found error %s", err.Error())
  93. return err
  94. }
  95. }
  96. func (ps *PortableSink) Close(ctx api.StreamContext) error {
  97. ctx.GetLogger().Infof("Closing sink %s", ps.symbolName)
  98. if ps.clean != nil {
  99. return ps.clean()
  100. }
  101. return nil
  102. }