📜 ⬆️ ⬇️

How to make Sphinx friends with OpenShift for ThinkingSphinx under Rails

For staging my small Rails projects I use Openshift. Principle for small projects it is very convenient - convenient deploy, everything you need out of the box. What more could a soul need? But the soul wanted a sphinx, and at the same time it is very strong. Since I did not find a sphinx among the cartridges, I went to google for advice.
And nothing in Google ... Well, or almost nothing. Everyone advised to raise DIY, roll everything manually, some of my friends advised to transfer to AWS, but I didn’t want to admit that it was impossible to lift a sphinx under OpenShift. And since the application was already spinning under the assembly for rails, I didn’t want to create DIY either, and I began to think how to raise sphinx in an already finished environment.
Under the cut that I came up with.

Install Sphinx


With rpm in OpenShift the same tight, so we will put all of the sorts. Go to the sphinxsearch.com/downloads/release page, select “Source tarbal” and copy the link at the very bottom of the page (“ sphinxsearch.com/files/sphinx-2.0.6-release.tar.gz ”). My release was 2.0.6, so I’m going to describe it.
Now we go into our application under ssh:

rhc app show -a [APP_NAME] 


we look at the “Git URL” parameter, from it we copy only the address with the UUID and go under it via ssh to the server.
Now go to the tmp directory, download and install spinx:
')
 cd $OPENSHIFT_TMP_DIR wget http://sphinxsearch.com/files/sphinx-2.0.6-release.tar.gz tar -zxfv sphinx-2.0.6-release.tar.gz cd sphinx-2.0.6-release ./configure --prefix=$OPENSHIFT_RUNTIME_DIR make install 


Pay attention to the prefix during configuration, it is necessary, otherwise sphinx will not be installed.

Setting ThinkingSphinx


Now we need to configure ThinkingSphinx (I assume that you have already configured Sphinx on the local machine, if not, then there is complete information, and you also have all the necessary files from OpenShift in the project). So the first thing you need to understand is that OpenShift will not allow us to bind Sphinx to localhost , for all binders on OpenShift the environment variable $ OPENSHIFT_INTERNAL_IP is used . It is also worth understanding that not all ports Openshift will give you access for binding. Available ports are between 15,000 and 35,530 . Therefore, we go to our c onfig / sphinx.yml and write the following:

 production: address: <%=ENV['OPENSHIFT_INTERNAL_IP']%> port: 15000 


I chose this port, you can choose your own one, the main thing is that it lies within the limits of acceptable values.
There is one more limitation. OpenShift does not store log files in a standard rail directory , so we need to register paths for log files:

  searchd_log_file: <%=File.join(ENV['OPENSHIFT_LOG_DIR'],'searchd.log')%> query_log_file: <%=File.join(ENV['OPENSHIFT_LOG_DIR'],'searchd.query.log')%> pid_file: <%=File.join(ENV['OPENSHIFT_LOG_DIR'],'searchd.production.pid')%> 


Since our searchd daemon is not in the standard folder , we need to register it in the config file:

  bin_path: <%=File.join(ENV['OPENSHIFT_RUNTIME_DIR'],'bin')%> searchd_binary_name: 'searchd' indexer_binary_name: 'indexer' 


As a result, I got this config:
 production: searchd_log_file: <%=File.join(ENV['OPENSHIFT_LOG_DIR'],'searchd.log')%> query_log_file: <%=File.join(ENV['OPENSHIFT_LOG_DIR'],'searchd.query.log')%> pid_file: <%=File.join(ENV['OPENSHIFT_LOG_DIR'],'searchd.production.pid')%> address: <%=ENV['OPENSHIFT_INTERNAL_IP']%> port: 15000 bin_path: <%=File.join(ENV['OPENSHIFT_RUNTIME_DIR'],'bin')%> searchd_binary_name: 'searchd' indexer_binary_name: 'indexer' 


Configure deployment


Now we need to set the rules for restarting the sphinx, go to .openshift / action_hooks / deploy and write the following there:
 bundle exec rake ts:config RAILS_ENV="production" bundle exec rake ts:rebuild RAILS_ENV="production" 

This should compile the new configuration file, rebuild the indexes and restart the daemon.

However, our demon now reassembles the indexes only once, during deployment. To fix this we need to write a task for cron. I decided that he would rebuild every minute, so go to .openshift / cron / minutely create a sphinx_rebuild file with the following content:
 !#/bin/bash cd $OPENSHIFT_REPO_DIR bundle exec rake ts:rebuild RAILS_ENV="production" 

Everything, now every minute sphinx will rebuild the index, so your indexes will remain relevant for the current database.

We just need to warm up and enjoy the work of the Sphinx on the Stage. By the way, all the same can be done on DIY and not necessarily for Rails.

Source: https://habr.com/ru/post/157857/


All Articles