Failing to open rails console and run a script - ruby-on-rails

I'm trying to open a Rails console and run a ruby script in it. The script works fine till the rails console opens and then nothing happens. Only when I exit the console manually, do I get an error message saying:
-bash: load: command not found
These are the commands I'm using:
source /etc/profile
source /etc/default/<my-app>
cd /usr/share/<my-app>
NO_AUTH=Y bundle exec ruby-flo -S rails console
load '/home/sirish.aditya/test_code.rb'
How do I load the script into the console?

You could use this
echo "$(ruby /home/sirish.aditya/test_code.rb)"
As per you requirement, you could even try rails runner or whenever gem.

It doesn't work like that. As soon as the rails console is invoked either manually or through a bash script, you are essentially running a separate process which has no visibility into the subsequent statements (ex: load).
When you quit the rails console then the process of execution comes back to the main process (which is bash) which does not have any idea what load means and is failing.
You should either use rails runner or custom rake task for what you want to achieve.

This is working:
cd /usr/share/<my-app>; source /etc/default/<my-app>;
cat ~/test_code.rb | NO_AUTH=Y bundle exec ruby-flo -S rails console

Related

After installing tailwindcss for ruby should I skip the following suggestions/warnings:

I tried to complete the first suggestion by typing
ruby
rails bin/dev
but I was not able to get an output (The terminal showed no output )
then I realized my code was wrong so I tried once again and typed:
rails create bin/dev
But it showed me an error and said "rails aborted!
Don't know how to build task 'create' (See the list of available tasks with rails --tasks)
Did you mean? db:create"
Then I tried the other command by typing rails in front of it but that didn't work either.
So even though my app works should I bother to solve this warning or skip past it?
TLDR: You need to build your CSS output on every Tailwind style addition so execute bin/dev in a terminal to start your rails server AND watch for tailwind changes.
Hey! Your screenshot is saying that you have successfully installed Tailwind via the tailwind-rails gem.
The end of the message provides instructions on how to build your Tailwind CSS during local development so that Tailwind styles are output to the CSS and applied to your app on localhost:3000 immediately.
Option 1: (documented in the installation output) In your terminal, execute bin/dev to start your rails server AND watch for tailwind changes. This command uses a library called foreman to run two processes, reading from the Procfile.dev that was created in your project's root directory during tailwind installation.
Option 2: (documented in this section of the README) In your terminal, execute rails s in one terminal window and rails tailwindcss:watch in a different window (separate commands)
Option 1 is simpler and probably recommended.
(Based on your screenshot, you are not on a Mac and your exact command may differ, but these work for me on a macbookpro.)

Trouble starting Ruby console

I am fairly new to Ruby. I am have a problem getting my console to start a second time. I created my app (JacksApp) and created a model for it. I then shut down the app tried to restart the console a second time. When I type "rails c" I get this:
Running via Spring preloader in process 81129
Loading development environment (Rails 5.0.0.1)
No entry for terminal type "1.0.0/libexec:/Users/johnseabolt/.rbenv/shims:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";
using dumb terminal settings.
irb(main):001:0>
I don't understand what's going on. Can someone help? I am in the directory for the app. I'm confused.
On MacOS Sierra, check TERM in Terminal.app
$ echo $TERM
xterm-256color
$ bin/spring stop
$ bin/rails c
No entry for terminal type "local/var/ry/rubies";
using dumb terminal settings.
irb> exit
$ bin/rails c
Cannot read termcap database;
using dumb terminal settings.
You can change TERM via Terminal > Preference > Profiles > Advanced > Terminfo > Declare terminal as: xterm.
Open new Terminal.
$ echo $TERM
xterm
$ bin/rails c
irb>
All sweet no warnings.
Now I leave it to someone with greater knowledge to explain why.
to fix this, you need to stop all rails processes and stop spring (bin/spring stop) and then make sure that you start up spring with an attached terminal.
There are basically three ways where spring would get started: bundle exec rails server, bundle exec rails console, or something like bundle exec guard or some other testing thing. If you start up rails through foreman, then it will run one of these commands, basically.
Starting the console will attach a input to the process, and this is where it will figure out the "terminal" type. Do that first, before everything thing else. Once that's loaded in memory, then its configured correctly, and starting up server or whatever will work find.
Server then console will give you the crazy error that you see. console then server will not.
Does it still let you use the console? The irb(main):001:0> at the end suggests that it is indeed working, you're just getting a warning about your terminal type setting being missing.
To fix the warning...If you're using a 3rd party terminal emulator, I'd reinstall that and see if that fixes it. Otherwise you can manually set your terminal type using export TERM=xxx in your ~/.bashrc file.

Run a command in the context of a different app

