rake-ui 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1908294b9112c4e194b37f62602869d5440b834ea96d1a608b4442bacd68931c
4
- data.tar.gz: 43cfb8e2c716024dd9f32f6653da422d6349b43ed303bf6f184e5d4274ed86b2
3
+ metadata.gz: 4cea89de28dcbfea01b180ba662855069bbbdbb2a79853e5c89548fa7a60364d
4
+ data.tar.gz: 450955682308674c13a2f1d7eb3d669c9f77990014cd4c4eee656c126196ed04
5
5
  SHA512:
6
- metadata.gz: 3e2615ec56b69c6188487ab893304f22a81cd9a9b3888ecbf52dd3ca5b50ef3aa7079a7863066218cbad28167c50443a574d4a58b9d427d0492d66e92e37d7a4
7
- data.tar.gz: 527eeb3ff132ad75e18f8125da4d6eabf5f770279ae586e24219a8aaaf23e5ad7d5b2d93ad68fbce9207c6453c533de5517de63ecc924cf842422cff9027a647
6
+ metadata.gz: ada3110d1742c286911d065bc64865770f9f0ffbf543f432c69da5ec91d8d805dc5360238aa7178b70d4ed7f022dc53b8059936c025971e5d2233430b3137718
7
+ data.tar.gz: f8afc9c092569f918d5d1790e1f01ab16d5d933273ff1cdd465aa560e8296e271835f9e487d3a16931cf030c4dba7136353a935001c9b2ccd1c80e67331edd40
data/README.md CHANGED
@@ -54,6 +54,33 @@ end
54
54
 
55
55
  We recommend adding guards in your route to ensure that the proper authentication is in place to ensure that users are authenticated so that if this were ever to be rendered in production, you would be covered. The best way for that is [router constraints](https://guides.rubyonrails.org/routing.html#specifying-constraints)
56
56
 
57
+ ### Log storage callbacks
58
+
59
+ By default a rake task log is written to `Rails.root/tmp/rake_ui` on the machine
60
+ that ran the task. In a multi-pod deployment another pod may serve the log page
61
+ and not have that file. These optional hooks let you point rake-ui at shared
62
+ storage:
63
+
64
+ ```rb
65
+ RakeUi.configuration do |config|
66
+ # Publish each log somewhere shared. Called with (log, event, payload) for
67
+ # :start, :progress, :finished and :error. Progress payloads are output lines.
68
+ config.log_writer = ->(log, event, payload = nil) { ... }
69
+
70
+ # Return {content: "log text", finished: true/false} for a log not on this pod.
71
+ config.log_reader = ->(log) { ... }
72
+
73
+ # Return the ids of logs stored outside this pod, for the index page.
74
+ config.log_list = -> { ["<log id>", ...] }
75
+ end
76
+ ```
77
+
78
+ All hooks default to `nil`, so the default file-backed behavior is unchanged.
79
+ The writer runs in the forked child process, so build connections lazily after
80
+ fork. If a writer raises, the error is logged and the task keeps running. Task
81
+ output can contain secrets or personal data; only enable a writer when the
82
+ destination and its retention meet your policy.
83
+
57
84
  ## Testing
58
85
 
59
86
  `bundle exec rake test`
@@ -26,10 +26,18 @@ module RakeUi
26
26
 
27
27
  def show
28
28
  @rake_task_log = RakeUi::RakeTaskLog.find_by_id(params[:id])
29
+ return render_log_not_found unless @rake_task_log
30
+
31
+ begin
32
+ source = @rake_task_log.log_source
33
+ @rake_task_log_content = source[:content]
34
+ @is_rake_task_log_finished = source[:finished]
35
+ rescue RakeUi::RakeTaskLog::MissingLogFile
36
+ Rails.logger.warn("[rake_ui] rake task log not found id=#{params[:id].to_s.inspect}")
37
+ return render_log_not_found
38
+ end
29
39
 
30
- @rake_task_log_content = @rake_task_log.file_contents
31
40
  @rake_task_log_content_url = rake_task_log_path(@rake_task_log.id, format: :json)
32
- @is_rake_task_log_finished = @rake_task_log.finished?
33
41
 
34
42
  respond_to do |format|
35
43
  format.html
@@ -46,6 +54,13 @@ module RakeUi
46
54
 
47
55
  private
48
56
 
57
+ def render_log_not_found
58
+ respond_to do |format|
59
+ format.html { head :not_found }
60
+ format.json { render json: {error: "Rake task log not found"}, status: :not_found }
61
+ end
62
+ end
63
+
49
64
  def rake_task_log_as_json(task)
50
65
  RAKE_TASK_LOG_ATTRS.each_with_object({}) do |param, obj|
51
66
  obj[param] = task.send(param)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "cgi"
3
4
  require "shellwords"
4
5
 
5
6
  module RakeUi
@@ -94,12 +95,18 @@ module RakeUi
94
95
  raker_id: id
95
96
  )
