ActiveRecord :: StatementInvalid when the process receives SIGTERM?

In my Rails application, I have a script that updates some records in the database. When I send SIGTERM to kill a script, it sometimes receives this signal while ActiveRecord executes the request. This raises an ActiveRecord :: StatementInvalid exception.

I would like to catch the StatementInvalid exceptions that occur when they are the result of SIGTERM and exit the script. How can I say that StatementInvalid is due to a signal, and not for some other reason?

+3
source share
3 answers

TERM, , . script ( , ).

 Signal.trap("TERM") do
   Kernel.exit!
 end

, StatementInvalid, - Ruby , SIGTERM . ActiveRecord StatementInvalid. , Ruby .

. Ruby Signal.

+5

: "script" Rails (script/runner ?), , , " " ""? , /thread/fiber/... , " "? , "" , - ( SIGTERM; -)).

0

OP, - .

time_to_die=false

# Prevent abrupt stopping of the daemon.
Signal.trap("TERM") { time_to_die=true; "SIG_IGN" }   

loop {
  .
  .
  exit_gracefully if time_to_die
  .
  .
}

def exit_gracefully
   #Cleaning up..
   $log.log "#{Time.now} TERM signal received. Exiting.."
   $db.close
   exit
 end
0

Source: https://habr.com/ru/post/1703208/


All Articles