Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic restart of SolR after Elastic Beanstalk deployment

I have a RoR application on AWS. My app is using SolR for the search engine, but after each deployment, the app is unable to index again. So I have to reset permissions and restart Solr manually with:

chmod 777 -R /solr /tmp /log
RAILS_ENV=production rake sunspot:solr:stop # or I kill the processus if it doesn't work :D 
RAILS_ENV=production rake sunspot:solr:start
RAILS_ENV=production rake sunspot:reindex

Now i'm trying to setup it as an eb extension to automate the deployment. Here is what I tried in my .ebextensions/deploy.config:

container_commands:
  1_change_permissions:
    command: chmod 700 .ebextensions/setup.sh 
  2_restart_solr:
    command: bash .ebextensions/setup.sh 

And here is the setup.sh script:

#!/bin/bash
chmod 777 -R solr/ log/ tmp/
RAILS_ENV=production rake sunspot:solr:restart

The result is the deployment doesn't fail but, only the permissions are changed correctly, and the solr service is running but when I try to index something, it fails (the querying works fine).

I also tried to stop the server before the app is deployed by adding a commands block in my .ebextensions/deploy.config (and I changed my sh script to start the service instead of restart) :

commands:
  1_stop_solr:
    command: cd /var/app/current & RAILS_ENV=production rake sunspot:solr:stop

I got this error (I don't know from where it is executed):

[2015-06-25T09:51:35.510Z] INFO [13207] - [CMD-AppDeploy/AppDeployStage0/EbExtensionPreBuild/Infra-EmbeddedPreBuild/prebuild_0_My_First_Elastic_Beanstalk_Application/Command 1_stop_solr] : Activity execution failed, because: rake aborted! couldn't find HOME environment -- expanding `~'

EDIT 1 (following jay's comment): The indexation process is done when I save the objects.

Here is an example for an entity (and where it fails) :

class Document < ActiveRecord::Base

  # .....

  # SolR entity 
  searchable do
    text :title, :description, :tags 
    integer :user_id
  end 

  # .....

end

**EDIT 2: **

James' answer doesn't fix the problem but, I realize that manually on my EC2 instance, i can just run the 2 following lines :

chmod 777 -R solr/ tmp/ log/
RAILS_ENV=production rake sunspot:reindex"

I tried to use James' link to create a post-deployment script, and the chmod works well but when I add the reindex command into the file, the deployment fails with this error :

[2015-07-07T16:26:25.509Z] INFO  [20402] - [CMD-AppDeploy/AppDeployStage1/AppDeployPostHook/99_restart_delayed_job.sh] : Activity execution failed, because: rake aborted!
Could not find rake-10.4.2 in any of the sources 
/var/app/current/config/boot.rb:3:in `<top (required)>'
/var/app/current/config/application.rb:1:in `<top (required)>'
/var/app/current/Rakefile:4:in `<top (required)>'

Also, if i try to run the command manually (after the post-deployment script chmod), it fails with 500 errors on each item to reindex. So i need to kill the solr server, start and then reindex.

It's really painful :)

like image 658
Julian Le Calvez Avatar asked Jun 25 '15 10:06

Julian Le Calvez


People also ask

When should you not use Elastic Beanstalk?

Elastic Beanstalk isn't great if you need a lot of environment variables. The simple reason is that Elastic Beanstalk has a hard limit of 4KB to store all key-value pairs. The environment had accumulated 74 environment variables — a few of them had exceedingly verbose names.

What is difference between immutable and blue green deployment?

The main difference is that in the immutable update, the new instances serve traffic alongside the old ones, while in the blue/green this doesn't happen (you have an instant complete switch from old to new).

Which EB automated deployment type will ensure app performance is unaffected by a new application version deployment without additional manual administration?

Immutable – A slower deployment method, that ensures your new application version is always deployed to new instances, instead of updating existing instances.


1 Answers

I know nothing about Beanstalk, but it looks like the shell that's executing your commands doesn't have the environment variables you're expecting. Assuming this blog post on post-deployment scripts is correct, you should be able to change your setup.sh script to something like this:

 #!/usr/bin/env bash
 . /opt/elasticbeanstalk/support/envvars
 cd $EB_CONFIG_APP_CURRENT
 su -c "RAILS_ENV=production /usr/local/bin/rvm 2.2 do rake sunspot:solr:restart" $EB_CONFIG_APP_USER
like image 151
James Mason Avatar answered Oct 18 '22 04:10

James Mason