sink.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "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)
  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. return err
  64. }
  65. ps.clean = func() error {
  66. ctx.GetLogger().Info("closing sink data channe")
  67. dataCh.Close()
  68. return ins.StopSymbol(ctx, c)
  69. }
  70. ps.dataCh = dataCh
  71. return nil
  72. }
  73. func (ps *PortableSink) Collect(ctx api.StreamContext, item interface{}) error {
  74. ctx.GetLogger().Debugf("Receive %+v", item)
  75. if val, _, err := ctx.TransformOutput(item); err == nil {
  76. ctx.GetLogger().Debugf("Send %s", val)
  77. e := ps.dataCh.Send(val)
  78. if e != nil {
  79. return fmt.Errorf("%s:%s", errorx.IOErr, e)
  80. }
  81. return nil
  82. } else {
  83. ctx.GetLogger().Errorf("Found error %s", err.Error())
  84. return err
  85. }
  86. }
  87. func (ps *PortableSink) Close(ctx api.StreamContext) error {
  88. if ps.clean != nil {
  89. return ps.clean()
  90. }
  91. return nil
  92. }