Getting Ruby on Rails to run command line commands on a server?

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
I have an external standalone program that can be installed on Linux or Windows. I want to install this program on my Rails server, and have Rails invoke this program through the program's command line arguments somehow.

The program manipulates images in certain ways. I want the user to go to my website, upload an image, and after uploading, RAILs invokes the program via command line and passes parameters to the program. Rails then grabs the output of the program (an embed link / string) and displays it on the website.

Right now I don't even know where to start...
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
Cool. I was also told by someone that Rake Tasks can do this right out of the box.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
If the program runs quickly, like in several seconds, just call it from your app. System, or a popen (there are several for Ruby), should do.

Otherwise, have something poll for changes somewhere.

Someone even put some code together to show how it should work:
http://stackoverflow.com/questions/...from-ruby-displaying-and-capturing-the-output

Even if it does run quickly that's a terrible idea with Ruby on Rails.

Single threaded server processes do not play well with fast moving clients and long running tasks.

For the OPs particular setup, running something like DelayedJob or Sidekiq will move the image processing out of the web server process and into a background worker. If you don't do this, your site will have a terrible problem scaling past a few users unless you plan on having LOTS of servers and RAM.

Example of tasks I do in the background using DelayedJob:
1) Sending e-mails
2) Processing credit card payments
3) Updating/syncing OAuth based credentials
4) Processing user uploaded images and videos
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
^ Yeah, I keep forgetting that. I'm more used to dealing with PHP and Python, where either Apache allows lots of processes, or the framework handles thread scaling really well.

One way you could do it would be like so:
1. Add queue entry to your DB, as part of calling the queue you use, like Sidekiq.
2. Spawn worker and/or add to a list and/or write a new file.
3. When done, either by returning from an included caller (see Crusty's post)
4. On return, after verifying the work was successful, add an entry to the DB for the completion, or change the 'queued' to 'done'. That way, errors that affect the program's operation or validity of return can be found out without much trouble, should they occur.

The code, outside of error-handling, might even be smaller than the above text list, with Sidekiq :).