sink.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2021-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. package runtime
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/errorx"
  19. )
  20. type PortableSink struct {
  21. symbolName string
  22. reg *PluginMeta
  23. props map[string]interface{}
  24. dataCh DataOutChannel
  25. clean func() error
  26. }
  27. func NewPortableSink(symbolName string, reg *PluginMeta) *PortableSink {
  28. return &PortableSink{
  29. symbolName: symbolName,
  30. reg: reg,
  31. }
  32. }
  33. func (ps *PortableSink) Configure(props map[string]interface{}) error {
  34. ps.props = props
  35. return nil
  36. }
  37. func (ps *PortableSink) Open(ctx api.StreamContext) error {
  38. ctx.GetLogger().Infof("Start running portable sink %s with conf %+v", ps.symbolName, ps.props)
  39. pm := GetPluginInsManager()
  40. ins, err := pm.getOrStartProcess(ps.reg, PortbleConf, false)
  41. if err != nil {
  42. return err
  43. }
  44. ctx.GetLogger().Infof("Plugin started successfully")
  45. // Control: send message to plugin to ask starting symbol
  46. c := &Control{
  47. Meta: Meta{
  48. RuleId: ctx.GetRuleId(),
  49. OpId: ctx.GetOpId(),
  50. InstanceId: ctx.GetInstanceId(),
  51. },
  52. SymbolName: ps.symbolName,
  53. PluginType: TYPE_SINK,
  54. Config: ps.props,
  55. }
  56. err = ins.StartSymbol(ctx, c)
  57. if err != nil {
  58. return err
  59. }
  60. // must start symbol firstly
  61. dataCh, err := CreateSinkChannel(ctx)
  62. if err != nil {
  63. _ = ins.StopSymbol(ctx, c)
  64. return err
  65. }
  66. ps.clean = func() error {
  67. ctx.GetLogger().Info("clean up sink")
  68. err1 := dataCh.Close()
  69. err2 := ins.StopSymbol(ctx, c)
  70. e := make(errorx.MultiError)
  71. if err1 != nil {
  72. e["dataCh"] = err1
  73. }
  74. if err != nil {
  75. e["symbol"] = err2
  76. }
  77. return e.GetError()
  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. }