How to Use Resque Web alongside a Sinatra app - ruby-on-rails

I’ve got a Sinatra web app. I’m using Resque and Resque Scheduler, and now I’m looking into adding Resque Web to (hopefully) see what my Resque queue looks like. Now here’s my problem: the official Resque Web is a Rails app. I don’t know how to use a Rails app inside of Sinatra, or if it’s even possible.
So my question: What’s the best way to implement Resque Web into my Sinatra app? Can I use the rails app inside of Sinatra? I saw one person say that you should have a separate part of your app running Rails, but that seems really nasty. Is there a better way to do it?
Thanks!

I've not used the ResqueWeb, but Rails and Sinatra are both Rack compliant frameworks, so they should be able to run each other or alongside.
http://www.sinatrarb.com/intro.html#Rack%20Middleware
## my-amazing-app.rb
use MySlightlyLessAmazingRailsApp
# rest of Sinatra stuff…
or
# config.ru
map "/" do
# easiest to mount Sinatra apps this way if using the modular style
run MyAmazingSinatraApp
end
map "/resque" do
run MySlightlyLessAmazingRailsApp
end
I don't know how you'd do this with Rails, perhaps try this link http://m.onkey.org/rails-meets-sinatra or perhaps this:
RailsApp::Application.routes.draw do
mount MyAmazingApp, :at => "/more-amazed"
# rest of the rails routes
end

Here’s what I was looking for: http://asciicasts.com/episodes/271-resque
Resque-web can be stand alone, run from the command line. So I’m just starting it up with a shell command whenever I spin up an instance of my server. Not exactly what I was looking for, but it does the trick!
Thanks for the suggestions, all.

Related

Preventing Rails from hanging while doing ssh?

I have a compile button in my Rails app which does
get_pdf_cmd = ['ssh', '-i', '~/.ssh/id_rsa', '-o', 'StrictHostKeyChecking=no', 'root#compile', '/bin/bash', '--login', '/compile.sh', container['host'] ]
Rails.logger.info(get_pdf_cmd)
stdin, stdout_and_stderr, wait_thr = Open3.popen2e({}, get_pdf_cmd.join(" "))
Rails.logger.info stdout_and_stderr.gets(nil)
stdout_and_stderr.close
stdin.close
exit_code = wait_thr.value
and while this happens the entire Rails app hangs and is not responding at all.
Question
How can I prevent Rails from hanging while the SSH command is running?
as pointed out already in the comments, the best practice would be to use a background process to do the heavy lifting.
rails provides an abstraction-layer on top of the many job frameworks available for rails. read more on this topic in the guides http://guides.rubyonrails.org/active_job_basics.html
if your rails application depends on the result of such an operation, you need to implement some kind of polling or use modern client communication style like ActionCable http://guides.rubyonrails.org/action_cable_overview.html
fork is also possible, it's not recommended in a running rails application though.

Sidekiq Forbidden error with Redis sessions

Seeing a Forbidden error when we attempt to retry / delete / etc from the sidekiq UI.
Read through a lot of the web traffic on people who experienced this issue, including: https://github.com/mperham/sidekiq/issues/1289
Running on
rails 4.1.11
ruby 2.2.3
sidekiq-pro 3.0.1
redis 2.8.21
puma
Heroku
redis-rails for storing sessions
relevant snippets from our app: https://gist.github.com/toddmetheny/4b511e364a4c91ad8187
We're using redis to store sessions. It seemed like a fair number of people having the problem were doing that but there were various problems sharing sessions w/ the sidekiq UI.
Tried most of the things I've seen suggested in the related closed issues without any luck. As described by others, it works well in development...and staging (which seems really weird). Was hoping someone might have something to add. Thanks in advance.
Read the comments just posted here:
https://github.com/mperham/sidekiq/issues/2487#issuecomment-179667037
Run bundle update sinatra to make sure you have the latest Sinatra and Rack stuff.
To properly share the session between Sinatra and Rails, you need to do something like this:
Sidekiq::Web.set :session_secret, Rails.application.secrets[:secret_token]
Sidekiq::Web.set :sessions, Rails.application.config.session_options

how do I set up RPush in rails?

