From f3ed882ca43f421af63d6dcbec452263bfa9dbed Mon Sep 17 00:00:00 2001 From: f Date: Wed, 14 Apr 2021 13:09:03 -0300 Subject: [PATCH] =?UTF-8?q?registrar=20si=20fall=C3=B3=20la=20compilaci?= =?UTF-8?q?=C3=B3n.=20=20no=20colgar=20la=20io.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/deploy.rb | 36 +++++++++++++------ app/models/deploy_local.rb | 4 +-- ...0210414152728_add_status_to_build_stats.rb | 8 +++++ 3 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20210414152728_add_status_to_build_stats.rb diff --git a/app/models/deploy.rb b/app/models/deploy.rb index 3611a74..3f034ad 100644 --- a/app/models/deploy.rb +++ b/app/models/deploy.rb @@ -43,29 +43,43 @@ class Deploy < ApplicationRecord @gems_dir ||= Rails.root.join('_storage', 'gems', site.name) end - # Corre un comando y devuelve true si terminó correctamente - # rubocop:disable Metrics/MethodLength + # Corre un comando, lo registra en la base de datos y devuelve el + # estado. + # + # @param [String] + # @return [Boolean] def run(cmd) - # XXX: prestar atención a la concurrencia de sqlite3, se podría - # enviar los datos directamente a una API para que se manejen desde - # el proceso principal de rails y evitar problemas. - stat = build_stats.build action: cmd.split(' -', 2).first.tr(' ', '_') r = nil + lines = [] time_start Dir.chdir(site.path) do Open3.popen2e(env, cmd, unsetenv_others: true) do |_, o, t| r = t.value - stat.log = o.read + # XXX: Tenemos que leer línea por línea porque en salidas largas + # se cuelga la IO + # TODO: Enviar a un websocket para ver el proceso en vivo? + o.each do |line| + lines << line + end end end time_stop - stat.seconds = time_spent_in_seconds - stat.bytes = size - stat.save + build_stats.create action: readable_cmd(cmd), + log: lines.join, + seconds: time_spent_in_seconds, + bytes: size, + status: r&.success? r&.success? end - # rubocop:enable Metrics/MethodLength + + private + + # @param [String] + # @return [String] + def readable_cmd(cmd) + cmd.split(' -', 2).first.tr(' ', '_') + end end diff --git a/app/models/deploy_local.rb b/app/models/deploy_local.rb index 7285c73..b6d1ef3 100644 --- a/app/models/deploy_local.rb +++ b/app/models/deploy_local.rb @@ -57,8 +57,6 @@ class DeployLocal < Deploy { 'HOME' => home_dir, 'PATH' => paths.join(':'), - 'SPREE_API_KEY' => site.tienda_api_key, - 'SPREE_URL' => site.tienda_url, 'AIRBRAKE_PROJECT_ID' => site.id.to_s, 'AIRBRAKE_PROJECT_KEY' => site.airbrake_api_key, 'JEKYLL_ENV' => Rails.env, @@ -94,7 +92,7 @@ class DeployLocal < Deploy end def jekyll_build - run %(bundle exec jekyll build --trace --destination "#{escaped_destination}") + run %(bundle exec jekyll build --trace --profile --destination "#{escaped_destination}") end # no debería haber espacios ni caracteres especiales, pero por si diff --git a/db/migrate/20210414152728_add_status_to_build_stats.rb b/db/migrate/20210414152728_add_status_to_build_stats.rb new file mode 100644 index 0000000..6b0c8f9 --- /dev/null +++ b/db/migrate/20210414152728_add_status_to_build_stats.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Registra el estado de las compilaciones +class AddStatusToBuildStats < ActiveRecord::Migration[6.1] + def change + add_column :build_stats, :status, :boolean, default: false + end +end