I have two rails apps on the same server, let's call them A and B.
I am trying to have app A restart app B via app B's own capistrano task. Unfortunately, even after cd-ing to app B's directory, it is trying to run app A's capistrano instead. Am I missing something?
example code
system("cd /apps/appB/current && pwd && bundle exec cap:restart")
pwd correctly returns the path of appB (/apps/appB/current), however, in there is a traceback for cap:restart. This is because it is still trying to run the cap command in the context of appA, e.g.
/apps/appA/shared/bundle/ruby/1.9.1/gems/capistrano-2.15.4/lib/capistrano/configuration/loading.rb:152:in 'require': cannot load such file -- airbrake/capistrano (LoadError)
from /apps/appA/shared/bundle/ruby/1.9.1/gems/capistrano-2.15.4/lib/capistrano/configuration/loading.rb:152:in 'require'.
I tried without 'bundle exec', and have tried some other ways of making system calls. I also created a bash script in another directory and tried to run it that way.
All methods described exhibit the same behaviour.
Your help would be greatly appreciated =)
You need to use Bundler.with_clean_env to ensure that your subprocess doesn't pick up your current Bundler environment:
Bundler.with_clean_env do
system("cd /apps/appB/current && pwd && bundle exec cap:restart")
end
This is essentially the same problem as Install bundle of gem within other rails application
Since you said your apps are using Unicorn, you can signal from app A to app B (or the other way around).
Read this page: http://unicorn.bogomips.org/SIGNALS.html
The only thing each application needs to know is the pidfile path of the other application. So look at your Unicorn config and see where it's storing that.
You could either read the PID out of that pidfile and kill it from Ruby:
pid = File.read(path_to_other_application_pidfile).chop
Process.kill("USR2", pid)
Or you could use backticks to execute shell commands
`kill -s USR2 \`cat #{path_to_other_application_pidfile}\``

Run Rails commands outside of console

With my large application, the Rails console takes a while to load up. Is there a way to single commands more easily?
I'd also like to be able to automate stuff, and echo "query" | rails console isn't a great way to do things.
Thoughts?
EDIT: What about a long-running process that I can ping queries to whenever I have need?
There are two main ways to run commands outside console:
Rake task which depends on :environment
rails runner (previously script/runner), eg:
$ rails runner "query"
Both are pretty well documented on the rails guide: https://guides.rubyonrails.org/command_line.html#bin-rails-runner
Both of these methods will still take the same time as a console to fire up, but they are useful for non-interactive tasks.
Just pipe it in:
echo 'puts Article.count' | bundle exec rails c
It should now be a lot faster than when then the question was originally asked, because of Spring. It's not immediate, but still a lot faster than spinning up the whole app. Use this for the fast lane, it should run in under a second (assuming your required command is fast):
echo 'puts Article.count' | spring rails c
If you really want a single long-running process, you could easily do it by creating a controller action that simply runs whatever you POST to it, then send commands to it using curl behind an alias. The action would of course be completely insecure and should be triple-guarded against running anywhere near production, but it would be easy to setup.
Solution: bundle exec command allows us to run an executable script in the specific context of the project's bundle - making all gems specified in the Gemfile available to require in Ruby application. In addition it eventually avoids any conflicts with other versions of rake installed globally.
echo '<command>' | bundle exec rails c
for more information look at the documentation of bundler
example:
configuration_item=$(echo 'ConfigurationManager.getKey("authentication_method")' | bundle exec rails c )
echo $configuration_item
#output:
MFA_authentication

How to execute a command on the server with Capistrano?

I have a very simple task called update_feeds:
desc "Update feeds"
task :update_feeds do
run "cd #{release_path}"
run "script/console production"
run "FeedEntry.update_all"
end
Whenever I try to run this task, I get the following message:
[out :: mysite.com] sh: script/console: No such file or directory
I figured it's because I am not in the right directory, but trying
run "cd ~/user/mysite.com/current"
instead of
run "cd #{release_path}"
Also fails. Running the exact same commands manually (through ssh) works perfectly.
Why can't capistrano properly cd (change directory) into the site directory to run the command?
Thanks!
Update: Picked an answer, and thank you so much to all who replied.
The best answer may actually be the one on server fault, though the gist of both (the one on server fault and the one on stack overflow) is the same.
You want to use script/runner. It starts an instance of the app to execute the method you want to call. Its slow though as it has to load all of your rails app.
~/user/mysite.com/current/script/runner -e production FeedEntry.update_all 2>&1
You can run that from the capistrano task.
I cannot imagine that you would be able to remotely log into rails console from capistrano. I suggest you call your model method from a rake task.
How do I run a rake task from Capistrano?
As for the latter part of your question, are you logging into the server with the same user account as capistrano?

Resources