I'm a bit confused on how to do some of the config for Rpush when starting from the Rails environment. If I just want to Rpush.embed and run within this same process, where should I call Rpush.embed? Does this go in the initializer? I am also unclear on the best place to set up my apps. I see there is a nice feature for signaling rpush to re-read from the configured app, but where do I configure the app itself? I see that I can do this with active model calls, but I obviously don't want to create a new app everytime I start the rack (or rails).
Create apps using database migration, put Rpush.embed in config.ru

Multiple Heroku apps on a single domain

I'm pretty sure the answer to this is "not possible", but I thought I would check one last time:
I'm migrating some Rails apps to Heroku. The way they are organized now, URL-wise, is:
http://example.com/site1 -- is served by app1
http://example.com/site2 -- is served by app2
Everything I've read so far says this isn't possible on Heroku: that each application must have its own subdomain (e.g. site1.example.com, site2.example.com).
My client does not want to change the URL structure (and actually there may be some argument for that; I've read several sources that say that paths vs. subdomains is better for SEO).
Am I correct that this is not possible on Heroku?
This IS possible with Heroku and Rails using Rack Reverse Proxy. I'm currently doing it to run a Wordpress blog and knowledge base at /blog and /kb respectively. For SEO purposes I did not want them on a subdomain.
Rails configuration
Add to Gemfile:
gem "rack-reverse-proxy", :require => "rack/reverse_proxy"
Now we want /blog and /blog/ to be directed to the Wordpress instance from the Rails app.
Add this to your config.ru right before you run the app:
use Rack::ReverseProxy do
reverse_proxy(/^\/blog(\/.*)$/,
'http://CHANGEME.herokuapp.com$1',
opts = {:preserve_host => true})
end
In config/routes.rb add a route:
match "/blog" => redirect("/blog/"), via: :all
Original Ref: http://rywalker.com/setting-up-a-wordpress-blog-on-heroku-as-a-subdirectory-of-a-rails-app-also-hosted-on-heroku
I’m probably late to the party, but it is possible. Short version of how we did it at Pilot:
We have an additional instance running HAProxy. It’s responsible for routing requests to corresponding Heroku apps based on their URL. ✌️
Your best bet is to turn your Rails apps into Rails engines:
http://pivotallabs.com/migrating-from-a-single-rails-app-to-a-suite-of-rails-engines/
And then mount each engine with the URLs you describe.
Of course, this is probably going to be tricky if your apps are very large and complex.

em-websocket gem with Ruby on Rails

I started developing a web-socket based game using the em-websocket gem.
To test the application I start the server by running
$> ruby server.rb
and then I just open two browsers going directly to the html file (no web server) and start playing.
But now I want to add a web server, some database tables, an other Ruby on Rails based gems.
How an achieve communication between my web-socket server and my Ruby on Rails application? Should they run in the same server and run as a single process? Run in separate servers and communicate through AJAX?
I need to support authentication and other features like updating the database when a game is finished, etc.
Thanks in advance.
There is an issue created about this:
https://github.com/igrigorik/em-websocket/issues/21
Here is the deal. I also wanted to develop a websocket server client with ruby on rails framework. However ruby-on-rails is not very friendly with eventmachine. I have struggeled with having a websocket client, so I managed to copy/cut/paste with from existing lib, and end up with the following two escessential ones.
Em-Websocket server
https://gist.github.com/ffaf2a8046b795d94ba0
ROR friendly websocket client
https://gist.github.com/2416740
have the server code in script directory, the start like the following in ruby code.
# Spawn a new process and run the rake command
pid = Process.spawn("ruby", "web_socket_server.rb",
"--loglevel=debug", "--logfile=#{Rails.root}/log/websocket.log",
:chdir=>"#{Rails.root}/script") #,
:out => 'dev/null', :err => 'dev/null'
Process.detach pid # Detach the spawned process
Then your client can be used like this
ws = WebSocketClient.new("ws://127.0.0.1:8099/import")
Thread.new() do
while data = ws.receive()
if data =~ /cancel/
ws.send("Cancelling..")
exit
end
end
end
ws.close
I wish there is a good ROR friendly em-websocket client, but couldn't fine one yet.
Once you made server/client works well, auth. and database support must not be very different from other rails code. (I mean having client side with some auth/db restrictions)
I am working on a gem that may be helpful with your current use case. The gem is called websocket-rails and has been designed from the ground up to make using WebSockets inside of a Rails application drop dead simple. It is now at a stable release.
Please let me know if you find this helpful or have any thoughts on where it may be lacking.

Resources