Module: ExisRay::TaskMonitor
- Defined in:
- lib/exis_ray/task_monitor.rb
Overview
Wrapper para monitorear tareas en segundo plano (Rake/Cron) o scripts aislados.
Esta clase inicializa un contexto de trazabilidad simulado (ya que no hay una petición HTTP entrante) generando un nuevo ‘Root ID`. Luego, configura el reporte de errores y el contexto global antes de ejecutar el bloque provisto.
Class Method Summary collapse
-
.run(task_name) { ... } ⇒ void
Ejecuta un bloque de código dentro de un contexto monitoreado por ExisRay.
Class Method Details
.run(task_name) { ... } ⇒ void
This method returns an undefined value.
Ejecuta un bloque de código dentro de un contexto monitoreado por ExisRay.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 |
# File 'lib/exis_ray/task_monitor.rb', line 16 def self.run(task_name, &block) setup_tracer(task_name) short_name = task_name.to_s.split(":").last # Configurar Reporter (Sentry u otro) if (rep = ExisRay.reporter_class) && rep.respond_to?(:transaction_name=) rep.transaction_name = short_name rep.(service: :cron, task: short_name) if rep.respond_to?(:add_tags) end # Configurar Current Attributes if (curr = ExisRay.current_class) && curr.respond_to?(:correlation_id=) curr.correlation_id = ExisRay::Tracer.correlation_id end log_event(:info, "component=exis_ray event=task_started task=#{task_name} outcome=started") # Bloque de ejecución con o sin tags dependiendo de la configuración (&block) duration_s = ExisRay::Tracer.current_duration_s human_time = ExisRay::Tracer.format_duration(duration_s) log_event(:info, "component=exis_ray event=task_finished task=#{task_name} " \ "outcome=success duration_s=#{duration_s} duration_human=\"#{human_time}\"") rescue StandardError => e duration_s = ExisRay::Tracer.current_duration_s human_time = ExisRay::Tracer.format_duration(duration_s) log_event(:error, "component=exis_ray event=task_finished task=#{task_name} " \ "outcome=failed duration_s=#{duration_s} duration_human=\"#{human_time}\" " \ "error_class=#{e.class} error_message=#{e..inspect} " \ "exception.type=#{e.class} exception.message=#{e..inspect} " \ "exception.stacktrace=#{format_stacktrace(e.backtrace)}") raise e ensure # Limpieza centralizada obligatoria para evitar filtraciones de memoria o contexto ExisRay::Tracer.reset current = ExisRay.current_class reporter = ExisRay.reporter_class current.reset if current.respond_to?(:reset) reporter.reset if reporter.respond_to?(:reset) end |