質問編集履歴
1
unicornの設定ファイルを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -241,3 +241,263 @@
|
|
241
241
|
|
242
242
|
|
243
243
|
宜しくお願いします。
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
追記(unicorn関連の設定ファイルを追記します。)
|
248
|
+
|
249
|
+
```ここに言語を入力
|
250
|
+
|
251
|
+
config/unicorn/production.rb
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
#ワーカーの数。後述
|
256
|
+
|
257
|
+
$worker = 2
|
258
|
+
|
259
|
+
#何秒経過すればワーカーを削除するのかを決める
|
260
|
+
|
261
|
+
$timeout = 30
|
262
|
+
|
263
|
+
#自分のアプリケーション名、currentがつくことに注意。
|
264
|
+
|
265
|
+
$app_dir = "/usr/share/nginx/html/current"
|
266
|
+
|
267
|
+
#リクエストを受け取るポート番号を指定。後述
|
268
|
+
|
269
|
+
$listen = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir
|
270
|
+
|
271
|
+
$pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir
|
272
|
+
|
273
|
+
#listen "/var/run/unicorn.sock"
|
274
|
+
|
275
|
+
#pid "/var/run/unicorn.pid"
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
#PIDの管理ファイルディレクトリi
|
280
|
+
|
281
|
+
#$pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir
|
282
|
+
|
283
|
+
#エラーログを吐き出すファイルのディレクトリ
|
284
|
+
|
285
|
+
$std_log = File.expand_path 'log/unicorn.log', $app_dir
|
286
|
+
|
287
|
+
$err_log = File.expand_path 'log/unicorn.log', $app_dir
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
# 上記で設定したものが適応されるよう定義
|
292
|
+
|
293
|
+
worker_processes $worker
|
294
|
+
|
295
|
+
working_directory $app_dir
|
296
|
+
|
297
|
+
#stderr_path $std_log
|
298
|
+
|
299
|
+
stderr_path $err_log
|
300
|
+
|
301
|
+
stdout_path $std_log
|
302
|
+
|
303
|
+
timeout $timeout
|
304
|
+
|
305
|
+
listen $listen
|
306
|
+
|
307
|
+
pid $pid
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
#ホットデプロイをするかしないかを設定
|
312
|
+
|
313
|
+
preload_app true
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
root = "/usr/share/nginx/html/current"
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
before_exec do |server|
|
322
|
+
|
323
|
+
ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
|
324
|
+
|
325
|
+
end
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
#fork前に行うことを定義。後述
|
332
|
+
|
333
|
+
before_fork do |server, worker|
|
334
|
+
|
335
|
+
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
|
336
|
+
|
337
|
+
old_pid = "#{server.config[:pid]}.oldbin"
|
338
|
+
|
339
|
+
if old_pid != server.pid
|
340
|
+
|
341
|
+
begin
|
342
|
+
|
343
|
+
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
|
344
|
+
|
345
|
+
Process.kill :QUIT, File.read(old_pid).to_i
|
346
|
+
|
347
|
+
rescue Errno::ENOENT, Errno::ESRCH
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
352
|
+
|
353
|
+
end
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
#fork後に行うことを定義。後述
|
358
|
+
|
359
|
+
after_fork do |server, worker|
|
360
|
+
|
361
|
+
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
|
362
|
+
|
363
|
+
end
|
364
|
+
|
365
|
+
```
|
366
|
+
|
367
|
+
```ここに言語を入力
|
368
|
+
|
369
|
+
lib/capistrano/tasks/unicorn.rb
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
#unicornのpidファイル、設定ファイルのディレクトリを指定
|
374
|
+
|
375
|
+
namespace :unicorn do
|
376
|
+
|
377
|
+
task :environment do
|
378
|
+
|
379
|
+
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
|
380
|
+
|
381
|
+
set :unicorn_config, "#{current_path}/config/unicorn/production.rb"
|
382
|
+
|
383
|
+
end
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
#unicornをスタートさせるメソッド
|
388
|
+
|
389
|
+
def start_unicorn
|
390
|
+
|
391
|
+
within current_path do
|
392
|
+
|
393
|
+
execute :bundle, :exec, :unicorn, "-c #{fetch(:unicorn_config)} -E #{fetch(:rails_env)} -D"
|
394
|
+
|
395
|
+
end
|
396
|
+
|
397
|
+
end
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
#unicornを停止させるメソッド
|
402
|
+
|
403
|
+
def stop_unicorn
|
404
|
+
|
405
|
+
execute :kill, "-s QUIT $(< #{fetch(:unicorn_pid)})"
|
406
|
+
|
407
|
+
end
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
#unicornを再起動するメソッド
|
412
|
+
|
413
|
+
def reload_unicorn
|
414
|
+
|
415
|
+
execute :kill, "-s USR2 $(< #{fetch(:unicorn_pid)})"
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
#unicronを強制終了するメソッド
|
422
|
+
|
423
|
+
def force_stop_unicorn
|
424
|
+
|
425
|
+
execute :kill, "$(< #{fetch(:unicorn_pid)})"
|
426
|
+
|
427
|
+
end
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
#unicornをスタートさせるtask
|
432
|
+
|
433
|
+
desc "Start unicorn server"
|
434
|
+
|
435
|
+
task start: :environment do
|
436
|
+
|
437
|
+
on roles(:app) do
|
438
|
+
|
439
|
+
start_unicorn
|
440
|
+
|
441
|
+
end
|
442
|
+
|
443
|
+
end
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
#unicornを停止させるtask
|
448
|
+
|
449
|
+
desc "Stop unicorn server gracefully"
|
450
|
+
|
451
|
+
task stop: :environment do
|
452
|
+
|
453
|
+
on roles(:app) do
|
454
|
+
|
455
|
+
stop_unicorn
|
456
|
+
|
457
|
+
end
|
458
|
+
|
459
|
+
end
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
#既にunicornが起動している場合再起動を、まだの場合起動を行うtask
|
464
|
+
|
465
|
+
desc "Restart unicorn server gracefully"
|
466
|
+
|
467
|
+
task restart: :environment do
|
468
|
+
|
469
|
+
on roles(:app) do
|
470
|
+
|
471
|
+
if test("[ -f #{fetch(:unicorn_pid)} ]")
|
472
|
+
|
473
|
+
reload_unicorn
|
474
|
+
|
475
|
+
else
|
476
|
+
|
477
|
+
start_unicorn
|
478
|
+
|
479
|
+
end
|
480
|
+
|
481
|
+
end
|
482
|
+
|
483
|
+
end
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
#unicornを強制終了させるtask
|
488
|
+
|
489
|
+
desc "Stop unicorn server immediately"
|
490
|
+
|
491
|
+
task force_stop: :environment do
|
492
|
+
|
493
|
+
on roles(:app) do
|
494
|
+
|
495
|
+
force_stop_unicorn
|
496
|
+
|
497
|
+
end
|
498
|
+
|
499
|
+
end
|
500
|
+
|
501
|
+
end
|
502
|
+
|
503
|
+
```
|