Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Last active May 10, 2019 03:37
Show Gist options
  • Save jhjguxin/40e555d8148c90125493 to your computer and use it in GitHub Desktop.
Save jhjguxin/40e555d8148c90125493 to your computer and use it in GitHub Desktop.
use qiniu to speed up your site asset file load time
set :linked_files, %W{
... config/app.god config/qrsync.json
config/application.yml
}
# you should remove bin from linked_dirs config
# https://github.com/capistrano/bundler/issues/45
set :linked_dirs, %w{config/unicorn log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'cp assets/images to public/assets'
after :restart, :cp_assets do
on roles(:app), in: :sequence, wait: 5 do
execute :cp, '-R', release_path.join('app/assets/images/*'), release_path.join('public/assets/')
end
end
desc 'cp assets/images to public/assets'
after :cp_assets, :auto_sync_cdn do
on roles(:app), in: :sequence, wait: 5 do
if fetch(:qiniu_cdn)
invoke 'deploy:sync_assets_to_qiniu'
else
execute :echo, "Pls, enable qiniu_cdn flag within STAGE file first"
end
end
end
after :publishing, :restart
desc 'upload setup_config for application'
task :upload_config do
on roles(:web), in: :sequence, wait: 5 do
fetch(:linked_files).each do |file_path|
unless test "[ -f #{shared_path}/#{file_path} ]"
upload!("#{file_path}", "#{shared_path}/#{file_path}", via: :scp)
end
end
end
end
desc 'sync assets file to qiniu'
task :sync_assets_to_qiniu do
on roles(:web), in: :sequence, wait: 5 do
within current_path do
SSHKit.config.command_map[:qrsync] = "#{release_path}/bin/qrsync"
if test "[ -f #{shared_path}/config/qrsync.json ]"
execute "qrsync", "#{shared_path}/config/qrsync.json"
else
execute :echo, "no qrsync config been detected."
end
end
end
end
end
# config/deploy/development.rb
set :qiniu_cdn, true
# config/deploy/evirontments/production.rb
Rails.application.configure do
# config.action_controller.asset_host = "http://assets.example.com"
if ENV["asset_host"]
config.action_controller.asset_host = ENV["asset_host"]
end
end

Qiniu CDN with Rails

Presentation

this post will intro how ikcrm try to use qiniu cdn speed site's asset file.

references:

depend tools;

  • capistrano
  • figaro
  • rails
  • qrsync

step by step

  • ensure figaro, capistrano within your Gemfile, and runed bundle install

  • place qrsync execute file within rails_root_path/bin

  • create qrsync file config file within rails_root_path/config create file rails_root_path/config/qrsync.json create file rails_root_path/config/qrsync.json.example

  • install figaro by type figaro install ensure file rails_root_path/config/application.yml ensure file rails_root_path/config/application.yml.example

production:
  asset_host: "http://assets.example.com"
  • update .gitignore file
config/qrsync.json
# Ignore application configuration
/config/application.yml
  • update deploy.rb file to define task to sync asset file

  • config qiniu_cdn flag within deploy stage file

  • try to load asset_host from figrao

tips

  • 多个项目使用相同的 七牛 bucket 做 cdn 是个坑, 因此最好 每个项目使用单独的 bucket,t 同时因为 rails 的资源文件 是有 签名串 的, 因此理论上同一个项目不同 stage 的 资源文件放在一个 bucket 里面是可行的
{
"src": "/deploy_to_path/current/public/assets",
"dest": "qiniu:access_key=xxx=xxxxxx&bucket=vcl-ikcrm&key_prefix=assets/&threshold=1",
"deletable": 0,
"debug_level": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment