0

I'm using a library that doesn't exit gracefully when receiving signals like INT or TERM. I would like to trap them and exit gracefully. Is is possible to monkey patch signal trapping into an external module? Signal trapping for my code works fine, but doesn't do any good when calling INT while code from the module is active.

tadman
  • 208,517
  • 23
  • 234
  • 262
Reuben
  • 53
  • 5

2 Answers2

0

Signals affect not objects or modules but processes, so there's no need to patch third-party modules to trap signals. Just trap them in your code.

Aetherus
  • 8,720
  • 1
  • 22
  • 36
0

Monkeypatching involves making modifications to a library's code by swapping out methods or jamming in more methods than it usually has into their namespace.

Signal handling is done on a process level, so it's not something you can monkeypatch. What you can do is add the requisite signal handlers and get the code to respond properly, or do whatever shutdown behaviour you want it to do.

For example:

Signal.trap("INT") do
  BustedLibrary.shutdown!
  exit(0)
end

You might also see the Interrupt exception at the top level, another thing you can catch and deal with.

tadman
  • 208,517
  • 23
  • 234
  • 262