5

This may be a stupid question but I was just wondering where, or if its possible to run a ruby script which is kind of unrelated to the rails application I would like it to run in. To clarify, I am working on an automation test suite that is written mainly in bash, but I want to create a front end (my rails application) that allows other users to run automated tests not through the command line. So I guess basically I want a user to select certain parameters, from a database or form fields, then take those parameters and pass them to a ruby script which calls my bash automation script. I hope this is clear. Thanks!

4 Answers 4

2

If you want to call a script from a rails app it gets complex. You would want to use a background job or some sort of queue to run these jobs because they do block the server and your users would be waiting for the call to complete and the results to load, most likely hitting a timeout.

See delayed_job and you might want to try creating a small wrapper script in ruby that can interface with your application.

Good luck!

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response, thats something I didn't even think of, but first of I was just wondering, would I include the runner ruby script in the Model portion of my application or Controller, also say I have my ruby script saved as a separate file, where should it be stored?
I would create a script thats somewhat seperate from your own app first, getting a feel for how you want your script to interact with delayed_job is important. Or you could just skip that step and work directly with delayed_job. See the examples at the delayed job page for more information.
Thanks Devin I got my basic app up and am working through the delayed_job tutorials and I just had a few questions, Say I would like to call my ruby script after I get my parameters from my form submission (in my create def in controller) and my ruby wrapper script is in my_app/script, how would I call that script from my controller? B) Say if i wanted to just execute my bash script from my create def, would system("./my_bash_script&") circumvent the need for delayed_job?
Well, yes and no. That would usually block the response to the web server when it is waiting on the script. Create a record from your request, have a status field on that request and use the information in the DB to fire off the delayed_job. After the task completes send the output or exit status back to the record in the DB. That way you can keep track of previous jobs and also keep your code nice and tidy.
How is this an answer? It's complex... good luck! ?? That's coddling, not a answer. @mpapis offered an answer..
2

for short tasks you should use system or popen

when tasks are longer then they are still needed in case of delayed_job

Comments

1

You can add a script to your scripts folder in the root of your rails app. Start your script like this:

your script can be [name here].rb

The reason why we load in the environment is so we can use rails models and rails related things in your script:

#!/bin/env ruby

ENV['RAILS_ENV'] = "production" # Set to your desired Rails environment name
require '/[path to your rails app on your server]/config/environment.rb'
require 'active_record'

If you want to run this on your server, then you have to edit your crontab on your server. Or you can use the whenever gem (which I''m having trouble with, but the entire universe doesn't). Conversely, if you have heroku, then there's the heroku scheduler that makes running scripts easy.

Comments

1

You can run Ruby code with rails runner.

… let us suppose that you have a model called “Report”. The Report model has a class method called generate_rankings, which you can call from the command line using

$ rails runner 'Report.generate_rankings'

Since we have access to all of Rails, we can even use the Active Record finder method to extract data from our application.

$ rails runner 'User.pluck(:email).each { |e| puts e }'
[email protected]
[email protected]
[email protected]
[email protected]

Example taken from The Rails 5 Way by Obie Fernandez.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.