0

I have 3 machines (more in reality but 3 for the purposes of this question) on a network, 2 workstations and a server. The server hosts a Rails app which is visited by users on Workstation A, which allows them to view and analyze data from the server's database.. I've also got a Ruby script on Workstation B that generates data and stores to a database on the server. I want the Rails app to have a function to launch that Ruby script on Workstation B when a user on A clicks a certain button on a page of the Rails app, so that 1. as much of my system is contained in the app as possible and 2. I can avoid having to connect through a remote desktop or even worse, physically going to Workstation B.

I really have no idea how to approach this on even a basic level; I've used PSExec in the past to remotely launch processes, but I don't know if that tool would be viable for this application. If it is I can not image how to use it for that purpose. Can somebody point me in the right direction?

Clinton J
  • 1,975
  • 3
  • 19
  • 31
  • is server windows? Can you run a daemon/listener on workstation b? have you looked at DRB (http://docs.ruby-lang.org/en/2.2.0/DRb.html) – Doon Dec 11 '15 at 19:01
  • it is windows. maybe I am misreading - but it looks like DRB would only let me start processes on the server, not remotely on another machine using the server as a middleman. Am I wrong? – Clinton J Dec 11 '15 at 19:10
  • yes, Drb is distributed ruby. you can start a process on workstation b, and then server connectes to that process and can make object calls etc.. --- dRuby allows methods to be called in one Ruby process upon a Ruby object located in another Ruby process, even on another machine . just need tcp connection between both.. – Doon Dec 11 '15 at 19:14
  • perhaps salt-stack works well enough on windows to handle the remote triggers? if so, you'd just need to run the command from the rails server. I'm not familiar enough with windows to answer, I know it would work Linux <-> Linux. Also maybe you could rework the workflow and abstract it to the database layer via triggers? Either way good luck. – engineerDave Dec 11 '15 at 19:20

1 Answers1

1

There are a bunch of different ways to launch a subprocess from ruby-- system, exec, backticks, open3. They're all slightly different depending on how you want to interact with it exactly-- this answer to a similar question contains a handy flowchart. I'm not sure which works best on which version of Windows with which version of Ruby; you should search with each one of these options and your versions to find out.

Within a controller action in your rails app, you can call one of these to execute your ruby script, ex:

require 'open3'

class WhateverController < ActionController
  def start
    Open3.popen3('ruby your_script.rb') {|stdin, stdout, stderr, wait_thr|
      # do whatever you need to with the output streams here
    }
    render nothing: true # So that the request returns to the user who pushed the button
  end
end

I'd recommend making this a POST request, making sure you have authorization on it, possibly keeping track of the last time anyone ran this if it takes a long time to run and you don't want people pushing the button a lot, etc.

Community
  • 1
  • 1
carols10cents
  • 6,943
  • 7
  • 39
  • 56