Rails file_store cache clears when redploying - ruby-on-rails

I am constantly developing my rails app and deploying about once every 1 or 2 weeks. I have a lot of content that I would like to be cached for quite awhile. I switched to using rails file_store for caching since the other caching options store the cache in memory which goes away when I have to restart passenger. The problem I am having is that every time I deploy an update, I loose my cache anyway even though it is file based. Is there a way to prevent the cache from clearing when you deploy an update for a rails app? I am using capistrano for deployment and git for version control.

Make a directory under the shared directory to hold the cache. In your deploy.rb, make a symlink to the new directory where the old cache directory used to be. For example, here is a simplified version of how I symlink the log directory:
namespace :deploy do
desc "Re-link log files"
task :link_logs, :roles => :app do
run "ln -nfs '#{shared_path}/log' '#{current_path}/log'"
end
end
# Activate post-deploy re-linking
after 'deploy:symlink', 'deploy:link_logs'
Now, whenever I deploy, my logs are continuous across deploys and stored in:
*/path/to/deploy_dir/shared/log*

Related

How can I deploy but not symlink/restart using Capistrano 3?

Capistrano v2 had two helpful tasks: cap deploy:update_code would do a full deployment to a new releases/ directory, but not change the current symlink or start/restart the server (so the server keeps running the current version without interruption). And cap deploy:update did the same thing plus changing the current symlink, but didn't start/restart the server. These were useful to shake out issues e.g. with asset compilation, before doing a real deploy.
Those two "update" tasks are gone in Capistrano v3. Is there an equivalent way to do a full deploy without changing the current symlink or restarting the server?
A custom task list this should do it:
task :deploy_without_symlink do
set(:deploying, true)
%w{ starting started
updating updated }.each do |task|
invoke "deploy:#{task}"
end
end
You can look at the code here: https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/framework.rake#L58 to see what deploy triggers. And the Publishing task per https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake#L38 is what changes the symlinks. So by omitting everything afterwards, you get what you are looking for.

How to setup the development environment using cap

I would like to run cap development deploy:setup and cap development deploy to setup the dev environment for my rails app. In order to do this, I will have to remove the project folder from the remote machine, is there a way to automate this in some fashion using cap.
I basically want to remove the app folder in remote machine before I do a full deploy.
The Capistrano app folder is structured like this:
releases - Contains separate folders (named using commit IDs or timestamps) for each deployment made so far
current - Linked to the latest deployment folder under releases
shared - Shared logs, gem files, and tmp which are used between deployments
Now do you really want to clean the entire app folder? That could be dangerous since you'll lose logs.
current is just a symbolic link, it points to the latest release only.
If you want to clean up old releases, check this answer. You can set keep_releases to 1.
If you want to delete the shared folder as well, then you have to write your own Capistrano hook as #Strik3r mentioned.
before 'deploy:setup', 'removecode'
task :removecode do
run "rm -rf <path>", :shell => fetch(:rvm_shell)
end
add this code in to your deploy.rb file, this will call before deploy:setup
in this way you can create a task and do what ever you want to do

Apache2, Git, Capistrano & Rails - creating symlinks

