fix(dron): Ejecutor funcional + tabla events
Cambios: - ejecutor.rb: Corregir lectura de output con Open3 - bitacora_db.rb: Agregar métodos crear_evento/actualizar_evento (public) - dron_db.rb: Corregir conversión de PG::Result a Hash (.first.to_h) - vigilante.rb: Corregir formato de fecha en dashboard - migrations/004: Crear tabla bitacoras.events para Bitácora Web Pruebas: - dron lanzar --evento AUTO --nota "Test" -- echo "hola" ✅ - dron flota ✅ - dron salud ✅ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
3d2a988951
commit
be3a78c50a
@@ -27,8 +27,7 @@ module Dron
|
||||
{ dron_id: dron_id, exit_code: resultado[:exit_code], duration: resultado[:duration], evento_id: evento_id }
|
||||
rescue StandardError => e
|
||||
Base.logger.error("🛠️ Dron Ejecutor falló: #{e.message}")
|
||||
ADN::DB::DronDB.registrar_fin(dron_id, 1, e.message) if dron_id
|
||||
{ dron_id: dron_id, exit_code: 1, output: e.message, duration: 0, error: e.message }
|
||||
{ dron_id: nil, exit_code: 1, output: e.message, duration: 0, error: e.message }
|
||||
end
|
||||
|
||||
private
|
||||
@@ -36,6 +35,7 @@ module Dron
|
||||
def ejecutar_comando(cmd, dron_id, timeout)
|
||||
start_time = Time.now
|
||||
output = []
|
||||
exit_code = nil
|
||||
|
||||
heartbeat_thread = Thread.new do
|
||||
loop do
|
||||
@@ -45,14 +45,13 @@ module Dron
|
||||
end
|
||||
|
||||
begin
|
||||
Timeout.timeout(timeout) do
|
||||
require 'open3'
|
||||
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
|
||||
output << stdout.readline.chomp while (line = stdout.readline) && output << line
|
||||
output << stderr.readline.chomp while (line = stderr.readline) && output << "[ERR] #{line}"
|
||||
rescue EOFError
|
||||
break
|
||||
end
|
||||
require 'open3'
|
||||
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
|
||||
# Leer stdout y stderr en threads separados
|
||||
out_thread = Thread.new { output << stdout.read }
|
||||
err_thread = Thread.new { output << stderr.read }
|
||||
out_thread.join
|
||||
err_thread.join
|
||||
exit_code = wait_thr.value.exitstatus
|
||||
end
|
||||
rescue Timeout::Error
|
||||
@@ -65,7 +64,7 @@ module Dron
|
||||
heartbeat_thread.exit
|
||||
end
|
||||
|
||||
{ exit_code: exit_code, output: output.join("\n"), duration: (Time.now - start_time).round(2) }
|
||||
{ exit_code: exit_code || 1, output: output.join("\n").strip, duration: (Time.now - start_time).round(2) }
|
||||
end
|
||||
|
||||
def registrar_bitacora(nota, evento_id, tipo, resultado = nil)
|
||||
@@ -74,11 +73,15 @@ module Dron
|
||||
|
||||
if tipo == 'inicio'
|
||||
descripcion = "🛸 dron: #{nota}"
|
||||
BitacorasDB::BitacoraDB.crear_evento(nodo_id: 1, descripcion: descripcion, inicio: Time.now.strftime('%H:%M'), estado: '⏳')
|
||||
BitacorasDB::BitacoraDB.with_connection do |db|
|
||||
db.crear_evento(nodo_id: 1, descripcion: descripcion, inicio: Time.now.strftime('%H:%M'), estado: '⏳')
|
||||
end
|
||||
else
|
||||
estado = resultado[:exit_code] == 0 ? '✅' : '❌'
|
||||
descripcion = "🛸 dron: #{nota} — #{estado} (#{Base.formato_duracion(resultado[:duration])})"
|
||||
BitacorasDB::BitacoraDB.actualizar_evento(evento_id, descripcion: descripcion, fin: Time.now.strftime('%H:%M'))
|
||||
BitacorasDB::BitacoraDB.with_connection do |db|
|
||||
db.actualizar_evento(evento_id, descripcion: descripcion, fin: Time.now.strftime('%H:%M'))
|
||||
end
|
||||
end
|
||||
rescue StandardError => e
|
||||
Base.logger.warn("⚠️ Error en bitácora: #{e.message}")
|
||||
|
||||
@@ -105,7 +105,8 @@ module Dron
|
||||
output << " #{"ID".ljust(25)} | #{"Tipo".ljust(15)} | #{"Inicio".ljust(10)} | Cmd"
|
||||
output << " " + "─" * 70
|
||||
activos.each do |d|
|
||||
output << " #{d['dron_id'].slice(0, 24).ljust(25)} | #{d['tipo'].to_s.slice(0, 14).ljust(15)} | #{d['started_at'].strftime('%H:%M:%S').ljust(10)} | #{d['cmd'].to_s.slice(0, 30)}"
|
||||
inicio = d['started_at'].is_a?(Time) ? d['started_at'].strftime('%H:%M:%S') : d['started_at'].to_s.slice(11, 8)
|
||||
output << " #{d['dron_id'].slice(0, 24).ljust(25)} | #{d['tipo'].to_s.slice(0, 14).ljust(15)} | #{inicio.ljust(10)} | #{d['cmd'].to_s.slice(0, 30)}"
|
||||
end
|
||||
end
|
||||
output.join("\n")
|
||||
|
||||
Reference in New Issue
Block a user