【Gearman学習ノート】耐久化記憶キュー

7086 ワード

1.はじめに
ここは公式サイトの文書です。http://gearman.org/manual/job_server/璖persistent_queuesさんは、話がよく分かりませんので、自分でまとめました。
テスト環境:
Linux-gentoo #1 SMP x86_64 Intel(R) Xeon(R) CPU E5645 @ 2.40GHz GenuineIntel GNU/Linux
2.なぜ持久化しますか?
公式サイトから答えを探しています。
Inside the Gearman job server,all job queues are stored in memory.This means if a server restorts or crases with pending jobs,ウィウィウィll be lost and arare never run by a woker.Persistetetetet queeues werereadded to allolow background jobs to to be stored inineeeeeternal durereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e lililililililililililililililililive bebebetwewewewewewewewewewewewewewewewewenenenenegoes away、the client can detect this and restart the foreground job somewhere else(or report an error back to the original caler).Background jobs on the other haveのatached client and are mpsily expected to mitten.
ギアスマンのjob serverの作業キューはメモリに保存されています。これはサーバが処理を待つタスクがある時に再起動したり、あたごがあると、これらのタスクが失われることを意味します。長期化された記憶キューは、バックグラウンドタスクの追加を許可し、外部の永続的なキュー(たとえば、mysqlデータベース)に格納することができます。
3.ギアスの持続化配置プロセス
私はmysqlを使ってミッションを長期化して保存します。
まず、表を作る:
create database gearman
   create table `gearman_queue` (
   `unique_key` varchar(64) NOT NULL,
   `function_name` varchar(255) NOT NULL,
   `priority` int(11) NOT NULL,
   `data` LONGBLOB NOT NULL,
   `when_to_run` INT, PRIMARY KEY  (`unique_key`)
)
注:geasmaanサポートの持続的な展開にはdrizle、mysqlがあります。 memcache、postgre、sqliteなど
(私は直接にギアスのソースから貼り付けました):
 case ${PERSISTENT:-none} in
      drizzle)
        ;;
      mysql)
        GEARMAND_PARAMS="${GEARMAND_PARAMS} -q mysql"
            [ ${PERSISTENT_HOST}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --mysql-host=${PERSISTENT_HOST}"
            [ ${PERSISTENT_USER}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --mysql-user=${PERSISTENT_USER}"
            [ ${PERSISTENT_PASS}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --mysql-password=${PERSISTENT_PASS}"
            [ ${PERSISTENT_DB}     ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --mysql-db=${PERSISTENT_DB}"
            [ ${PERSISTENT_TABLE}  ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --mysql-table=${PERSISTENT_TABLE}"
            [ ${PERSISTENT_PORT}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --mysql-port=${PERSISTENT_PORT}"
        ;;
      memcache)
        [ ${PERSISTENT_SERVERLIST} ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} -q libmemcached --libmemcached-servers=${PERSISTENT_SERVERLIST}"
        ;;
      postgre)
        GEARMAND_PARAMS="${GEARMAND_PARAMS} -q libpq"
            [ ${PERSISTENT_HOST}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --libpq-host=${PERSISTENT_HOST}"
            [ ${PERSISTENT_USER}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --libpq-user=${PERSISTENT_USER}"
            [ ${PERSISTENT_PASS}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --libpq-password=${PERSISTENT_PASS}"
            [ ${PERSISTENT_DB}     ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --libpq-dbname=${PERSISTENT_DB}"
            [ ${PERSISTENT_PORT}   ] && GEARMAND_PARAMS="${GEARMAND_PARAMS} --libpq-port=${PERSISTENT_PORT}"
            [ ${PERSISTENT_TABLE}  ] && ewarn "Libpq doesn't recognise 'table' parameter."
            [ ${PERSISTENT_SOCKET} ] && ewarn "Libpq doesn't recognise 'socket' parameter. If no host is set, it automatically falls back to a socket."
        ;;
      tokyocabinet)
        GEARMAND_PARAMS="${GEARMAND_PARAMS} -q libtokyocabinet --libtokyocabinet-file=${PERSISTENT_FILE}"
        ;;
      sqlite)
        GEARMAND_PARAMS="${GEARMAND_PARAMS} -q libsqlite3 --libsqlite3-db=${PERSISTENT_FILE}"
        ;;
          none)
            ;;
      *)
            eerror "Wrong persistent queue store setting in /etc/conf.d/gearmand."
        return 1
        ;;
    esac
一般的に、geasmaanのプロファイルは/etc/init.d/ディレクトリの下に置いてあります。ここに置いてあります。
設定ファイル:(ここのPERSISTENT=「mysql」に注意)
# /etc/conf.d/gearmand: config file for /etc/init.d/gearmand

# Persistent queue store
# The following queue stores are available:
# drizzle|memcache|mysql|postgre|sqlite|tokyocabinet|none
# If you do not wish to use persistent queues, leave this option commented out.
# Note that persistent queue mechanisms are mutally exclusive.
PERSISTENT="mysql"

# Persistent queue settings for drizzle, mysql and postgre
#PERSISTENT_SOCKET=""
PERSISTENT_HOST="localhost"
PERSISTENT_PORT="3306"
PERSISTENT_USER="gearman"
PERSISTENT_PASS="your-pass-word-here"
PERSISTENT_DB="gearman"
PERSISTENT_TABLE="gearman_queue"

# Persistent queue settings for sqlite
#PERSISTENT_FILE=""

# Persistent queue settings for memcache
#PERSISTENT_SERVERLIST=""

# General settings
#
# -j, --job-retries=RETRIES   Number of attempts to run the job before the job
#                             server removes it. Thisis helpful to ensure a bad
#                             job does not crash all available workers. Default
#                             is no limit.
# -L, --listen=ADDRESS        Address the server should listen on. Default is
#                             INADDR_ANY.
# -p, --port=PORT             Port the server should listen on. Default=4730.
# -r, --protocol=PROTOCOL     Load protocol module.
# -t, --threads=THREADS       Number of I/O threads to use. Default=0.
# -v, --verbose               Increase verbosity level by one.
# -w, --worker-wakeup=WORKERS Number of workers to wakeup for each job received.
#                             The default is to wakeup all available workers.
GEARMAND_PARAMS="-L 127.0.0.1 --verbose=DEBUG"
そして、geasmaanのスタートshellでソースを見ます。
 ebegin "Starting ${SVCNAME}"
        start-stop-daemon --pidfile /var/run/gearmand/gearmand.pid --start \
        --exec /usr/sbin/gearmand -- --pid-file=/var/run/gearmand/gearmand.pid \
        --user=gearmand --daemon \
        --log-file=/var/log/gearmand/gearmand.log ${GEARMAND_PARAMS}
 eend $?
geasmaanを起動したら、プロセスを確認します。
ps aux | grep gearman
に、geasmaanとmysqlデータベースの接続を確立する語句が見られます。
gearmand 16746  0.0  0.3 423380  2176 ?        Ssl  02:54   0:00 /usr/sbin/gearmand --pid-file=/var/run/gearmand/gearmand.pid --user=gearmand --daemon --log-file=/var/log/gearmand/gearmand.log -L 127.0.0.1 --verbose=DEBUG -q mysql --mysql-host=localhost --mysql-user=gearman --mysql-password=DJW55RbdvazpmLuN --mysql-db=gearman --mysql-table=queue --mysql-port=3306
4.参考
1.http://gearman.org/manual/job_server/璖persistent_queues
2.http://www.jaceju.net/blog/archives/1211/
3.http://blog.sina.com.cn/s/blog_54 ef 3989010118 l 84.