capistranoをほんのちょっと動かしてみる③(並列制御)
(2)では複数のサーバに対し並列で実行した。 今回は並列実行数を制限して行う。
(2)から変更するファイルは下記の2ファイル。 ./config/deploy/manage.rb ./config/servers.rb
./config/deploy/manage.rbの中身。
load './config/servers.rb'
task :mkdir do
dir = '/root/test'
# 同時実行数を2サーバとし、インターバルを3秒とする
on roles( :client ), in: :groups, limit: 2, wait: 3 do
execute "mkdir -p #{dir}"
execute "ls -ld #{dir}"
end
end
./config/servers.rbの中身 ※roleに定義しているサーバを1個追加しただけ。
SSHKit::Backend::Netssh.configure do |ssh|
ssh.ssh_options = {
:user => 'root',
:password => '****',
}
end
# ホスト名がcapistranoのサーバを追加
role :client, %w{chef openvz capistrano}
実行してみる。 ※timeコマンドは実行時間計測コマンド
$ time cap manage mkdir
INFO [0c298885] Running /usr/bin/env mkdir -p /root/test on openvz
INFO [64711761] Running /usr/bin/env mkdir -p /root/test on chef
DEBUG [64711761] Command: mkdir -p /root/test
DEBUG [0c298885] Command: mkdir -p /root/test
INFO [0c298885] Finished in 0.156 seconds with exit status 0 (successful).
INFO [844bf243] Running /usr/bin/env ls -ld /root/test on openvz
DEBUG [844bf243] Command: ls -ld /root/test
DEBUG [844bf243] drwxr-xr-x 2 root root 4096 Jul 7 02:15 /root/test
INFO [844bf243] Finished in 0.030 seconds with exit status 0 (successful).
INFO [64711761] Finished in 0.372 seconds with exit status 0 (successful).
INFO [cf1ad737] Running /usr/bin/env ls -ld /root/test on chef
DEBUG [cf1ad737] Command: ls -ld /root/test
DEBUG [cf1ad737] drwxr-xr-x. 2 root root 4096 Jul 7 01:36 /root/test
INFO [cf1ad737] Finished in 0.032 seconds with exit status 0 (successful).
INFO [1d2d7b9d] Running /usr/bin/env mkdir -p /root/test on capistrano
DEBUG [1d2d7b9d] Command: mkdir -p /root/test
INFO [1d2d7b9d] Finished in 0.383 seconds with exit status 0 (successful).
INFO [c4032d51] Running /usr/bin/env ls -ld /root/test on capistrano
DEBUG [c4032d51] Command: ls -ld /root/test
DEBUG [c4032d51] drwxr-xr-x. 2 root root 4096 Jul 7 02:39 /root/test
INFO [c4032d51] Finished in 0.033 seconds with exit status 0 (successful).
real 0m7.288s
user 0m0.424s
sys 0m0.132s
実行時間は約7.3秒。 waitが実行されたのは下記の2回(のように感じた) ・2サーバ実行後 ・残りの1サーバ実行後