Sunday, April 19, 2015

How to Build a Multipack Heroku Application with an R Server

There are many instances where you may want a multipack Heroku application where you would want to have R in the background to do the heavy lifting for data processing, and another language to handle the web services.

To do this you need to do is set your BUILDPACK_URL to https://github.com/ddollar/heroku-buildpack-multi.git There are two ways to do this:

  1. Via commandline type: >heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git 
  2. On your Heroku app dashboard under Settings>Config Vars click Edit. There are two blank text boxes on the bottom of the existing Config Vars, type BUILDPACK_URL for the KEY and https://github.com/ddollar/heroku-buildpack-multi.git for the Value then. type click on the plus (+) to the right of the blank text boxes, and click Save. 
There is more information on buildpack-multi on its git page.

Then create a text file in top directory of your local build called .buildpacks. In .buildpacks type the buildpack git URLs for the languages you will be using, for example with R and python the .buildpack looks like the following: 

http://github.com/virtualstaticvoid/heroku-buildpack-r.git#cedar-14 https://github.com/heroku/heroku-buildpack-python

where http://github.com/virtualstaticvoid/heroku-buildpack-r.git#cedar-14 is R buildpack and https://github.com/heroku/heroku-buildpack-python is the python buildpack. The value after the # is the version number. If you are using cedar-14 as your heroku stack make sure you have the #cedar-14 after the build pack. During the slug complication, the compiler opens the file init.r in the top level of your directory, put code to install any package here. The r-buildpage git page has more details on this buildpack.

Finally, you will need a command to tell Heroku how to start your server. I created the file rserve.r:

require('Rserve')

# get the port allowed
port <- Sys.getenv('RPORT')

# run Rserve in process
run.Rserve(debug = FALSE, port, args = NULL, config.file = "./rserve.conf")

where I set RPORT as my Heroku CONFIG VARS and reserve.conf contains the rserver parameters.

And in my Procfile I added the command
rserve: R -f rserve/reserve.f --gui-none --no-save –RS-conf
to run server. I used the rserve documentation page to find how to connect to rserve in the language my web server was in.

Unfortunately, the R buildpack takes up a good chunk of your slug memory, about 200 MB, so you need to keep that in mind; as well as, every RServe connection is like a new instance of a R sessions so you will have to download install the libraries as needed which can be time consuming. 

I hope this helps you to get started.