Chaining Webrick Proxies - ruby-on-rails

I have been trying to test the chaining of Webrick proxies, and I am having some trouble.
Each proxy works fine on its own from 127.0.0.1:port (when :ProxyURI is commented out for proxy_2), but I am getting the error:
ERROR unsupported method `GET'.
from proxy_2 output (httpproxy.rb) when I try chaining them.
To clarify, when I chain them I am using 127.0.0.1:8086 as my access point from another application.
Looking at the logs for proxy_1, it appears that it is not receiving any requests.
Any help would be much appreciated.
require 'webrick'
require 'webrick/httpproxy'
port_1 = 8085
port_2 = 8086
proxy_1 =
WEBrick::HTTPProxyServer.new(
:Port => port_1,
:ServerType => Thread,
:Logger => WEBrick::Log.new("./logs/#{port_1}.out"),
:ServerName => "future_authentication_proxy"
)
proxy_1.start
proxy_2 =
WEBrick::HTTPProxyServer.new(
:Port => port_2,
:ProxyURI => '127.0.0.1:'+port_1.to_s
)
trap("INT"){
proxy_1.shutdown
proxy_2.shutdown
}
proxy_2.start

You passed wrong ProxyURI option, it should be something like:
:ProxyURI => URI.parse("http://#{host_1_ip}:#{port_1}/")

Related

Using Sidekiq with Mailer to send asynchronous emails [RAILS]

So I am currently trying to use the Rails gem 'sidekiq' to send a large number of emails asynchronously, so it does not bog down the load time for the next page.
Everything I have works except for this:
FeedbackMailer.invite_contact(current_user, current_business, contact).deliver()
This is my own mailer and a set of paramters which it must take to create the email for the client.
The error that gets thrown by sidekiq is this:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
The default_url_options[:host] is set in the application.rb, but does not seem to get read here. I also tried setting only_path: true and got the same error still.
I can send emails just find when I am outside one of sidekiq's workers, the problem only exists there. Anyone know of a workaround so that emails can be sent from sidekiq's workers?
Also before you answer, using Mailer.delay doesnt really solve my problem, because there is alot of other code around the mailing that must go off during every iteration of the loop.
This error happens when your mail is not configured properly. Check that you have something like this in the relevant environment file (production.rb or staging.rb)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.something.com',
:port => 587,
:domain => 'yourdomain.com',
:user_name => 'account-admin#yourdomain.com',
:password => 'your secure password',
:authentication => 'plain',
:enable_starttls_auto => true
}

What causes a End Of File Error when Rails Mailer sends an email?

A Rails 3.2.8 application developed with a gmail account as the "sending address".
When mail sending works my environment.rb file contains this:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:user_name => "accountname",
:password => "123456789"
}
I get this message in my application log:
EOFError (end of file reached):
when the above code is changed to what is shown below:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "mail.company.com",
:port => 25,
:domain => "company.com",
:authentication => :login,
:user_name => "accountname",
:password => "123456789"
}
I can tell you I -am- able to send a manual email message to the email address
and see it arrive when using a email client such as ThunderBird, thus I know
the accountname#company.com is functional.
I don't understand how an end of file error comes into play.
I also cannot figure out how to get more information to appear in the log.
I look forward to reading a few suggestions of determining the cause of the End Of File.
Started POST "/sendInvites?locale=en&lot_id=18&user_id=17" for 99.99.99.99 at 2013-10-03 08:52:09 -0700
Processing by WaitingListsController#sendInvites as HTML
Parameters: {"authenticity_token"=>"uwz/6pW1rLPXR4gU3m3OwCmU0O3DSJ/haNM2/ai+OR8=", "locale"=>"en", "lot_id"=>"18", "user_id"=>"17"}
=======>>>> Beginning Send Invitation Process <<<<=======
=======>>>> just before the PassEmailer.requestApprovedWL IS called to send the invitation <<<<=======
>>>> Beginning ::: requestApprovedWL(user_info) <<<<=======
Rendered pass_emailer/requestApprovedWL.erb (0.9ms)
>>>> at the end of ::: requestApprovedWL(user_info) <<<<=======
Completed 500 Internal Server Error in 1718ms
EOFError (end of file reached):
app/controllers/waiting_lists_controller.rb:276:in `sendInvites'
For anyone still experiencing this issue, Whenever appears to default to Production. It is using your Production variables (or looking for them) while in your Development or Staging. Also, per the docs it does not load the Rails environment.
While you are in development or staging you must manually let schedule.rb know this. After finding the File.expand_path method here, the following is how I start my schedule.rb file:
require File.expand_path(File.dirname(__FILE__) + "/environment")
set :environment, Rails.env
set :output, Rails.root.join('log','cron.log')
This provides you the Rails environment and allows you to also set the path for logging.

