sink.go 2.4 KB

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