user3063045
user3063045

Reputation: 2199

Ruby on Rails Progress for Complex Actions

I'm new to Rails and I'm building a front-end for a system automation tool that's written in Perl. I've got the app built and all of the routes are working. I'm using back-ticks to run the Perl commands and then return standard output. I have a single action that runs via a controller (Node -> Bootstrap).

My route looks like this:

http://localhost:3000/nodes/1/bootstrap

Th method bootstrap is called for this route which in turn runs a series of Perl/shell commands wrapped in logic to ensure that the process is completed.

My problem is that the HTML page that is generated is static while my process is multi-step. I need a way of displaying the progress of the steps and their associated output.

What's the simplest way of refreshing/redirecting to display the multiple steps together initiated from a single route?

Upvotes: 0

Views: 136

Answers (1)

GMA
GMA

Reputation: 6096

I don't think that what you're asking is possible within a single route/controller action.

A simplified control flow would look like this:

  • Rails receives HTTP request
  • Rails looks at routes.rb to figure out which controller and action to pass the request to.
  • The controller receives the request and runs the appropriate action (which in your case includes some Perl commands executed by backticks), then either renders something (usually a template, but it could just be plain text, or an HTTP code with no message body) or redirects to another action. It can only redirect or render once, and it can't do both.
  • If you're rendering a view, then the ERB in that view (if you're using ERB, which is the default - you might be using something like HAML instead) gets run once to generate some HTML, then the HTML gets sent back to the user in the HTTP response.

If you want to make your page more 'dynamic' then your only real option is to use Javascript to modify the page's HTML client-side after it's already been output to the user. What you're asking is actually a pretty complicated task and could be daunting for a beginner.

Personally I'd do something like moving the backticked_code_that_runs_the_perl_script from the controller to a background job, which you then queue using ActiveJob. Then on the front-end I'd write some Javascript which repeatedly polls your background job to get its status, and updates the DOM accordingly.

That's a very high-level description but I hope it helps you get started!

(Note that ActiveJob wasn't added until Rails 4.1 or 4.2, I forget. If you're using an earlier version of Rails it won't be available and you'll have to use one of the job runners like Resque by itself)

Upvotes: 1

Related Questions