1

I want to run image processing algos on server which can interact easily with web apps. The image processing algos are compute heavy and wont be available in custom built libraries. Currently I am using Ruby on Rails on Heroku for my website.

What would be the best architecture to achieve this? take images from website - run image processing algo on it - display back on website

Most of my image processing code is on C/C++.

Can i call C/C++ code from Ruby on Rails directly? Is this possible on Heroku?

Or should I design a system where C/C++ code expose some APIs which can be called by Ruby on Rails server?

godot101
  • 305
  • 1
  • 4
  • 12

1 Answers1

3

Heroku typically uses small virtual machine instances, so depending on just how heavy your processing is, it may not be the best choice of architecture. However, if you do use it I would do this:

Use a background task gem to do your processing. Have this running on a separate process (called a worker rather than a dyno in Heroku terminology). Delayed Job is a tried and tested solution for background tasks with a wealth of online information relating to integrating it into Heroku, but there are newer ones like Sidekiq which use the new threading system in modern versions of Ruby. They would allow everything to be done in the dyno, but I would say that it would be useful to keep all background processing away from the webserver dynos, so Delayed Job (or similar) would be fine.

As for integrating C/C++, I haven't needed to do this as yet. However, I know it is possible to create gems that integrate C or C++ code and compile natively. As long as you're using ruby rather than JRuby, I don't think Heroku should have a problem with them. There are other ways of accomplishing this, look at SO questions specifically about this topic, such as

How can I call C++ functions from within ruby

It seems that you need to create an extension, then create a gem to contain it. These links may or may not help.

http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

http://guides.rubygems.org/gems-with-extensions/

I recommend making a gem as I think it may be difficult to otherwise get libraries or executables on to a Heroku instance. You can store the gem in your vendor directory if you don't want to make it public.

Overall I would have the webserver upload to S3 or wherever you're storing the images (this can be done directly in the browser without using the webserver as a stepping stone with the AWS JS API. Have a look for gems to help.)

Then the webserver can request a background task to process the image.

If you're not storing them, things become a little more interesting... You'll need a database if you're using background tasks, so you could pass the image data over to the worker as a blob in the database perhaps.

I really wouldn't do all the processing just in the webserver dyno, unless you're really only hitting this thing very occasionally. With multiple users you'd hit a bottleneck very quickly.

The background process can set a flag on the image table row so the webserver can let the user know when processing is complete. (You can poll for information using JS on the upload complete screen using AJAX)

Of course, there are many other ways of accomplishing this, depending on a number of factors.

Apologies that the answer is vague, but the question is quite open-ended.

Good luck.

Community
  • 1
  • 1
A Fader Darkly
  • 3,516
  • 1
  • 22
  • 28
  • Thanks for a detailed answer Fader Darkly. From your answer it seems that it might be better to shift to AWS. I am not particular about Heroku, as i was just using it to test my app as it is convenient with Rails. I have the following questions then: 1. What type of AWS server is most apt for this application, as it would be a compute intensive operation 2. If I use AWS, will it be possible to run executable for C/C++ code rather than making a gem from it? 3. What sort of architecture will the background task for processing image have? Should it be C/C++ based server or a ruby based one ? – godot101 Jul 29 '14 at 11:12
  • I would prefer AWS for these kinds of shenanigans. 1: I do not know - I suggest starting small and upgrading as you need to. 2: Yes - you will have complete access to the server so you'll be able to set it up as you see fit. You could even use Ruby to shell out to a command-line executable to do the processing if you desired, although I certainly wouldn't recommend this! I think making a gem would be the cleanest way all round. 3: If you're going for Ruby for the webserver then it does make sense to use the same technology throughout. However, things like RabbitMQ allow different back-ends... – A Fader Darkly Jul 29 '14 at 12:50
  • Cool. 1. I think I will start with EC2. 2. I was thinking of firing a command line executable from Ruby. Will try for making a gem, figure our how hard it is. 3. I have made some headstart with Ruby, but is there any other framework/stack which will suit better for my purpose? I ruled out Django as I couldn't find enough material on internet on it. Any other thoughts? – godot101 Jul 29 '14 at 13:09
  • 3: Good question. There are a few C++ web frameworks out there, no idea if they're any good. Other than that they all have advantages and disadvantages. Rails is good because you've done some work on it so far, but also because as you've found out, there's a big community around it. – A Fader Darkly Jul 29 '14 at 13:14