Class: RakeUi::RakeTaskLog

Inherits:
OpenStruct
  • Object
show all
Defined in:
app/models/rake_ui/rake_task_log.rb

Constant Summary collapse

ID_DATE_FORMAT =

year-month-day-hour(24hour time)-minute-second-utc

"%Y-%m-%d-%H-%M-%S%z"
REPOSITORY_DIR =
Rails.root.join("tmp", "rake_ui")
FILE_DELIMITER =
"____"
FINISHED_STRING =
"+++++ COMMAND FINISHED +++++"
TASK_HEADER_OUTPUT_DELIMITER =
"-------------------------------"
FILE_ITEM_SEPARATOR =
": "

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



65
66
67
68
69
70
71
72
73
# File 'app/models/rake_ui/rake_task_log.rb', line 65

def self.all
  create_tmp_file_dir

  Dir.entries(REPOSITORY_DIR).reject { |file|
    file == "." || file == ".."
  }.map do |log|
    RakeUi::RakeTaskLog.build_from_file(log)
  end
end

.build_from_file(log_file_name) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/models/rake_ui/rake_task_log.rb', line 21

def self.build_from_file(log_file_name)
  log_file_name.split(FILE_DELIMITER)

  new(
    id: log_file_name.gsub(".txt", ""),
    log_file_name: log_file_name,
    log_file_full_path: Rails.root.join(REPOSITORY_DIR, log_file_name).to_s
  )
end

.build_new_for_command(name:, rake_definition_file:, rake_command:, raker_id:, args: nil, environment: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/rake_ui/rake_task_log.rb', line 31

def self.build_new_for_command(name:, rake_definition_file:, rake_command:, raker_id:, args: nil, environment: nil)
  create_tmp_file_dir

  date = Time.now.strftime(ID_DATE_FORMAT)
  id = "#{date}#{FILE_DELIMITER}#{raker_id}"
  log_file_name = "#{id}.txt"
  log_file_full_path = REPOSITORY_DIR.join(log_file_name).to_s

  File.open(log_file_full_path, "w+") do |f|
    f.puts "id#{FILE_ITEM_SEPARATOR}#{id}"
    f.puts "name#{FILE_ITEM_SEPARATOR}#{name}"
    f.puts "date#{FILE_ITEM_SEPARATOR}#{date}"
    f.puts "args#{FILE_ITEM_SEPARATOR}#{args}"
    f.puts "environment#{FILE_ITEM_SEPARATOR}#{environment}"
    f.puts "rake_command#{FILE_ITEM_SEPARATOR}#{rake_command}"
    f.puts "rake_definition_file#{FILE_ITEM_SEPARATOR}#{rake_definition_file}"
    f.puts "log_file_name#{FILE_ITEM_SEPARATOR}#{log_file_name}"
    f.puts "log_file_full_path#{FILE_ITEM_SEPARATOR}#{log_file_full_path}"

    f.puts TASK_HEADER_OUTPUT_DELIMITER.to_s
    f.puts " INVOKED RAKE TASK OUTPUT BELOW"
    f.puts TASK_HEADER_OUTPUT_DELIMITER.to_s
  end

  new(id: id,
    name: name,
    args: args,
    environment: environment,
    rake_command: rake_command,
    rake_definition_file: rake_definition_file,
    log_file_name: log_file_name,
    log_file_full_path: log_file_full_path)
end

.create_tmp_file_dirObject



13
14
15
# File 'app/models/rake_ui/rake_task_log.rb', line 13

def self.create_tmp_file_dir
  FileUtils.mkdir_p(REPOSITORY_DIR.to_s)
end

.find_by_id(id) ⇒ Object



75
76
77
78
79
# File 'app/models/rake_ui/rake_task_log.rb', line 75

def self.find_by_id(id)
  all.find do |a|
    a.id == id || a.id == RakeUi::RakeTask.to_safe_identifier(id)
  end
end

.for(rake_ui_rake_task) ⇒ Object



81
82
83
84
85
# File 'app/models/rake_ui/rake_task_log.rb', line 81

def self.for(rake_ui_rake_task)
  all.select do |history|
    history.id == rake_ui_rake_task.id
  end
end

.truncateObject



17
18
19
# File 'app/models/rake_ui/rake_task_log.rb', line 17

def self.truncate
  FileUtils.rm_rf(Dir.glob(REPOSITORY_DIR.to_s + "/*"))
end

Instance Method Details

#argsObject



95
96
97
# File 'app/models/rake_ui/rake_task_log.rb', line 95

def args
  super || parsed_file_contents[:args]
end

#command_to_mark_log_finishedObject



127
128
129
# File 'app/models/rake_ui/rake_task_log.rb', line 127

def command_to_mark_log_finished
  "echo #{FINISHED_STRING} >> #{log_file_full_path}"
end

#dateObject



91
92
93
# File 'app/models/rake_ui/rake_task_log.rb', line 91

def date
  super || parsed_log_file_name[:date] || parsed_file_contents[:date]
end

#environmentObject



99
100
101
# File 'app/models/rake_ui/rake_task_log.rb', line 99

def environment
  super || parsed_file_contents[:environment]
end

#file_contentsObject



123
124
125
# File 'app/models/rake_ui/rake_task_log.rb', line 123

def file_contents
  @file_contents ||= File.read(log_file_full_path)
end

#finished?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/rake_ui/rake_task_log.rb', line 131

def finished?
  file_contents.include? FINISHED_STRING
end

#log_file_full_pathObject



115
116
117
# File 'app/models/rake_ui/rake_task_log.rb', line 115

def log_file_full_path
  super || parsed_file_contents[:log_file_full_path]
end

#log_file_nameObject



111
112
113
# File 'app/models/rake_ui/rake_task_log.rb', line 111

def log_file_name
  super || parsed_file_contents[:log_file_name]
end

#nameObject



87
88
89
# File 'app/models/rake_ui/rake_task_log.rb', line 87

def name
  super || parsed_log_file_name[:name] || parsed_file_contents[:name]
end

#rake_commandObject



103
104
105
# File 'app/models/rake_ui/rake_task_log.rb', line 103

def rake_command
  super || parsed_file_contents[:rake_command]
end

#rake_command_with_loggingObject



119
120
121
# File 'app/models/rake_ui/rake_task_log.rb', line 119

def rake_command_with_logging
  "#{rake_command} 2>&1 >> #{log_file_full_path}"
end

#rake_definition_fileObject



107
108
109
# File 'app/models/rake_ui/rake_task_log.rb', line 107

def rake_definition_file
  super || parsed_file_contents[:rake_definition_file]
end