Persisting thread in delayed_job

So I have a rails app where I want my delayed job process to communicate with an SMPP server. But the problem occurs when I try to send the messages. My thread that I created in an initializer (delayed_job.rb):
if $0.ends_with?('/delayed_job')
require_relative '../../lib/gateway'
config = {
:host => 'SERVER.COM',
:port => 2345,
:system_id => 'USERNAME',
:password => 'PASSWORD',
:system_type => '', # default given according to SMPP 3.4 Spec
:interface_version => 52,
:source_ton => 0,
:source_npi => 1,
:destination_ton => 1,
:destination_npi => 1,
:source_address_range => '',
:destination_address_range => '',
:enquire_link_delay_secs => 60
}
Thread.new{
gw = Gateway.new
gw.start(config)
}
end
But checking my log file for the smpp server, it seems that the thread dies right after it starts. So I guess my question is how to persist the thread while the delayed_job daemon is running?
If I start my rails app in production and I try to send messages individually, it works without a problem, but because delayed_job is a separate process, I can't communicate with the the smpp thread in the rails app from my workers in the delayed_job queues.
Any ideas?
Sorted, decided to separate everything into their own daemons and each would communicate with the database independently as opposed to trying to work with pipes and signals.

Exception Notification - How to Shut Off in Dev. Mode

I've been happily using exception notification up till now and never sent an email when running in development mode. Now, the gem seems to want to send me an email whenever an exception occurs -- particularly a routing error. Is there some config setting I'm missing? The dox seem to have dried up and blown away.
Assuming you have an initializer to set up your configuration, just wrap it in a conditional to check if it's in production mode:
if Rails.env.production?
Whatever::Application.config.middleware.use ExceptionNotifier,
:email_prefix => "[Whatever] ",
:sender_address => %{"notifier" <notifier#example.com>},
:exception_recipients => %w{exceptions#example.com}
end
Part 1: set up different configurations for ExceptionNotifier in your config/environments/environment_name.rb files. For example, in config/environments/development.rb, use something like this to send your notifications elsewhere, or to a black hole:
YourApp::Application.configure do
<other stuff>
config.middleware.use ExceptionNotifier,
:email_prefix => "[YourApp - DEVELOPMENT:#{`hostname`}] ",
:sender_address => %{"notifier" <notifier##{`hostname`}>},
:exception_recipients => %w{<some_bitbucket_email_address>}
end
This way, your ExceptionNotifier is still configured in all environments. If it isn't configured, any direct calls to
ExceptionNotifier::Notifier.background_exception_notification(e).deliver
will throw exceptions, which could be undesired behavior inside a rescue block.
Part 2: try setting this config parameter:
config.consider_all_requests_local = true
I picked that up from the reverse question to this: Exception notifier plugin not sending emails
Rails 3
Is better write the configuration in the production environment, look at my examle
APP::Application.configure do
...
..
.
config.middleware.use ExceptionNotifier,
:email_prefix => "[Error] ",
:sender_address => %{"Notificacion de error" <notifier#example.com>},
:exception_recipients => %w{addres1#email.com}

localhost on rails

I am sending an email that contains a link to my website. I want to be able to test it locally and be able to move the scripts around to different hosts easily.
In my email right now I use the following:
<%= url_for(:host => 'localhost:3000', :controller => "user_activations", :action => "show", :id=>#id, :confirm=>#passcode) %>
This works for when testing locally but will obviously fail for production. Is there an easy way to have rails (or ruby) detect what the current host is? I'm thinking something like $_SERVER of php.
I realize I can use some logic using my environment variable but I would like to avoid this.
Thanks
I define a constant 'HOST' in my environment.rb that sets my host. Alternatively you can use request.host or request.domain.
in environments/development.rb
config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 }
in environments/production.rb
config.action_mailer.default_url_options = { :host => "www.xyu.at" }
and use tests with rspec-email :)

Resources