96
97
 
97
- puts "[rake_ui] [rake_task] [forked] #{rake_task_log.rake_command_with_logging}"
98
+ log_writer = RakeUi.log_writer
99
+ command = log_writer ? rake_task_log.rake_command : rake_task_log.rake_command_with_logging
100
+ puts "[rake_ui] [rake_task] [forked] #{command}"
98
101
 
99
102
  fork do
100
- system(rake_task_log.rake_command_with_logging)
103
+ if log_writer
104
+ rake_task_log.run_with_log_writer(log_writer)
105
+ else
106
+ system(rake_task_log.rake_command_with_logging)
101
107
 
102
- system(rake_task_log.command_to_mark_log_finished)
108
+ system(rake_task_log.command_to_mark_log_finished)
109
+ end
103
110
  end
104
111
 
105
112
  rake_task_log
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ostruct"
4
+ require "open3"
5
+ require "shellwords"
6
+
3
7
  module RakeUi
4
8
  class RakeTaskLog < OpenStruct
9
+ # Raised when the pod serving the request no longer has the log file, for
10
+ # example when the file was removed between listing and reading it.
11
+ class MissingLogFile < StandardError; end
12
+
5
13
  # year-month-day-hour(24hour time)-minute-second-nanosecond-utc
6
14
  ID_DATE_FORMAT = "%Y-%m-%d-%H-%M-%S-%N%z"
7
15
  REPOSITORY_DIR = Rails.root.join("tmp", "rake_ui")
@@ -24,7 +32,7 @@ module RakeUi
24
32
  new(
25
33
  id: log_file_name.gsub(".txt", ""),
26
34
  log_file_name: log_file_name,
27
- log_file_full_path: Rails.root.join(REPOSITORY_DIR, log_file_name).to_s
35
+ log_file_full_path: REPOSITORY_DIR.join(log_file_name).to_s
28
36
  )
29
37
  end
30
38
 
@@ -65,14 +73,42 @@ module RakeUi
65
73
  def self.all
66
74
  create_tmp_file_dir
67
75
 
