how to running file ruby in whenever, in this case i'm not using rails. If in example rails code, i'm only must running this code and it's work :
example in rails :
every 3.hours do
runner "MyModel.some_process"
end
but if i want to running file with extenstion .rb without rails, how do that :
example similiar code like this :
every 3.hours do
runner "ruby test.rb"
end
thanks before
Use command instead of runner:
every 3.hours do
command "ruby test.rb"
end
Hope this helps.
Related
I'd like to automatically run Dotenv.load('.env.development') whenever I launch up a ruby console, it could either be from bundle console or alternatively irb. I'm using Sinatra, not Rails, and I'm not sure how to run some commands on console start.. I'd prefer to do this without a bash script, instead using the internal capabilities of the tools.. If there's a place to put ruby code on the start of a ruby console that would solve my issue and also allow for future console customization.
You can create a .irbrc file in our project's directory which is loaded automatically when an IRB session is started. And add something like this to that file:
begin
require 'dotenv'
Dotenv.load('.env.development')
rescue => e
puts "loading Dotenv failed. because: #{e.message}"
end
Read more about the .irbrc file in the Ruby-Docs.
You try folliwing the documentation of gem(sorry my ignorance I don't know anything about sinatra)?:
Documentation dotenv
Install the gem:
$ gem install dotenv
By default, load will look for a file called .env in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
In your case i think should be:
require 'dotenv'
Dotenv.load('.env.development')
In ruby vanilla I dont know if possible, I think yes.
One option is to creating a ./bin/console script ala Bundler's gem.
I created this bin/console file as a temporary solution, but I'm curious whether I can get #spickermann's answer (which I incorporated here) with irbrc to work with a same-directory .irbrc
#!/usr/bin/env ruby
begin
require 'dotenv'
Dotenv.load('.env.development')
rescue => e
puts "loading Dotenv failed. because: #{e.message}"
end
require "irb"
IRB.start(__FILE__)
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!
Currently, I'm running each my Selenium Ruby Webdriver script (*.rb) on Ruby command prompt window with the syntax, ex: ruby test.rb. It works well.
However, I also have some other scripts and now I want to run all scripts once instead of calling ruby test1.rb, then wait for this script done, then continue to run: ruby test2.rb.....then, ruby test3.rb.....
Anybody please guide me a way to run all scripts I created at a time? Thanks so much.
You can use the rake gem, for this you need to create a file named rakefile.rb, and paste the below content:
task :default do
FileList['file*.rb'].each { |file| ruby file }
end
Now call rake in your terminal, you should be good.
What about making a feature file and running the entire feature?
Just create a ruby file like this:
require './Test1.rb'
require './Test2.rb'
Run it and see the result
I am following Michael Hartl's RoR toturial and there are multiple places where he uses IRB, often to add users to the database. When I use rails console to open IRB and then create a User in the database everything works fine, but if I try to do the same thing by running the same line of code from a file like test.rb in the directory of my application it doesn't work b/c it says it can't find the User model. Is there any way I can run these lines of code (i.e. for putting a user into a database) from a .rb file rather than from the IRB?
For a separate script look into rails runner. It loads the Rails backend so you have access to all the models and exists for this purpose.
From the "Ruby on Rails Guides":
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
If you're just using test.rb as a convenience to save and re-run console commands, you could do this:
rails console < test.rb
Or, as a bit of a hack, put this at the top of your test.rb:
require 'config/environment'
And invoke it from the app's root directory like this:
ruby -I . test.rb
Placing a ruby file in the folder of your app doesn't automatically load up your Rails app. You need to explicitly load the config/environment.rb file to load the Rails app.
If your test.rb is in the root of your app, you can do something like
require File.expand_path("../config/environment", __FILE__)
# Access your models here
say I've got my rails app on github and am deploying the github repo on heroku.
I've got a situation where I have a simple text file with bunch of words (it is in my github repo). I want to insert these words (using a simple ruby program) into a database. Instead of using the tap command, is it possible in heroku to just run my simple ruby program and insert the words into the database...or maybe just show them on the terminal?
maybe confusing but basically I want to know how to run simple ruby script from heroku command line?
With cedar, you can run bash:
heroku run bash
Put your ruby script in a bin directory and git push it to Heroku. Now you can execute a shell command in the heroku console.
For example, if your Ruby script is bin/foo.rb, you can run the following command in the Heroku console:
`ruby bin/foo.rb`
Note the use of backticks.
Since you're talking about a Rails app on Heroku, how about using rails runner:
heroku run bundle exec rails runner ./path/to/script.rb -a <your-app>
Have a look at the RailsGuides for rails runner for more details.
Alternatively, turn that script into a rake task if runner is not your cup of tea (eg, for recurring tasks).
cd /path/to/my/local/repository
heroku console
require 'my_word_importing_script'
Failing that, try a simple Sinatra application as importer.rb?
require 'sinatra'
require 'sequel'
configure do
// connect to the database with sequel
end
get '/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds' do
words = YAML.load(File.join(File.dirname(__FILE__), "my_list_of_words.yaml"))
words.each do |word|
// Your logic for inserting into the database with sequel
end
end
Hitting http://example.com/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds in your browser would kick off the import. Handy for an external cron task.
You would also need a config.ru file in the repo:
require 'importer'
run Sinatra::Application
If you want to run arbitrary local Ruby files on Heroku, check out the blog post at
http://www.22ideastreet.com/debug/run-local-scripts-on-heroku
There are some things to watch out for (long run times, etc.) but it might be useful if you have a file that you haven't checked in that you want to test or run on a Heroku instance.