This page looks best with JavaScript enabled

development環境でRails + whenever + flockがうまくいかない場合の原因と対処法

 ·  ☕ 4 min read

先日Rails + wheneverでの重複起動を防止する方法を探しているとflockというものを見つけました。 いい感じに使用できそうだと思いdevelopment環境で試した際に驚くほど詰まったのでまとめておきます。

flockとは

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ man 1 flock

FLOCK(1)                                                                                                         User Commands                                                                                                        FLOCK(1)

NAME
       flock - manage locks from shell scripts

SYNOPSIS
       flock [options] file|directory command [arguments]
       flock [options] file|directory -c command
       flock [options] number

DESCRIPTION
       This utility manages flock(2) locks from within shell scripts or from the command line.

       The first and second of the above forms wrap the lock around the execution of a command, in a manner similar to su(1) or newgrp(1).  They lock a specified file or directory, which is created (assuming appropriate permissions) if it
       does not already exist.  By default, if the lock cannot be immediately acquired, flock waits until the lock is available.

       The third form uses an open file by its file descriptor number.  See the examples below for how that can be used.

OPTIONS
......

flock(2)を管理するためのものだと書いてあるのでそちらもみてみます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ man 2 flock

FLOCK(2)                                                                                                   Linux Programmer's Manual                                                                                                  FLOCK(2)

NAME
       flock - apply or remove an advisory lock on an open file

SYNOPSIS
       #include <sys/file.h>

       int flock(int fd, int operation);

DESCRIPTION
       Apply or remove an advisory lock on the open file specified by fd.  The argument operation is one of the following:

           LOCK_SH  Place a shared lock.  More than one process may hold a shared lock for a given file at a given time.

           LOCK_EX  Place an exclusive lock.  Only one process may hold an exclusive lock for a given file at a given time.

           LOCK_UN  Remove an existing lock held by this process.

       A call to flock() may block if an incompatible lock is held by another process.  To make a nonblocking request, include LOCK_NB (by ORing) with any of the above operations.

       A single file may not simultaneously have both shared and exclusive locks.

       Locks created by flock() are associated with an open file table entry.  This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using
       any of these descriptors.  Furthermore, the lock is released either by an explicit LOCK_UN operation on any of these duplicate descriptors, or when all such descriptors have been closed.

       If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock().  An attempt to lock the file using one of these file descriptors may be denied  by
       a lock that the calling process has already placed via another descriptor.

       A process may hold only one type of lock (shared or exclusive) on a file.  Subsequent flock() calls on an already locked file will convert an existing lock to the new lock mode.

       Locks created by flock() are preserved across an execve(2).

       A shared or exclusive lock can be placed on a file regardless of the mode in which the file was opened.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

簡単に言えば、自動でロックを管理してくれるもののようです。簡単なサンプルを試してみましょう。

1
$ flock -x /tmp/test-hello -c "echo hello terminal1 sleep 60"

とし、別ターミナルで

1
$ flock -x /tmp/test-hello -c "echo hello terminal2 sleep 60"

としてみると、terminal1の方で hello terminal1 は即座に出力されますが、terminal2の方では何も出力されずに固まったような状態になります。そしてterminal1の方のプロンプトが表示されると、terminal2でも hello terminal2 が表示されました。これはcronの多重起動防止に使えそうですね。

wheneverでflockを使用する

ということでRailsでのcronの設定を自動化してくれるwheneverとflockを併用して定期実行の多重起動を防止してみます。設定ファイルはこんな感じです。

1
2
3
4
5
job_type :app_command, 'cd :path && :task :output'

every 1.minute do
  app_command 'flock -xn /tmp/test-task -c "bundle exec rails runner Rails.logger.debug\(RUBY_VERSION\)"'
end

毎分rubyのバージョンをlogに出力します。実際に試すと 3.0.0 を出力しました。うまくいったように見えますが、出力されるのは1回きりでその後の出力はありませんでした。

調査

ということで調査です。まずはcronが動いているかを確認します。

1
2
$ service cron status
[ ok ] cron is running.

cronは問題なく動いていそうです。ではcronで叩いているコマンドを手で叩いてみます。

1
$ flock -x /tmp/test-task -c "bundle exec rails runner Rails.logger.debug\(RUBY_VERSION\)"

返ってきません。どうやらファイルがロックされているようです。しかし1回は出力したのでRailsのプロセスがロックしているとも思えません。とりあえずはどのプロセスがロックしているかを特定していきます。

1
2
3
4
$ lslocks
COMMAND         PID   TYPE SIZE MODE  M START END PATH
(undefined)      -1 OFDLCK      READ  0     0   0
ruby             21  FLOCK   3B WRITE 0     0   0 /tmp/spring-0/95e5a399c0da6cdcc48bd2d1fa033734.pid

特にロックはされていないように思えます。自分はこの後2時間くらい解決策を探して彷徨ってました。

1
2
3
4
5
6
7
$ lsof /tmp/test-task
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
ruby     21 root    3r   REG  0,119        0 538122 /tmp/test-task

$ ps aux | grep 21
root        21  0.1  1.0 288152 21848 ?        Sl   02:27   0:00 spring server | myapp | started 5 mins ago
root       200  0.0  0.0   4836   876 pts/0    S+   02:32   0:00 grep 21

発見しました。springサーバーが何故かロックしていました。

解決策

springサーバーがロックしてしまうならcronでのコマンドでは起動しないようにしてしまいましょう。

1
2
3
4
5
6
7
job_type :app_command, 'cd :path && :task :output'

env 'DISABLE_SPTINRG', '1'

every 1.minute do
  app_command 'flock -xn /tmp/test-task -c "bundle exec rails runner Rails.logger.debug\(RUBY_VERSION\)"'
end

これだけです。終わってみればたったの1行でした。またspringサーバーが原因なので基本的にはdevelopment以外では発生しないとは思います。

まとめ

ロックが原因だと思われることで詰まった時は lslocks だけでなく lsof でも確認するようにしましょう。

Share on

ippachi
WRITTEN BY
ippachi
Software Developer