file_writer.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 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 file
  15. import (
  16. "bufio"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "io"
  21. "os"
  22. "time"
  23. )
  24. type fileWriter struct {
  25. File *os.File
  26. Writer io.Writer
  27. Hook writerHooks
  28. Start time.Time
  29. Count int
  30. }
  31. func createFileWriter(ctx api.StreamContext, fn string, ft FileType, headers string) (_ *fileWriter, ge error) {
  32. ctx.GetLogger().Infof("Create new file writer for %s", fn)
  33. fws := &fileWriter{Start: conf.GetNow()}
  34. var (
  35. f *os.File
  36. err error
  37. )
  38. if _, err = os.Stat(fn); os.IsNotExist(err) {
  39. _, err = os.Create(fn)
  40. }
  41. f, err = os.OpenFile(fn, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
  42. if err != nil {
  43. return nil, fmt.Errorf("fail to open file sink for %s: %v", fn, err)
  44. }
  45. defer func() {
  46. if ge != nil {
  47. _ = f.Close()
  48. }
  49. }()
  50. fws.File = f
  51. switch ft {
  52. case JSON_TYPE:
  53. fws.Hook = jsonHooks
  54. case CSV_TYPE:
  55. fws.Hook = &csvWriterHooks{header: []byte(headers)}
  56. case LINES_TYPE:
  57. fws.Hook = linesHooks
  58. }
  59. fws.Writer = bufio.NewWriter(f)
  60. n, err := fws.Writer.Write(fws.Hook.Header())
  61. if err != nil {
  62. return nil, err
  63. }
  64. if n > 0 {
  65. _, e := fws.Writer.Write(fws.Hook.Line())
  66. if e != nil {
  67. return nil, err
  68. }
  69. }
  70. return fws, nil
  71. }
  72. func (fw *fileWriter) Close(ctx api.StreamContext) error {
  73. if fw.File != nil {
  74. ctx.GetLogger().Debugf("File sync before close")
  75. _, e := fw.Writer.Write(fw.Hook.Footer())
  76. if e != nil {
  77. ctx.GetLogger().Errorf("file sink fails to write footer with error %s.", e)
  78. }
  79. err := fw.Writer.(*bufio.Writer).Flush()
  80. if err != nil {
  81. ctx.GetLogger().Errorf("file sink fails to flush with error %s.", err)
  82. }
  83. err = fw.File.Sync()
  84. if err != nil {
  85. ctx.GetLogger().Errorf("file sink fails to sync with error %s.", err)
  86. }
  87. ctx.GetLogger().Infof("Close file %s", fw.File.Name())
  88. return fw.File.Close()
  89. }
  90. return nil
  91. }