namespace :proxy do task start: :environment do # 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::Proxy.new( ports: ENV['PROXY_PORTS'], hosts: ENV['PROXY_HOSTS'], max_processings: 10, auth_mode: :AUTH_REQUIRED, tls_mode: :TLS_REQUIRED, tls_cert_path: './ssl/cert.pem', tls_key_path: './ssl/key.pem' ) service_name = 'BetterMail::SMTPProxy' # 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 # 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