I'm sort of stuck with adding symlinks to my app on the server. I currently have the following in .gitignore:
/non-public/system/uploads/*
I basically don't want Git to store the contents of the upload directory. So far so good.
On my server, inside my deploy.rb, I have the following:
namespace :customs do
task :symlink, :roles => :app do
run <<-CMD
ln -nfs #{shared_path}/system/uploads #{release_path}/non-public/system/uploads
CMD
end
end
after "deploy:symlink","customs:symlink"
after "deploy", "deploy:cleanup"
I want to create a symlink after each deployment for the uploads directory, but I keep getting a failed error message because the non-public/system/uploads directory doesn't exist in the git repository in the first place.
I've verified this by taking a look at the repository, and the structure /non-public/system/uploads doesn't exist because I have that set in .gitignore to ignore it.
I've looked at the Git wiki and it doesn't track directories, so I must be missing something. How do other developers symlink the uploads directory with their server?
What I'll usually do on my cap deploys is to create the directories, by doing a basic
set :deploy_to, "/this/dir"
run "mkdir -p #{deploy_to}/then/more/dirs"
after "deploy:symlink"
namespace :deploy....
....
....
then provide the run code to do some symlinks either on an after or whatever. This probably not optimal for all situations, but for the simple stuff it usually gets the job done.

capistrano put() and upload() both failing

With capistrano, I am deploying a Rails application from Mac OS X 10.5 to CentOS 5.2
Note that deploy.rb and the server environment have not changed in over a year.
There is a task within our deploy.rb file called upload:
put(File.read( file ),"#{shared_path}/#{filename}", :via => :scp)
This fails each and every time with the following exception:
No such file or directory - /srv/ourapp/releases/20100104194410/config/database.yml
My local copy of config/database.yml is failing to upload properly. I've verified it's not our internet connection, as this happens on three different connections and two different systems.
I've also tried to swap out put() for upload() but get the same result; also, dropping :via => :scp, and/or trying to force :sftp instead similarly fails.
Relevant info:
$ cap -V
Capistrano v2.5.10
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9.6.0]
If I understand your question correctly, it sounds like Capistrano is successfully uploading the files, but Rails is failing to start because it can't find the deploy.yml file. This might be happening during the Capistrano deploy as part of the deploy:restart task, making it look like a Capistrano error.
Based on the information you've given Capistrano is uploading the file to /svr/ourapp/shared/ and Rails is almost definitely looking for it in /svr/ourapp/releases/20100104194410/config/.
If that's the case, what you'll need to do is create a task which symlinks the shared database file to the expected location then add a hook so that task will be run after finalize_update. For instance:
task :symlink_database do
run "ln -s #{shared_path}/database.yml #{latest_release}/config/database.yml"
end
after 'deploy:finalize_update', :symlink_database
namespace :deploy do
task :upload_settings, :roles => :app do
run "mkdir -p #{shared_path}/config/"
top.upload "config/database.yml", "#{shared_path}/config/database.yml", :via => :scp
end
task :symlink_yml, :roles => :app do
run "ln -sf #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
end
after 'deploy:setup', 'deploy:upload_settings'
after 'deploy:update_code', 'deploy:symlink_yml'
This is purely from a server-side view, but have you checked to make sure that the user you're using to upload has proper permissions for the directory?
Doing a recursive change for the user (or group) depending on your server environment should fix this.
chown -R user_name_here /srv/ourapp/releases/
chgrp -R group_name_here /srv/ourapp/releases/
You might also want to clean up any code repositories e.g. git gc or svn cleanup. As well as updating any symbolic links.
The "No such file or directory" error occurs when you are trying to copy a file to a destination path that does not exist. As far as I know, the put and upload methods of Capistrano merely attempt to transfer the file, but do not make the required path. Is it possible that the config/ directory, or even the shared_path itself, has not been created when you are trying to upload this file?

Deploy from Git using Capistrano without a hard reset?

I've an issue at the moment where we are running a CMS within a site
(browsercms) that lets the user upload files. However, every time I
do a deploy Capistrano runs a hard reset thus nuking any uploaded
files.
Does anyone have any suggestions as to how to prevent the hard reset,
and just do a pull, or a way of moving the uploaded files elsewhere,
without having to change the application code?
This might not be the right approach.
You should include your 'images' folder in your .gitignore and symlink the $current_release/images folder to $shared/images.
This may be done automatically on every deployment if you put in your deploy.rb:
task :link_imgs do
run "ln -s #{shared_path}/photos #{release_path}/photos"
end
after "deploy:update_code", :link_imgs
I've done the same with my CMS and it works like a charm
This doesn't quite meet your criteria of "without having to change the application code".
However after running into a similar issue I shifted my uploaded image from /public/images to /public/system/images the /public/system directory is not 'versioned' by each capistrano deployment so the images survive.
Could it be the capistrano 'versioning' causing the problem (instead of a git reset)?
cap deploy calls deploy:update and deploy:restart
deploy:update makes the versioning, copying stuff
deploy:restart does the true restart, overload it at your convenince, usually in your config/deploy.rb file
namespace :deploy do
desc "Softly restart the server"
task :restart, :roles => :app, :except => { :no_release => true } do
my_own.restart_recipe
end
end

Resources