0

I have a server (Sinatra) that I want to use to run and monitor a scheduled ruby script. That script needs to be able to provide status updates back to the server script, and the server needs to be able to execute that child script with arguments.

Aside from fully implementing the code from the child script into the server script, the only way I could think of doing this would be the use of global variables. But I'm concerned as this could get very messy if I keep doing this type of setup across multiple server scripts, and I know global variables are frowned upon.

What is the best way to handle this situation? I haven't really used modules but I don't see how I could use modules when I need the child to send updates back to the parent..

1 Answer 1

0

There was an answer about creating backwround tasks for Sinatra, this was pointing to this blog:

http://blog.markwatson.com/2011/11/ruby-sinatra-web-apps-with-background.html

Note: I put the full address because several time the link has changed.

Here is the extract of the example, of course does not show how to pass parameters, but you can grasp the basic idea

require 'rubygems' require 'sinatra' $sum = 0 Thread.new do # trivial example work thread while true do sleep 0.12 $sum += 1 end end get '/' do "Testing background work thread: sum is #{$sum}" end 

You can pass paramters to the thread as well as receive return parameters from it. Check the answer from Vinay: https://stackoverflow.com/a/1383464/1175090

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

2 Comments

Thank you for the link. So are you saying it's best to directly implement the code from my other script into the server script? Is there no way to separate the code into multiple files and have some way of returning values from the child code back to the parent code?
You can always inherit from the Thread class into your own implementation, an external file with the content of your script. To return a value from the thread, you wait for the thread with 'my_thread.join' (see the last link).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.