Module: RakeAudit::Builders::TaskExecutionRecordBuilder

Defined in:
lib/rake_audit/builders/task_execution_record_builder.rb

Overview

Assembles a TaskExecutionRecord from the raw inputs gathered by ExecutionRecorder: the task, its arguments, the timing window, and any exception that was raised.

Environment fields (hostname, pid, ruby version, rails env) are captured here and are individually gated by the active Configuration.

Class Method Summary collapse

Class Method Details

.build(task:, args:, started_at:, finished_at:, exception:, config:) ⇒ RakeAudit::TaskExecutionRecord

Build a record describing one execution.

rubocop:disable Metrics/ParameterLists, Metrics/CyclomaticComplexity

Parameters:

  • task (#name)

    the executed Rake task (only #name is required).

  • args (Object, nil)

    arguments passed to the task.

  • started_at (Time)

    when execution began.

  • finished_at (Time)

    when execution finished (success or failure).

  • exception (Exception, nil)

    the raised exception, if the task failed.

  • config (RakeAudit::Configuration)

    configuration controlling capture.

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rake_audit/builders/task_execution_record_builder.rb', line 28

def build(task:, args:, started_at:, finished_at:, exception:, config:)
  TaskExecutionRecord.new(
    task_name: task_name_for(task),
    arguments: normalize_arguments(args),
    started_at: started_at,
    finished_at: finished_at,
    duration_ms: duration_ms_for(started_at, finished_at),
    status: exception ? 'failure' : 'success',
    error_class: exception&.class&.name,
    error_message: exception&.message,
    hostname: config.capture_hostname ? Socket.gethostname : nil,
    pid: config.capture_pid ? Process.pid : nil,
    ruby_version: config.capture_ruby_version ? RUBY_VERSION : nil,
    rails_env: config.capture_rails_env ? rails_env : nil
  )
end

.duration_ms_for(started_at, finished_at) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns elapsed wall-clock time in whole milliseconds.

Returns:

  • (Integer)

    elapsed wall-clock time in whole milliseconds.



68
69
70
# File 'lib/rake_audit/builders/task_execution_record_builder.rb', line 68

def duration_ms_for(started_at, finished_at)
  ((finished_at - started_at) * 1000).round
end

.normalize_arguments(args) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce the task arguments into a plain Hash for stable serialization.

Rake passes a Rake::TaskArguments which responds to #to_hash; we also accept a raw Hash and treat anything else (including nil) as empty.

Returns:

  • (Hash)


58
59
60
61
62
63
64
# File 'lib/rake_audit/builders/task_execution_record_builder.rb', line 58

def normalize_arguments(args)
  if args.respond_to?(:to_hash)
    args.to_hash
  else
    {}
  end
end

.rails_envString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current Rails environment, when available.

Returns:

  • (String, nil)

    the current Rails environment, when available.



74
75
76
77
78
# File 'lib/rake_audit/builders/task_execution_record_builder.rb', line 74

def rails_env
  return nil unless defined?(Rails) && Rails.respond_to?(:env)

  Rails.env.to_s
end

.task_name_for(task) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
# File 'lib/rake_audit/builders/task_execution_record_builder.rb', line 47

def task_name_for(task)
  task.respond_to?(:name) ? task.name.to_s : task.to_s
end