basic proxy and consumer done

This commit is contained in:
Mike Sutton
2022-11-15 01:03:15 +01:00
parent 14e163a1a5
commit d3be685b08
15 changed files with 334 additions and 45 deletions

1
consumer/.ruby-gemset Normal file
View File

@ -0,0 +1 @@
bettermail_consumer

1
consumer/.ruby-version Normal file
View File

@ -0,0 +1 @@
ruby-2.7.2

View File

@ -1,15 +1,6 @@
source 'https://rubygems.org'
ruby '2.6.5'
gem 'dotenv-rails'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development do
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
ruby '2.7.2'
gem 'dotenv'
gem 'bunny'
gem 'mail'
gem 'pg'

32
consumer/Gemfile.lock Normal file
View File

@ -0,0 +1,32 @@
GEM
remote: https://rubygems.org/
specs:
amq-protocol (2.3.2)
bunny (2.19.0)
amq-protocol (~> 2.3, >= 2.3.1)
sorted_set (~> 1, >= 1.0.2)
dotenv (2.8.1)
mail (2.7.1)
mini_mime (>= 0.1.1)
mini_mime (1.1.2)
pg (1.4.4)
rbtree (0.4.5)
set (1.0.3)
sorted_set (1.0.3)
rbtree
set (~> 1.0)
PLATFORMS
ruby
DEPENDENCIES
bunny
dotenv
mail
pg
RUBY VERSION
ruby 2.7.2p137
BUNDLED WITH
2.1.4

View File

@ -0,0 +1,88 @@
# frozen_string_literal: true
require 'dotenv/load'
require 'pg'
require 'mail'
require 'bunny'
# Server class
class BettermailConsumer
def initialize
@pg = PG::Connection.new(ENV['DATABASE_URL'])
end
def start
begin
puts ' [*] Waiting for messages. To exit press CTRL+C'
mail_queue = bunny_channel.queue(ENV['RABBIT_MAIL_QUEUE'])
mail_queue.subscribe(block: true) do |_delivery_info, _properties, body|
mail = Mail.read_from_string(body)
puts mail
end
sns_queue = bunny_channel.queue(ENV['RABBIT_SNS_QUEUE'])
sns_queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " [x] Received #{_delivery_info}"
end
rescue Interrupt => _
stop
end
end
def stop
@bunny_conn&.close
@pg&.close
end
def bunny_channel
if @bunny_channel.nil?
@bunny_conn = Bunny.new
@bunny_conn.start
@bunny_channel = @bunny_conn.create_channel
end
@bunny_channel
end
end
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 = BettermailConsumer.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

1
consumer/start.sh Executable file
View File

@ -0,0 +1 @@
ruby ./bettermail_consumer.rb