Skip to content

Commit

Permalink
Run multiple assets processes
Browse files Browse the repository at this point in the history
  • Loading branch information
timriley committed Jan 16, 2024
1 parent 825e725 commit b3278f8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 6 deletions.
54 changes: 51 additions & 3 deletions lib/hanami/cli/commands/app/assets/compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,66 @@ module Assets
class Compile < Assets::Command
desc "Compile assets for deployments"

def call(**)
slices = app.slices.with_nested + [app]

# capture pids here
start_children(slices)
end

# @since 2.1.0
# @api private
def cmd_with_args
result = super
def cmd_with_args(slice)
result = super()

result << "--"

if slice.eql?(slice.app)
result << "--path=app"
result << "--target=public/assets"
else
result << "--path=#{slice.root.relative_path_from(slice.app.root)}"
result << "--target=public/assets/#{slice.slice_name}"
# TODO: work for nested slices
end

if config.subresource_integrity.any?
result << "--"
result << "--sri=#{escape(config.subresource_integrity.join(','))}"
end

result
end

private

def start_children(slices)
slices.each do |slice|
fork_child(slice)
end
end

def fork_child(slice)
Process.fork do
cmd, *args = cmd_with_args(slice)
p cmd_with_args(slice)
result = system_call.call(cmd, *args)

if result.exit_code == 0
puts result.out

if result.err && result.err != ""
puts ""
puts result.err
end
else
puts "AssetsCompilationError"
puts result.out
puts result.err
raise "AssetsCompilationError"
# raise AssetsCompilationError.new(result.out, result.err)
end
end
end
end
end
end
Expand Down
80 changes: 77 additions & 3 deletions lib/hanami/cli/commands/app/assets/watch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,87 @@ def initialize(config: app.config.assets, system_call: InteractiveSystemCall.new
super(config: config, system_call: system_call)
end

private
def call(**)
slices = app.slices.with_nested + [app]

# capture pids here
pids = start_children(slices)


%w[INT USR1 TERM].each do |sig|
Signal.trap(sig) do
pids.each do |pid|
puts "killing..."
p sig
p pid
Process.kill(sig, pid)
end
end
end

Process.waitall
end

# @since 2.1.0
# @api private
def cmd_with_args
super + ["--", "--watch"]
def cmd_with_args(slice)
result = super()

result << "--"

result << "--watch"

if slice.eql?(slice.app)
result << "--path=app"
result << "--target=public/assets"
else
result << "--path=#{slice.root.relative_path_from(slice.app.root)}"
result << "--target=public/assets/#{slice.slice_name}"
# TODO: work for nested slices
end

result
end

private

def start_children(slices)
slices.map do |slice|
fork_child(slice)
end
end

def fork_child(slice)
Process.fork do
cmd, *args = cmd_with_args(slice)
p cmd_with_args(slice)
result = system_call.call(cmd, *args)

if result.exit_code == 0
puts result.out

if result.err && result.err != ""
puts ""
puts result.err
end
else
puts "AssetsCompilationError"
puts result.out
puts result.err
raise "AssetsCompilationError"
# raise AssetsCompilationError.new(result.out, result.err)
end
end
end


# private

# # @since 2.1.0
# # @api private
# def cmd_with_args
# super + ["--", "--watch"]
# end
end
end
end
Expand Down

0 comments on commit b3278f8

Please sign in to comment.