Sha256: b3fa476f46e25fc07dcf56f023c44ecc9ae6389b4cab4d725318d0796d41fbd3

Contents?: true

Size: 1.28 KB

Versions: 43

Compression:

Stored size: 1.28 KB

Contents

module ForemanTasks
  class ContinuousOutput
    attr_accessor :raw_outputs

    def initialize(raw_outputs = [])
      @raw_outputs = []
      raw_outputs.each { |raw_output| add_raw_output(raw_output) }
    end

    def add_raw_output(raw_output)
      missing_args = %w[output_type output timestamp] - raw_output.keys
      unless missing_args.empty?
        raise ArgumentError, "Missing args for raw output: #{missing_args.inspect}"
      end
      @raw_outputs << raw_output
    end

    def empty?
      @raw_outputs.empty?
    end

    def last_timestamp
      return if @raw_outputs.empty?
      @raw_outputs.last.fetch('timestamp')
    end

    def sort!
      @raw_outputs.sort_by! { |record| record['timestamp'].to_f }
    end

    def humanize
      sort!
      raw_outputs.map { |output| output['output'] }.join("\n")
    end

    def add_exception(context, exception, timestamp = Time.now.getlocal)
      add_output(context + ": #{exception.class} - #{exception.message}", 'debug', timestamp)
    end

    def add_output(*args)
      add_raw_output(self.class.format_output(*args))
    end

    def self.format_output(message, type = 'debug', timestamp = Time.now.getlocal)
      { 'output_type' => type,
        'output' => message,
        'timestamp' => timestamp.to_f }
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
foreman-tasks-10.0.1 lib/foreman_tasks/continuous_output.rb
foreman-tasks-10.0.0 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.2.3 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.2.2 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.2.1 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.2.0 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.1.1 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.0.4 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.1.0 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.0.2 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.0.1 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.3.3 lib/foreman_tasks/continuous_output.rb
foreman-tasks-9.0.0 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.2.1 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.1.4 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.3.2 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.3.1 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.1.3 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.3.0 lib/foreman_tasks/continuous_output.rb
foreman-tasks-8.2.0 lib/foreman_tasks/continuous_output.rb