file_writer.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Whether the file has written any data. It is only used to determine if new line is needed when writing data.
  31. Written bool
  32. }
  33. func createFileWriter(ctx api.StreamContext, fn string, ft FileType, headers string) (_ *fileWriter, ge error) {
  34. ctx.GetLogger().Infof("Create new file writer for %s", fn)
  35. fws := &fileWriter{Start: conf.GetNow()}
  36. var (
  37. f *os.File
  38. err error
  39. )
  40. if _, err = os.Stat(fn); os.IsNotExist(err) {
  41. _, err = os.Create(fn)
  42. }
  43. f, err = os.OpenFile(fn, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
  44. if err != nil {
  45. return nil, fmt.Errorf("fail to open file sink for %s: %v", fn, err)
  46. }
  47. defer func() {
  48. if ge != nil {
  49. _ = f.Close()
  50. }
  51. }()
  52. fws.File = f
  53. switch ft {
  54. case JSON_TYPE:
  55. fws.Hook = jsonHooks
  56. case CSV_TYPE:
  57. fws.Hook = &csvWriterHooks{header: []byte(headers)}
  58. case LINES_TYPE:
  59. fws.Hook = linesHooks
  60. }
  61. fws.Writer = bufio.NewWriter(f)
  62. _, err = fws.Writer.Write(fws.Hook.Header())
  63. if err != nil {
  64. return nil, err
  65. }
  66. return fws, nil
  67. }
  68. func (fw *fileWriter) Close(ctx api.StreamContext) error {
  69. if fw.File != nil {
  70. ctx.GetLogger().Debugf("File sync before close")
  71. _, e := fw.Writer.Write(fw.Hook.Footer())
  72. if e != nil {
  73. ctx.GetLogger().Errorf("file sink fails to write footer with error %s.", e)
  74. }
  75. err := fw.Writer.(*bufio.Writer).Flush()
  76. if err != nil {
  77. ctx.GetLogger().Errorf("file sink fails to flush with error %s.", err)
  78. }
  79. err = fw.File.Sync()
  80. if err != nil {
  81. ctx.GetLogger().Errorf("file sink fails to sync with error %s.", err)
  82. }
  83. ctx.GetLogger().Infof("Close file %s", fw.File.Name())
  84. return fw.File.Close()
  85. }
  86. return nil
  87. }