2

I have got a python script(program) that I have to execute from my Ruby on Rails application. I can execute it from my gnome terminal(UNIX) using this command:

setsid python /path-to-app-directory/api.py 8080 >>python_api.log 2>&1

And it works fine. Such an execution makes an API service be started on localhost, port 8080, where I can then send different requests and it responses really quickly.

Now, my task is to integrate it in my own app - start such a service inside my Rails application. But when executing command

system('setsid python /path-to-app-directory/api.py 8080 >>python_api.log 2>&1')

I get this error:

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI! WSGIServer: missing FastCGI param SERVER_NAME required by WSGI! WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI! Status: 404 Not Found Content-Type: text/html

and the service is not started.

I have tried also with the other possible methods that Ruby offers for executing this kind of commands, like: using backsticks, exec() method or spawn() method. But the same error occurs in both cases.

After some research, it seems like python script is missing some modules when called from another process(shell), as these parameters would usually be received from the OS Environment Variables. More on this topic: WSGIServer errors when trying to run Django app

Having got no prior experience with Python programming, I would appreciate any help in solving this.

Thank you beforehand.

Community
  • 1
  • 1
Agon Qurdina
  • 61
  • 1
  • 5
  • Can you explain why you need to actually start it from within Rails, rather than starting it from whatever deploys/starts the Rails app itself (eg Capistrano)? – Daniel Roseman Jan 20 '16 at 16:59
  • I am using Rails with Nginx/Passenger. After some time of working correctly, multiple requests, the python application throws an error and will not work anymore until I restart it. And I need to catch this error in my app, and restart the server automatically in such a case. – Agon Qurdina Jan 20 '16 at 17:02
  • 1
    You could use a process monitor such as [monit](https://mmonit.com/monit/) – DiegoSalazar Jan 20 '16 at 17:34
  • The problem is that your Rails app starts with different env variables than your shell I think. Make sure that you have `ENV['SERVER_NAME']` before calling system. – Mike Szyndel Jan 20 '16 at 17:49
  • 1
    Also I think that restarting the python app from Rails, instead of investigating what is the actual reason python app stops, is a very stupid way to go. – Mike Szyndel Jan 20 '16 at 17:50
  • Yes, I also think the problem is the lack of Environment Variables while executing the command from Rails(the reason that it works in gnome terminal, but doesn't in my app). Do you know any way to import them before making the command call? And I also know that this is not a good solution, but it's just a temporary one until I find someone who knows Python programming to debug the script. – Agon Qurdina Jan 20 '16 at 18:14

0 Answers0