68
- Dir.entries(REPOSITORY_DIR).reject { |file|
76
+ local = Dir.entries(REPOSITORY_DIR).reject { |file|
69
77
  file == "." || file == ".."
70
78
  }.map do |log|
71
79
  RakeUi::RakeTaskLog.build_from_file(log)
72
80
  end
81
+
82
+ remote = log_list_ids.map { |id| build_from_id(id) }
83
+ (local + remote).uniq(&:id)
84
+ end
85
+
86
+ def self.log_list_ids
87
+ Array(RakeUi.log_list&.call)
88
+ rescue => e
89
+ Rails.logger&.warn("[rake_ui] log list failed error=#{e.class}: #{e.message.to_s[0, 200]}")
90
+ []
91
+ end
92
+
93
+ def self.build_from_id(id)
94
+ id = id.to_s.delete_suffix(".txt")
95
+ new(id: id, log_file_name: "#{id}.txt")
73
96
  end
74
97
 
75
98
  def self.find_by_id(id)
99
+ if RakeUi.log_reader
100
+ log_file_name = "#{id.to_s.delete_suffix(".txt")}.txt"
101
+ local_path = begin
102
+ path = REPOSITORY_DIR.join(log_file_name)
103
+ build_from_file(log_file_name) if path.basename.to_s == log_file_name && path.exist?
104
+ rescue ArgumentError, SystemCallError
105
+ nil
106
+ end
107
+ return local_path if local_path
108
+
109
+ return build_from_id(id)
110
+ end
111
+
76
112
  all.find do |a|
77
113
  a.id == id || a.id == RakeUi::RakeTask.to_safe_identifier(id)
78
114
  end
@@ -117,29 +153,91 @@ module RakeUi
117
153
  end
118
154
 
119
155
  def rake_command_with_logging
120
- "#{rake_command} 2>&1 >> #{log_file_full_path}"
156
+ "#{rake_command} >> #{Shellwords.escape(log_file_full_path)} 2>&1"
157
+ end
158
+
159
+ def log_source
160
+ if log_file_full_path.present? || RakeUi.log_reader.nil?
161
+ return {content: file_contents, finished: finished?}
162
+ end
163
+
164
+ result = begin
165
+ RakeUi.log_reader.call(self)
166
+ rescue => e
167
+ Rails.logger&.warn("[rake_ui] log reader failed id=#{id.to_s.inspect} error=#{e.class}: #{e.message.to_s[0, 200]}")
168
+ nil
169
+ end
170
+ raise MissingLogFile, id unless result.is_a?(Hash)
171
+
172
+ {content: result[:content].to_s, finished: !!result[:finished]}
173
+ end
174
+
175
+ def run_with_log_writer(writer)
176
+ publish_log_event(writer, :start)
177
+
178
+ status = nil
179
+ last_line = nil
180
+
181
+ File.open(log_file_full_path, "a") do |file|
182
+ Open3.popen2e(rake_command) do |stdin, stdout_and_stderr, wait_thread|
183
+ stdin.close
184
+
185
+ stdout_and_stderr.each_line do |line|
186
+ last_line = line
187
+ file.write(line)
188
+ file.flush
189
+ publish_log_event(writer, :progress, line)
190
+ end
191
+
192
+ status = wait_thread.value
193
+ end
194
+ ensure
195
+ file.write("\n") if last_line && !last_line.end_with?("\n")
196
+ file.puts FINISHED_STRING
197
+ file.flush
198
+ end
199
+
200
+ if status&.success?
201
+ publish_log_event(writer, :finished)
202
+ else
203
+ publish_log_event(writer, :error, status ? status.to_s : "command failed")
204
+ end
205
+ rescue => e
206
+ publish_log_event(writer, :error, e.message)
121
207
  end
122
208
 
123
209
  def file_contents
124
- @file_contents ||= File.read(log_file_full_path)
210
+ path = log_file_full_path
211
+ raise MissingLogFile, id if path.blank?
212
+
213
+ @file_contents ||= File.read(path)
214
+ rescue Errno::ENOENT
215
+ raise MissingLogFile, path
125
216
  end
126
217
 
127
218
  def command_to_mark_log_finished
128
- "echo #{FINISHED_STRING} >> #{log_file_full_path}"
219
+ "echo #{Shellwords.escape(FINISHED_STRING)} >> #{Shellwords.escape(log_file_full_path)}"
129
220
  end
130
221
 
131
222
  def finished?
132
- file_contents.include? FINISHED_STRING
223
+ file_contents.b.include? FINISHED_STRING.b
133
224
  end
134
225
 
135
226
  private
136
227
 
228
+ # A failing writer must not abort the task.
229
+ def publish_log_event(writer, event, payload = nil)
230
+ writer.call(self, event, payload)
231
+ rescue => e
232
+ Rails.logger&.warn("[rake_ui] log writer failed event=#{event} error=#{e.class}: #{e.message.to_s[0, 200]}")
233
+ end
234
+
137
235
  # converts standard formatted file id into an object
138
236
  def parsed_log_file_name
139
237
  @parsed_log_file_name ||= {}.tap do |parsed|
140
238
  date, name = id.split(FILE_DELIMITER, 2)
141
239
  parsed[:date] = date
142
- parsed[:name] = RakeUi::RakeTask.from_safe_identifier(name)
240
+ parsed[:name] = name && RakeUi::RakeTask.from_safe_identifier(name)
143
241
  end
144
242
  end
145
243
 
@@ -153,14 +251,19 @@ module RakeUi
153
251
  def parsed_file_contents
154
252
  return @parsed_file_contents if defined? @parsed_file_contents
155
253
 
254
+ path = self[:log_file_full_path]
255
+ return @parsed_file_contents = {}.with_indifferent_access if path.blank?
256
+
156
257
  @parsed_file_contents = {}.tap do |parsed|
157
- File.foreach(log_file_full_path).first(9).each do |line|
258
+ File.foreach(path).first(9).each do |line|
158
259
  name, value = line.split(FILE_ITEM_SEPARATOR, 2)
159
260
  next unless name
160
261
 
161
262
  parsed[name] = value && value.delete("\n")
162
263
  end
163
264
  end.with_indifferent_access
265
+ rescue Errno::ENOENT
266
+ @parsed_file_contents = {}.with_indifferent_access
164
267
  end
165
268
  end
166
269
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RakeUi
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/rake-ui.rb CHANGED
@@ -6,6 +6,10 @@ module RakeUi
6
6
  mattr_accessor :allow_production
7
7
  self.allow_production = false
8
8
 
9
+ mattr_accessor :log_reader
10
+ mattr_accessor :log_list
11
+ mattr_accessor :log_writer
12
+
9
13
  def self.configuration
10
14
  yield(self) if block_given?
11
15
  self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Story
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-08-27 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cgi
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ostruct
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: railties
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -131,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
159
  - !ruby/object:Gem::Version
132
160
  version: '0'
133
161
  requirements: []
134
- rubygems_version: 3.1.6
162
+ rubygems_version: 3.5.22
135
163
  signing_key:
136
164
  specification_version: 4
137
165
  summary: A Mountable Rails Engine to manage Rake Tasks through a UI