Persisting thread in delayed_job - ruby-on-rails

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.

Related

How to retrieve emails from multiple emails addresses using pop3 of gem mail in Rails?

in my project I will have to write different cron jobs to read emails from different email addresses to do different tasks. I am using mail gem but the problem is, retriever_method is singleton. So when I'll mention the new email address and new password the previous settings of retriever_method will be changed. So, I am not able to retrieve the emails when cron jobs are running at the same time.
Suppose, In my first cron job I have something like the following settings
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username1>',
:password => '<password1>',
:enable_ssl => true
end
In my second cron job, If I use something like
:user_name => '<username2>',
:password => '<password1>'
In that case both will be changed to username2
is there any workaround.
Or any other suggestion to do this job. I don't want to IMAP for some other reason.
Any suggestion will be appreciated.
Mail retrieve_method is singleton. so I am using Net::IMAP directly with the Mail to retrieve the attachment. One can use Net::Pop3 with Mail as well to resolve this issue.

How to subscribe a Pubnub channel asynchronous?

I want my rails app to subscribe a global channel, so everything happens on client, then client will publish into that channel. I want it to be asynchronous because there will be a lot of messages via that channels, and I want it to run along with Rails process. Currently, I can't get it to work, I put it initializers/pubnub.rb:
$pubnub.subscribe(channel: 'global', callback: ->(envelop) { do_something })
I don't get any incoming messages.
Just an usage example in my test app:
$pubnub = Pubnub.new(
:subscribe_key => "demo",
:publish_key => "demo",
:heartbeat => 10,
:logger => Rails.logger
)
ActiveRecord::Base.establish_connection
$pubnub.subscribe(:channel => :demo){|e| Message.create(:content => e.msg)}
It works just fine. What code You are trying to run in the callback?

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
}

Chaining Webrick Proxies

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}/")

How often do initializers run in Rails?

Do the initializers in a Rails app run each time someone visits the site?
For example, if my server is started in Texas at 10 a.m. , and someone visits my site from New York at 1 p.m. and someone visits from Los Angeles at 10 p.m, do the the initializers in a rails application run when the people from New York and Los Angeles visit, or do the initializers only run once I start the server in Texas?
The reason I'm asking is because I was using a case expression in an initializer file to change email settings depending on the time of day that app is visited. This would only make sense of course if the initializers ran when someone visited the site. If they ran only when the server was started then it would only be one case...
If that's not the right place to do it, or if the initializers only run once the server is started in Texas (for example) then where would you put this code?
case
when Time.now.hour == 0
ActionMailer::Base.smtp_settings = {
:user_name => "blahblah#example.com",
:password => "blahblah",
:address => "smtp.examplel.com",
:port => 25,
:tls => true
}
when Time.now.hour == 1
ActionMailer::Base.smtp_settings = {
:user_name => "blah#example.com",
:password => "eatshit",
:address => "smtp.example.com",
:port => 25,
:tls => true
}
end
A very simple and straight answer is: Just once, when your server kicks up.
You may be intrested in this article The Rails Initialization Process
Initializers get loaded whenever you start up passenger / mongrel or whatever you are using.
To set these settings at runtime take a look at Rails: Runtime configuration of ActionMailer?

Resources