From 0a4115629b7e94053df35778cbe7fa7db8f5d59b Mon Sep 17 00:00:00 2001
From: Gerry Power <gerry.power@homestars.com>
Date: Thu, 1 Feb 2024 15:10:22 -0500
Subject: [PATCH] Contain the logic to a class

---
 lib/que/command_line_interface.rb | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/lib/que/command_line_interface.rb b/lib/que/command_line_interface.rb
index 5a60014c..d6e8fd4f 100644
--- a/lib/que/command_line_interface.rb
+++ b/lib/que/command_line_interface.rb
@@ -9,6 +9,23 @@ module CommandLineInterface
     # Have a sensible default require file for Rails.
     RAILS_ENVIRONMENT_FILE = './config/environment.rb'
 
+    class ThreadDumper
+      def self.call
+        thread_details = "Dumping #{Thread.list.count} threads traces:\n"
+        Thread.list.each do |t|
+          thread_details += "\n\n## Thread TID-#{t.object_id.to_s(36)} #{t['label']} \n"
+          if t.backtrace
+            thread_details += t.backtrace.join("\n")
+          else
+            thread_details += "\tThread has no backtrace available\n"
+          end
+        end
+
+        STDOUT.puts thread_details
+        IO.write("#{Dir.pwd}/thread_dump.txt", thread_details)
+      end
+    end
+
     class << self
       # Need to rely on dependency injection a bit to make this method cleanly
       # testable :/
@@ -245,19 +262,7 @@ def parse(
         # It's a bit sloppy to use a global for this when a local variable would
         # do, but we want to stop the locker from the CLI specs, so...
         $stop_que_executable = false
-        %w[INT TERM].each { |signal| trap(signal) { $stop_que_executable = true } }
-
-        trap("TTIN") do
-          puts 'Dumping thread traces:'
-          Thread.list.each do |t|
-            puts "## Thread TID-#{t.object_id.to_s(36)} #{t['label']}"
-            if t.backtrace
-              puts t.backtrace.join("\n")
-            else
-              puts "\tThread has no backtrace available"
-            end
-          end
-        end
+        %w[INT TERM].each { |signal| trap(signal) { ThreadDumper.call; $stop_que_executable = true } }
 
         output.puts(
           <<~STARTUP