Class: ArchivesSpaceThreadDump

Inherits:
Object
  • Object
show all
Defined in:
common/archivesspace_thread_dump.rb

Class Method Summary collapse

Class Method Details

.init(status_file_path) ⇒ Object



12
13
14
15
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
# File 'common/archivesspace_thread_dump.rb', line 12

def self.init(status_file_path)
  $stderr.puts("\n#{self}: Touch the file '#{status_file_path}' to trigger a thread dump")

  Thread.new do
    begin
      watcher = java.nio.file.FileSystems.getDefault().newWatchService()

      status_file_path = File.absolute_path(status_file_path)
      dir = java.nio.file.Paths.get(File.dirname(status_file_path))

      dir.register(watcher,
                   java.nio.file.StandardWatchEventKinds::ENTRY_CREATE)

      loop do
        key = watcher.take

        key.poll_events.each do |event|
          # Cast both to Path objects to normalize between '/' and '\\' on win32
          if dir.resolve(event.context).to_string == java.nio.file.Paths.get(status_file_path).to_string
            begin
              ArchivesSpaceThreadDump.print_dump
            rescue
              $stderr.puts("Problem while printing thread dump: #{$!}")
            end

            File.unlink(status_file_path)
          end
        end

        unless key.reset
          raise "Key reset failed"
        end
      end
    rescue
      $stderr.puts "Failure in #{self} handler for path #{status_file_path}: #{$!}"
      $stderr.puts($@)
    end
  end
end


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'common/archivesspace_thread_dump.rb', line 52

def self.print_dump
  $stderr.puts("[#{Time.now.to_i}] Starting Ruby-level thread dump")
  $stderr.puts("=" * 72)

  Thread.list.each do |thread|
    $stderr.puts("\n")
    $stderr.puts(thread.inspect)
    thread.backtrace.each do |frame|
      $stderr.puts("  #{frame}")
    end
  end

  $stderr.puts("")

  $stderr.puts("[#{Time.now.to_i}] Starting JVM-level thread dump")
  $stderr.puts("=" * 72)

  java.lang.Thread.all_stack_traces.each do |thread, frames|
    $stderr.puts("\n")
    $stderr.puts("\"#{thread.name}\"")
    frames.each do |frame|
      $stderr.puts("  #{frame}")
    end
  end

  $stderr.puts("")
  $stderr.puts("==== End of thread dump ====")
end