namespace :consumer do task start: :environment do service_name = 'BetterMail::Consumer' # Create a new server instance for listening at localhost interfaces 127.0.0.1:2525 # and accepting a maximum of 4 simultaneous connections per default service = Bettermail::Consumer.new # save flag for Ctrl-C pressed flag_status_ctrl_c_pressed = false # try to gracefully shutdown on Ctrl-C trap('INT') do # print an empty line right after ^C puts # notify flag about Ctrl-C was pressed flag_status_ctrl_c_pressed = true # signal exit to app exit 0 end # Output for debug puts("Starting #{service_name}") # setup exit code at_exit do # check to shutdown connection if service # Output for debug puts('Ctrl-C interrupted, exit now...') if flag_status_ctrl_c_pressed # info about shutdown puts("Shutdown #{service_name}...") # stop all threads and connections gracefully service.stop end # Output for debug puts "#{service_name} stopped!" end # Start the server service.start # Run on server forever service.join end end