Method: Fluent::Plugin::ExecOutput#try_write

Defined in:
lib/fluent/plugin/out_exec.rb

#try_write(chunk) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fluent/plugin/out_exec.rb', line 69

def try_write(chunk)
  tmpfile = nil
  prog = if chunk.respond_to?(:path)
           "#{@command} #{chunk.path}"
         else
           tmpfile = Tempfile.new("fluent-plugin-out-exec-")
           tmpfile.binmode
           chunk.write_to(tmpfile)
           tmpfile.close
           "#{@command} #{tmpfile.path}"
         end
  chunk_id = chunk.unique_id
  callback = ->(status){
    begin
      if tmpfile
        tmpfile.delete rescue nil
      end
      if status && status.success?
        commit_write(chunk_id)
      elsif status
        # #rollback_write will be done automatically if it isn't called at here.
        # But it's after command_timeout, and this timeout should be longer than users expectation.
        # So here, this plugin calls it explicitly.
        rollback_write(chunk_id)
        log.warn "command exits with error code", prog: prog, status: status.exitstatus, signal: status.termsig
      else
        rollback_write(chunk_id)
        log.warn "command unexpectedly exits without exit status", prog: prog
      end
    rescue => e
      log.error "unexpected error in child process callback", error: e
    end
  }
  child_process_execute(:out_exec_process, prog, stderr: :connect, immediate: true, parallel: true, mode: [], wait_timeout: @command_timeout, on_exit_callback: callback)
end