2015年3月27日金曜日

【ActiveRecord】update_allでのwhere, limitの指定方法

使うたびに忘れるのでメモ

複数のレコードを一括で更新する場合ActiveRecordではupdate_allを使う。そのとき対象のレコードをwhereで指定したり、limitで件数をしぼったりするときには、


Member.update_all("lock=0").where("lock=1").limit(100)

っとしてしまいがちだが、こうではなくて、


Member.update_all("lock=0","lock=1", :order=>"last_update", limit: 100 )


こうする。

2015年3月15日日曜日

Nginxがerror_pageで設定した内容を返さない件

サイトのメンテナンスのためnginxがメンテナンスページを返すようにしようとググってみたら、サンプルがたくさんでてきたのでその通りにやってみたが、なぜか設定したメンテナンスページが表示されない。Nginxが吐く503のレスポンスの文字列が出てしまう。

調べてみるとerror_pageディレクティブの設定が反映されないのはNginxのバグではないかとのこと(http://serverfault.com/questions/326877/nginx-error-page-directive-is-silently-ignored)。


returnをlocationの中にいれると正常に表示された。


server {
listen 80;
server_name lw.aroundtheclock.jp;

error_page 503 /maintenance/maintenance.html;
location /maintenance/ {
root /var/www/html/;
}

location / {
return 503;
#proxy_pass http://localhost:3010;
}
}


余談

/tmp/do_maintenance があればメンテナンスページを返すようにnginxの設定するサンプルがたくさんでてきた。リクエストのたびにファイルの存在を確認しにいくのってどうなのか思う(きっと対した負荷じゃないんだろうけど)。今回は個人サイトなのでメンテナンスページなんて出さなくてもいいくらいだし、今後も滅多に使わないだろうからそこらへんの仕組みはいれず使うときはconfで設定をコメントアウトを解除してreloadするつもり。

2015年3月14日土曜日

Sinatora + bootstrap 「Uncaught SyntaxError: Unexpected token var」の対処方法

Sinatora + bootstrap でWebアプリを作って、Capistranoでデプロイしたところ

ブラウザのconsoleに
Uncaught SyntaxError: Unexpected token var
というエラーがでてjsが正しく動作しなかった。

調べるとbootstrap.jsの中で発生していた。
どうもSinatraのjsの圧縮が原因のよう。


app.rbのassets句でjsの圧縮方法を指定できるが、下記のようにそこを「js_compression :closure, :level=>"WHITESPACE_ONLY"」にしたら直った。

assets do
serve '/javascripts', from: 'assets/javascripts'
serve '/stylesheets', from: 'assets/stylesheets'

js :application, [
'/javascripts/jquery-*.js',
'/javascripts/jquery.zclip.js',
'/javascripts/*.js',
'/javascripts/*.coffee'
]

css :application, [
'/stylesheets/*.css',
'/stylesheets/*.scss'
]

js_compression :closure, :level=>"WHITESPACE_ONLY" # :jsminだとbootstrap.jsないでエラーが起こる。closureにしても "WHITESPACE_ONLY"指定しないとだめ。
#js_compression :jsmin
css_compression :sass
end


デフォルトはjsminだが、圧縮なしのオプションはない模様。

2015年3月8日日曜日

Capistrano3でSinatraをデプロイ

Railsでは使っていたがSinatraは初めてなのでメモ。

capistrano3インストール



Gemfileに以下のように書いてbundle install。


group :development do
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails', '~> 1.1.3'
end



その後

$ bin/cap install

これでCapfileなどが作られる。

capistrano task設定

capistranoのタスクは http://qiita.com/satococoa/items/9b0cc416ffc042680b9b を参考に以下のようにして、
lib/capistrano/tasks/unicorn.rake に保存。

namespace :deploy do
task :environment do
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
set :unicorn_config, "#{current_path}/config/unicorn/unicorn_#{fetch(:rails_env)}.rb"
set :unicorn_path, "#{current_path}/bin/unicorn"
end

def start_unicorn
within current_path do
execute "#{fetch(:unicorn_path)} -c #{fetch(:unicorn_config)} -E #{fetch(:rails_env)} -D"
end
end

def stop_unicorn
execute :kill, "-s QUIT $(< #{fetch(:unicorn_pid)})" end def reload_unicorn execute :kill, "-s USR2 $(< #{fetch(:unicorn_pid)})" end def force_stop_unicorn execute :kill, "$(< #{fetch(:unicorn_pid)})" end desc "Start unicorn server" task :start => :environment do
on roles(:app) do
start_unicorn
end
end

desc "Stop unicorn server gracefully"
task :stop => :environment do
on roles(:app) do
stop_unicorn
end
end

desc "Restart unicorn server gracefully"
task :restart => :environment do
on roles(:app) do
if test("[ -f #{fetch(:unicorn_pid)} ]")
reload_unicorn
else
start_unicorn
end
end
end

desc "Stop unicorn server immediately"
task :force_stop => :environment do
on roles(:app) do
force_stop_unicorn
end
end
end
end


Capfile

Capfileはとりあえずbundle installしてくれるうにcapistrano/bundlerはONにした。
assets_compileするにはcapistrano/rails/assetsもONに。
あと先ほどのtaskファイルを読み込むように。

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }


config/deploy.rb


# config valid only for current version of Capistrano
lock '3.4.0'

set :application, '#{app_name}'
set :repo_url, '#{repo_url}'

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '#{deploy_path}'
set :bundle_flags, "--binstubs" # bundleにbinstubsオプションをつけている。

# Default value for :scm is :git
set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

# Default value for :pty is false
# set :pty, true

# Default value for :linked_files is []
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push( 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 10

namespace :deploy do

after :restart, :clear_cache do
on roles(:app), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
after 'deploy:publishing', 'deploy:restart' # これ入れないとdeployした後にunicornに更新が読みこまれない。

end


config/deploy/production

こちらは

set :rails_env, "production"

を入れるのと、assets_compileはroleがwebのときにしか実行されないので、
webサーバをかねる場合はwebのroleをつける必要がある。

またその場合、Railsでアセットのルーティングを処理しなければいけないので、
config/environments/production.rbでconfig.serve_static_assetsをtrueに設定する。
これをやらないと画像、css,jsなどがNot Foundになってしまう。




これで

$ bin/cap production deploy

で本番リリースできるようになります。


更新履歴

2015/06/20 assets_compileについての記載追加。

文字列修正サービス もでぃもでぃ リリース

データの調査をしているとき、重複を消去したり、前後の空白を消したり、ソートしたりと文字列の整形をすることがあると思いますが、どうされてるでしょうか。エクセルでがんばったりすることも多いでしょう。さくらエディタで正規表現使えば結構いけますが、重複はどうにもできません。私の場合Railsのconsoleで配列に入れて、、、なんてよくやります。
が毎回それも面倒なんでWebアプリにしました。

文字列修正サービス もでぃもでぃ

データ整形の際にご利用ください。