Tell Webrick to load everything / restart on next request (something like restart.txt) - passenger

Is there a way to tell Webrick to reload everything on the next request? Something like tmp/restart.txt for passenger?
I love guard and would like it to restart my development server whenever a critical file changes. I accomplished to stop the server using guard-shell:
guard :shell do
watch %r{\.ruby-version|Gemfile|Gemfile\.lock|config/(application|environment)\.rb|config/environments/.*\.rb|config/initializers/.*\.rb} do |m|
`lsof -i tcp:3000 | awk 'NR!=1 {print $2}' | xargs kill -9`
n "#{m[0]} saved, restart of development server needed", 'Webrick restart'
end
end
But it would be great to have it restarted automatically.
I also tried other options like pow in combination with powder which automatically reloads pow successfully, and also phusion passenger with a guard-shell script that touches tmp/restart.txt, which also works.
BUT: I want to use pry from within the server output, so I can simply add a binding.pry somewhere in my code and debug it in the console, and whether pow nor passenger seem to allow this: while pow disregards any binding.pry and does not halt, passenger seems to halt, but the interaction with pry is very strange, not every keystroke seems to reach it, etc.
So webrick is still my favorite and works very well... if I could just make it restart somehow automatically...

I found another way to do this. Just create a bin/server executable file in your Rails project with the following content:
#!/bin/bash
# This starts webrick using rerun which automatically restarts it after critical config files are changed
rerun --pattern '{Gemfile.lock,config/environment.rb,config/environments/development.rb,config/initializers/*.rb,lib/**/*.rb}' --no-growl --signal INT --background --clear -- rails s
Then install the rerun gem: gem install rerun.
Now simply start webrick by typing server (instead of rails server) and rerun will take care of restarting the server whenever an important file is changed.

Related

How to run app via Ruby shell command, when same command works on command line

I have a simple Ruby (Sinatra) server that starts up without issue from the command line with ruby app.rb. But when I execute the same command via my command line app, either with `ruby app.rb` or with system("ruby app.rb"), I get this error:
app.rb:1:in `require': cannot load such file -- sinatra (LoadError)
from app.rb:1:in `<main>'
The opening line of app.rb is simply require 'sinatra'. The Sinatra gem is installed in my system, of course; I don't understand why the interpreter is acting as if it's not there.
While troubleshooting, I decided to add Sinatra to the Gemfile of the command line app that is calling app.rb. Lo and behold, now that the parent process has access to Sinatra, now it works (i.e., system(app.rb) successfully starts the Sinatra server). But when I exit the command line app, a Sinatra server is always there, saying:
[2018-12-18 23:17:37] INFO WEBrick 1.3.1
[2018-12-18 23:17:37] INFO ruby 2.4.0 (2016-12-24) [x86_64-linux]
== Sinatra (v2.0.4) has taken the stage on 4567 for development with backup from WEBrick
[2018-12-18 23:17:37] INFO WEBrick::HTTPServer#start: pid=27384 port=4567
So I have to Ctrl-c to exit the command line app.
Question: Is there a way to spawn an independent Sinatra process/server, as I was trying to do with system("ruby app.rb"), without installing it in the parent app (the command line app)? I also tried using Process.fork followed by Process.wait, but that didn't help.
Self-answered, since I found a solution, and nobody else has answered it:
Instead of executing just ruby app.rb, which by itself runs in the same environment as the surrounding program and hence uses the same Gemfile (ignoring the one referred to in the spawned script), execute:
system("BUNDLE_GEMFILE='./Gemfile' && ruby app.rb")
That's all there was to it! This tells Ruby to load the correct gemfile when it runs the program.
Personal note: Many thanks to all of you in the discussion, above, on the question, as well as the local Ruby group, my best friend, and my 12-year-old coder son (who actually supplied the exact code). Now I just have to figure out how to stop the spawned process without stopping the surrounding program. Can't just use "Ctrl-C." But that's a different problem!

How to make Puma reload everytime there are changes on .rb?

I use thin before for my sinatra app, and using rerun to automatically reload server when there are changes on .rb
rerun --pattern="**/*.{rb}" ruby ./main_service.rb
how to make puma do the same? since this command does not work:
rerun --pattern="**/*.{rb}" puma -b tcp://127.0.0.1:12345
that shows:
ERROR: No application configured, nothing to run
my config.ru:
require './main_service'
run Sinatra::Application
but when i use manual command puma -b tcp://127.0.0.1:12345 it works normally..
ah nevermind, i found the solution:
rerun --pattern="**/*.{rb}" "puma -b tcp://127.0.0.1:12345 ./config.ru"
the second rerun parameter must be quoted

How do you get LiveReload to work with Octopress using Guard

I am using this guide to add liveReload to Octopress.
http://www.erikzaadi.com/2012/09/16/using-live-reload-with-octopress/
Once I get to 'rake generate && rake watch',
The command line spits out I am missing a specific gem; i.e.'rake-0.9.6', which is strange because I thought by installing the bundle took care of that.
I suspect that bundle is being saved elsewhere; but shouldn't that bundle be saved in the directory of my choosing (i.e. Sites/myproject?)
Add these two entries to your Gemfile, in the :development group:
gem 'guard'
gem 'guard-livereload'
Create a file called Guardfile containing something like:
guard 'livereload' do
watch(%r{public/generated})
watch(%r{public/.+\.(css|js|html)})
end
Start 2 shell tabs running these commands: rake generate && rake watch and guard
`
rake generate && rake watch
`
start guard LiveReload
`
guard
`
It’s neat to get LiveReload working with Octopress. However, the generation can finish after your page does a reload, so you won’t see your latest changes. I’ll update this blog post when I figure out a solution to that one. Until then, you may find it more convenient to manually refresh the blog page yourself.
It’s worth noting that if you’re running any other instance of guard- LiveReload, then one of these two copies will win and one won’t work. If you run a rails server this way, then this can bite you. It took me a bit of time to figure out why guard wasn’t working.
source
http://www.railsonmaui.com/blog/2013/04/27/octopress-setup-with-github-and-org-mode/#sec-4

how to use RVM to a ruby app engine?

i wanna make a simple ruby App Engine in rails just like heroku, i'm dealing with a problem now.
My idea was:
1.use rails to establish the App Engine, use a class 'App' to handle all apps.
2.when a user create an ruby app he should offer it's git path
3.when the user deploys it, my app engine will do these things:
clone the git to a path in my server (done
use RVM to designatine the ruby version witch user wanted and make a gemdir for the project (some problems here
create a nginx conf for the project, then include it and reload nginx (i can do it
Problems in the second step:
codes here:
def start_thin
Dir.chdir(proj_path) do
system('rvm use ruby-1.8.7-p352#testname --create')
system('gem env gemdir')
success = system ('thin start -s3 --socket ' + self.proj_sock)
if success
return true
end
end
return false
end
when the code runs here, the log told me "RVM is not a function...blahblah", i know something about the login-shell and non-login-shell, then i try to fix it via editing .bashrc but same problem occurred.
And if i ignore it, the app can't be deployed, because of a Load Error :
myapp.rb:2:in `require': cannot load such file -- sinatra (LoadError)
if i open a terminal in that app directory, i can use thin to start it.
i wanna know how to run cmd just like in a terminal, without all these odd problem?
or how to edit my method to fix it?
Thanks!
Thanks Casper and GhostRider.
The user and rvm settings are correct.
After lots of googles and tests i found it's impossible...
Finally I fixed it by using RVM's ruby api instead of running system command.
Such as :
require 'rvm'
env = RVM.current
env.gemset.create('app1')

Need help with starting RoR: Command "rails server" does not result in "Booting WEBrick"?

I need help to get started with RoR.
I currently follow this guideline:
http://allaboutruby.wordpress.com/2009/07/20/installing-rails-on-windows-3-years-later/#comment-11099
I followed step 1 through 3 w/o problems.
In step 5: I can get the webserver through WEBrick working.
When i put
"rails server"
instead of getting "Booting Webrick", i get "rails new_path option"
thus when i try 127.0.0.1:3000 in the browser... it does work.
Can anyone guide me on this on how to get it up and runnning? (Im a total newb for now...so i need specific explanations! thanks!)
In your tutorial i can't see the command 'bundle install' - it's checking and installing all necessary gems in your system. So why you don't use another great rails tutorial - http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
I'm guessing you are running windows, on which rails can be a little awkward. You'll probably need to run the rails server command by pointing ruby at the server script. On windows, your rails "commmand" is actually just a .bat file that lives in the /bin file of your ruby installation, and that .bat file just passes the arguments to ruby. If you look at the rails gem that is installed on your machine, you'll see the files that correspond to the normal first argument of a rails command (console, generate, server, etc). You might find it helpful to copy these to the /script directory of your application, and when you want to run a rails command you can just run "ruby script\server" from your application's main directory, though there may be more accepted ways of getting the same result.

Resources