read_file_agent.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. module Agents
  2. class ReadFileAgent < Agent
  3. include FormConfigurable
  4. include FileHandling
  5. cannot_be_scheduled!
  6. consumes_file_pointer!
  7. def default_options
  8. {
  9. 'data_key' => 'data'
  10. }
  11. end
  12. description do
  13. <<~MD
  14. The ReadFileAgent takes events from `FileHandling` agents, reads the file, and emits the contents as a string.
  15. `data_key` specifies the key of the emitted event which contains the file contents.
  16. #{receiving_file_handling_agent_description}
  17. MD
  18. end
  19. event_description <<~MD
  20. Events look like:
  21. {
  22. "data" => '...'
  23. }
  24. MD
  25. form_configurable :data_key, type: :string
  26. def validate_options
  27. if options['data_key'].blank?
  28. errors.add(:base, "The 'data_key' options is required.")
  29. end
  30. end
  31. def working?
  32. received_event_without_error?
  33. end
  34. def receive(incoming_events)
  35. incoming_events.each do |event|
  36. next unless io = get_io(event)
  37. create_event payload: { interpolated['data_key'] => io.read }
  38. end
  39. end
  40. end
  41. end