LoginSignup
0
1

More than 5 years have passed since last update.

コンピュータ将棋ソフトとの対局サーバーを立てよう<その6>

Last updated at Posted at 2017-03-09

さくらVPSのお試し期間は 2週間なんだが、まだ プログラムの常駐 ができていない。

で、さっさと最優先で プログラムの常駐 が可能かどうか 調べた方がいいと思うんだが、
その他の可能性を潰していくのに 記事<その1>~<その5> までかかった。

前回の記事 : http://qiita.com/muzudho1/items/8f940f8b4c679c23a174

やりたいこと

プラン2

  • コンピュータ将棋プログラム を常駐させる
  • ブラウザでのページ・アクセスの度にPHPは 指し手ブローカー三河屋 を起動して、キュー待ち させ、コンピューター将棋プログラムに 指し手のお伺いを立てる
  • PHPは、指し手ブローカー三河屋 が持ってきた指し手を PHPで出力する

プラン1

  • コンピュータ将棋プログラム を常駐させる
  • ゲームサーバー を常駐させる
  • ゲームサーバーは コンピューター将棋プログラム と局面検討ができるものとする
  • ブラウザでのページ・アクセスの度にPHPは 無限ループに入り ゲームサーバーにプロセス間通信、メッセージキューに指し手が入っていれば 無限ループを出て 指し手を PHPで出力する

で、プラン1は それができないという話しが えんえんと 記事<その1>~<その3> ぐらいに書いてあったのだった。

残ってるのは プラン2。

調べてみる。

コンピュータ将棋プログラムが 常駐できないのではない

PuTTYで遠隔ログインし、PHPで無限ループを書いて バックグランド実行しても ログアウトすると ジョブは消えてしまう。

そこで Cron を使って 無限ループPHP を実行するんだが、すぐ S (停止)している。

Apache は常駐している。
PuTTY でセッション・ログアウトしても ブラウザで http://★.★.★.★/index.php とでも打てば画面は出てくる。

常駐はできるようだ。
どうやって常駐するのか。

Windows 10 だと タスクバーの「何でも聞いてください」と書いてあるテキストボックスに 「コンピューターの管理」と打ち込んで [サービスとアプリケーション] - [サービス] と進んでいけば、常駐しているプログラムの一覧が出てくる。

Ubuntu16.04 だと

ps aux

と打ち込めば プロセスの一覧が見れるんだが、これはアプリケーションの一覧ではない。

# ps aux | grep apache

と打ち込むと例えば

www-data  2776  0.0  0.2 352548  2496 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root     11959  0.0  0.0 351884    76 ?        Ss   Mar07   0:34 /usr/sbin/apache2 -k start
www-data 11965  0.0  0.0 352296   148 ?        S    Mar07   0:14 /usr/sbin/apache2 -k start
root     19418  0.0  0.0  12936   980 pts/2    S+   03:16   0:00 grep --color=auto apache
www-data 29283  0.0  0.1 352164  2004 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29284  0.0  0.0 352164   556 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29285  0.0  0.1 352164  1944 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29286  0.0  0.0 352164     0 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29287  0.0  0.1 352164  1776 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29318  0.0  0.2 352164  2508 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 31812  0.0  0.1 352164  1116 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start

と出てきて、このうち「color=auto apache」というのは apacheの文字色を赤色にしましょう、という今叩いたばかりのコマンドだから無視するとして、

/usr/sbin/apache2 -k start

というコマンドで実行されたプロセスが 稼働していることが分かる。

しかしよく見ると みんな S だな。

www-data 15221  197 27.6 712928 281056 pts/3   Rsl+ Mar09 458:47 ./release

これなんか、ブラウザでアクセスして 叩き起こした コンピュータ将棋ソフトの 技巧 のプログラムなんじゃないか?

Rsl+ というのがどういう状況なんだ。

「psコマンドまとめ」 (Qiita)
http://qiita.com/shell/items/68ed71a7f018e5688f73

「S」はストップじゃなくて スリープ なのか……。

# kill 15221

ということは。

# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  37448  1872 ?        Ss   Mar04   0:56 /lib/systemd/systemd --system --deserialize 19
root         2  0.0  0.0      0     0 ?        S    Mar04   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Mar04   0:03 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    Mar04   1:52 [rcu_sched]
root         8  0.0  0.0      0     0 ?        S    Mar04   2:16 [rcuos/0]
root         9  0.0  0.0      0     0 ?        S    Mar04   0:18 [rcuos/1]
root        10  0.0  0.0      0     0 ?        S    Mar04   0:00 [rcu_bh]
root        11  0.0  0.0      0     0 ?        S    Mar04   0:00 [rcuob/0]
root        12  0.0  0.0      0     0 ?        S    Mar04   0:00 [rcuob/1]
root        13  0.0  0.0      0     0 ?        S    Mar04   0:05 [migration/0]
root        14  0.0  0.0      0     0 ?        S    Mar04   0:10 [watchdog/0]
root        15  0.0  0.0      0     0 ?        S    Mar04   0:07 [watchdog/1]
root        16  0.0  0.0      0     0 ?        S    Mar04   0:01 [migration/1]
root        17  0.0  0.0      0     0 ?        S    Mar04   0:01 [ksoftirqd/1]
root        19  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kworker/1:0H]
root        20  0.0  0.0      0     0 ?        S<   Mar04   0:00 [khelper]
root        21  0.0  0.0      0     0 ?        S    Mar04   0:00 [kdevtmpfs]
root        22  0.0  0.0      0     0 ?        S<   Mar04   0:00 [netns]
root        23  0.0  0.0      0     0 ?        S<   Mar04   0:00 [writeback]
root        24  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kintegrityd]
root        25  0.0  0.0      0     0 ?        S<   Mar04   0:00 [bioset]
root        27  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kblockd]
root        28  0.0  0.0      0     0 ?        S<   Mar04   0:00 [ata_sff]
root        29  0.0  0.0      0     0 ?        S    Mar04   0:00 [khubd]
root        30  0.0  0.0      0     0 ?        S<   Mar04   0:00 [md]
root        31  0.0  0.0      0     0 ?        S<   Mar04   0:00 [devfreq_wq]
root        35  0.0  0.0      0     0 ?        S    Mar04   0:02 [khungtaskd]
root        36  0.0  0.0      0     0 ?        S    Mar04   3:27 [kswapd0]
root        37  0.0  0.0      0     0 ?        SN   Mar04   0:00 [ksmd]
root        38  0.0  0.0      0     0 ?        SN   Mar04   0:11 [khugepaged]
root        39  0.0  0.0      0     0 ?        S    Mar04   0:00 [fsnotify_mark]
root        40  0.0  0.0      0     0 ?        S    Mar04   0:00 [ecryptfs-kthrea]
root        41  0.0  0.0      0     0 ?        S<   Mar04   0:00 [crypto]
root        53  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kthrotld]
root        55  0.0  0.0      0     0 ?        S    Mar04   0:00 [vballoon]
root        56  0.0  0.0      0     0 ?        S    Mar04   0:00 [scsi_eh_0]
root        57  0.0  0.0      0     0 ?        S    Mar04   0:00 [scsi_eh_1]
root        77  0.0  0.0      0     0 ?        S<   Mar04   0:00 [deferwq]
root        78  0.0  0.0      0     0 ?        S<   Mar04   0:00 [charger_manager]
root       115  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kpsmoused]
root       141  0.0  0.0      0     0 ?        S    Mar04   0:36 [jbd2/vda1-8]
root       142  0.0  0.0      0     0 ?        S<   Mar04   0:00 [ext4-rsv-conver]
root       185  0.0  0.0  29480  1012 ?        Ss   Mar04   0:47 /lib/systemd/systemd-journald
root       196  0.0  0.0      0     0 ?        S    Mar04   0:00 [kauditd]
root       234  0.0  0.0  43808     8 ?        Ss   Mar04   0:03 /lib/systemd/systemd-udevd
systemd+   417  0.0  0.0 100316    60 ?        Ssl  Mar04   0:03 /lib/systemd/systemd-timesyncd
syslog     443  0.0  0.0 256388   376 ?        Ssl  Mar04   0:39 /usr/sbin/rsyslogd -n
root       453  0.0  0.0  29872     4 ?        Ss   Mar04   0:00 /sbin/cgmanager -m name=systemd
root       469  0.0  0.0 232308   236 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root       488  0.0  0.0      0     0 ?        S<   Mar04   0:00 [kvm-irqfd-clean]
root       490  0.0  0.1 274560  1356 ?        Ssl  Mar04   1:03 /usr/lib/accountsservice/accounts-daemon
root       494  0.0  0.0  28540   880 ?        Ss   Mar04   0:13 /lib/systemd/systemd-logind
message+   515  0.0  0.0  43020   636 ?        Ss   Mar04   0:22 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-a
root       551  0.0  0.0  19592   304 ?        Ss   Mar04   0:56 /usr/sbin/irqbalance --pid=/var/run/irqbalance.pid
root       709  0.0  0.0  65512   332 ?        Ss   Mar04   0:04 /usr/sbin/sshd -D
memcache   714  0.0  0.0 340888   540 ?        Ssl  Mar04   1:32 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 160.16.83.155
mysql      715  0.1  0.0 1302112    0 ?        Ssl  Mar04  11:35 /usr/sbin/mysqld
root       757  0.0  0.0  14468     0 ttyS0    Ss+  Mar04   0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root       758  0.0  0.0  14652     0 tty1     Ss+  Mar04   0:00 /sbin/agetty --noclear tty1 linux
root      1878  0.0  0.0 232308   108 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
www-data  2776  0.0  0.2 352548  2496 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root      2866  0.0  0.0      0     0 ?        S    Mar07   0:00 [kworker/0:2]
root      2957  0.0  0.0 232308    40 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4784  0.0  0.0 232308    92 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4945  0.0  0.0 232308   104 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      5693  0.0  0.0      0     0 ?        S    Mar07   0:49 [kworker/0:0]
root      5916  0.0  0.0 232308   172 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6091  0.0  0.0 311040   312 ?        Ss   Mar07   0:49 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  6094  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
www-data  6095  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
root      6301  0.0  0.0 232308   260 ?        S    Mar08   0:10 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6856  0.0  0.0 232308   112 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      7280  0.0  0.0      0     0 ?        S    Mar07   0:18 [kworker/1:1]
root      7795  0.0  0.0 232308   112 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8303  0.0  0.0 232308   132 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8732  0.0  0.0 232308   132 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9246  0.0  0.0 232308    80 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9664  0.0  0.0 232308   272 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10278  0.0  0.0 232308    52 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10632  0.0  0.0 232308   124 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11364  0.0  0.0 232308   276 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11567  0.0  0.0 232308    72 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11959  0.0  0.0 351884    76 ?        Ss   Mar07   0:34 /usr/sbin/apache2 -k start
www-data 11965  0.0  0.0 352296   148 ?        S    Mar07   0:14 /usr/sbin/apache2 -k start
root     12122  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh /usr/sbin/rabbitmq-server
root     12129  0.0  0.0  55128     0 ?        S    Mar07   0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12130  0.0  0.0   4500     8 ?        Ss   Mar07   0:00 sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12131  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh -e /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12237  0.6  1.8 2812192 19272 ?       Sl   Mar07  25:13 /usr/lib/erlang/erts-8.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -
rabbitmq 12333  0.0  0.0   4356    72 ?        Ss   Mar07   0:16 erl_child_setup 1024
rabbitmq 12343  0.0  0.0  13676    36 ?        Ss   Mar07   0:00 inet_gethost 4
rabbitmq 12344  0.0  0.0  15796   228 ?        S    Mar07   0:01 inet_gethost 4
root     12376  0.0  0.0 232308    76 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12582  0.0  0.0 232308   164 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12714  0.0  0.0  99616   332 ?        Ss   Mar09   0:00 sshd: ★user [priv]
★user    12738  0.0  0.0  99616   524 ?        S    Mar09   0:01 sshd: ★user@pts/2
★user    12742  0.0  0.0  21484     0 pts/2    Ss   Mar09   0:00 -bash
root     12760  0.0  0.0  55780     4 pts/2    S    Mar09   0:00 sudo su -
root     12761  0.0  0.0  55128     0 pts/2    S    Mar09   0:00 su -
root     12762  0.0  0.1  21508  1348 pts/2    S    Mar09   0:00 -su
root     13304  0.0  0.0 232308   180 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     13713  0.0  0.0 232308    72 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14237  0.0  0.0 232308   100 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14600  0.0  0.0  99528   164 ?        Ss   Mar09   0:00 sshd: ★user [priv]
★user    14616  0.0  0.0  99528   584 ?        S    Mar09   0:00 sshd: ★user@notty
★user    14617  0.0  0.0  12876   256 ?        Ss   Mar09   0:00 /usr/lib/openssh/sftp-server
root     14706  0.0  0.0 232308   136 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15183  0.0  0.0 232308   204 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15608  0.0  0.0      0     0 ?        S    Mar09   0:00 [kworker/u4:2]
root     15780  0.0  0.0      0     0 ?        S    00:04   0:00 [kworker/u4:0]
root     15791  0.0  0.0 232308    80 ?        S    00:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16115  0.0  0.0 232308    80 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16778  0.0  0.0 232308    60 ?        S    01:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17059  0.0  0.0 232308    76 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17726  0.0  0.0      0     0 ?        S<   02:00   0:00 [kworker/u5:2]
root     17967  0.0  0.0 232308   256 ?        S    02:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17993  0.0  0.0 232308   252 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     18025  0.0  0.0      0     0 ?        S<   02:07   0:00 [kworker/u5:0]
root     18398  0.0  0.0      0     0 ?        S    02:24   0:00 [kworker/1:0]
root     19047  0.0  0.0 232308    76 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19194  0.0  1.3 232308 14048 ?        S    03:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19518  0.0  0.0   6004   632 ?        S    03:23   0:00 sleep 1m
root     19529  0.0  0.1  36076  1700 pts/2    R+   03:24   0:00 ps aux
root     20097  0.0  0.0 232308   212 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     21104  0.0  0.0 232308   224 ?        S    Mar08   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22197  0.0  0.0 232308   264 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22468  0.0  0.0  11332   556 ?        S    Mar07   0:04 /bin/bash ./loop8.sh
★user    22666  0.0  0.0  45240     0 ?        Ss   Mar06   0:00 /lib/systemd/systemd --user
★user    22667  0.0  0.0  63256     4 ?        S    Mar06   0:00 (sd-pam)
root     23284  0.0  0.0 232308    96 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     24410  0.0  0.0 232308   192 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     25393  0.0  0.0 232308   196 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     26650  0.0  0.0 232308   232 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
rabbitmq 27670  0.0  0.0  13848    64 ?        S    Mar06   0:11 /usr/lib/erlang/erts-8.1/bin/epmd -daemon
root     27756  0.0  0.0 232308   228 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     28734  0.0  0.0 232308   128 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
www-data 29283  0.0  0.1 352164  2004 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29284  0.0  0.1 352164  1300 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29285  0.0  0.1 352164  1944 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29286  0.0  0.1 352556  1956 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29287  0.0  0.1 352164  1776 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
www-data 29318  0.0  0.2 352164  2508 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root     29957  0.0  0.0 232308    84 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     30931  0.0  0.0 232308    40 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     31317  0.0  0.0  27720   300 ?        Ss   Mar08   0:02 /usr/sbin/cron -f
www-data 31812  0.0  0.1 352164  1116 ?        S    Mar09   0:00 /usr/sbin/apache2 -k start
root     31947  0.0  0.0 232308   180 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php

よく見ると dequeueBroker.php が たくさん 常駐している。

うーむ、一回 dequeueBroker.php 全部 kill してみるか。

# ps aux | grep dequeueBroker.php
root       469  0.0  0.0 232308   236 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      1878  0.0  0.0 232308   108 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      2957  0.0  0.0 232308    40 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4784  0.0  0.0 232308    92 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      4945  0.0  0.0 232308   104 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      5916  0.0  0.0 232308   172 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6301  0.0  0.0 232308   260 ?        S    Mar08   0:10 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      6856  0.0  0.0 232308   112 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      7795  0.0  0.0 232308   112 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8303  0.0  0.0 232308   132 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      8732  0.0  0.0 232308   132 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9246  0.0  0.0 232308    80 ?        S    Mar08   0:09 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root      9664  0.0  0.0 232308   272 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10278  0.0  0.0 232308    52 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     10632  0.0  0.0 232308   124 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11364  0.0  0.0 232308   276 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     11567  0.0  0.0 232308    72 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12376  0.0  0.0 232308    76 ?        S    Mar08   0:08 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     12582  0.0  0.0 232308   164 ?        S    Mar09   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     13304  0.0  0.0 232308   180 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     13713  0.0  0.0 232308    72 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14237  0.0  0.0 232308   100 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     14706  0.0  0.0 232308   136 ?        S    Mar09   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15183  0.0  0.0 232308   204 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     15791  0.0  0.0 232308    80 ?        S    00:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16115  0.0  0.0 232308    80 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     16778  0.0  0.0 232308    60 ?        S    01:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17059  0.0  0.0 232308    76 ?        S    Mar08   0:07 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17967  0.0  0.0 232308   256 ?        S    02:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     17993  0.0  0.0 232308   252 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19047  0.0  0.0 232308    76 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19194  0.0  1.3 232308 14048 ?        S    03:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     19588  0.0  0.0  13068   984 pts/2    S+   03:29   0:00 grep --color=auto dequeueBroker.php
root     20097  0.0  0.0 232308   212 ?        S    Mar08   0:06 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     21104  0.0  0.0 232308   224 ?        S    Mar08   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22197  0.0  0.0 232308   264 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     23284  0.0  0.0 232308    96 ?        S    Mar09   0:05 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     24410  0.0  0.0 232308   192 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     25393  0.0  0.0 232308   196 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     26650  0.0  0.0 232308   232 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     27756  0.0  0.0 232308   228 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     28734  0.0  0.0 232308   128 ?        S    Mar09   0:03 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     29957  0.0  0.0 232308    84 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     30931  0.0  0.0 232308    40 ?        S    Mar09   0:04 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     31947  0.0  0.0 232308   180 ?        S    Mar09   0:02 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php

で、何列目だけ抽出するという コマンドがあったと思うんだが、覚えてないんでググる。

「Linuxコマンドでテキストデータを自在に操る」 (orangain flavor)
http://orangain.hatenablog.com/entry/20100916/1284631280

ええっ! cat と cut があるの!?

cat でフィールド抽出する方法なんか無いよな、とか思ってたんだが。
じゃあ、こうしたらどうか?

# ps aux | grep dequeueBroker.php | cut -f 2

ダメ、アウツ。 なんにもならない。

「1つ以上の連続するスペースまたはタブ文字を区切りとして処理するには」 (cut コマンド)
https://hydrocul.github.io/wiki/commands/cut.html

こういったブログ記事は IT系の会社 に務めているとセキュリティで見れないし、ITじゃない系の会社 にいると見れたりする。 IT系とは何なのか……。
1つ1つのリスクを切っていることで万全、というわけなんだろうが、不祥事1発が広く拡散するITテクノロジーが今のIT系を作った。

じゃあ、こうか?

# ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2
469
1878
2957
4784
4945
5916
6301
6856
7795
8303
8732
9246
9664
10278
10632
11364
11567
12376
12582
13304
13713
14237
14706
15183
15791
16115
16778
17059
17967
17993
19047
19194
19795
20097
21104
22197
23284
24410
25393
26650
27756
28734
29957
30931
31947

グレート!

で、kill を for文か何かで回せないの?

「複数のプロセスをまとめてkillする」 (mikage014の日記)
http://d.hatena.ne.jp/mikage014/20100205/1265330850

で、これを読むと kill にはそれができるらしい。こうか?

kill `ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2`
# kill `ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2`
-su: kill: (19870) - No such process

# ps aux | grep dequeueBroker.php
root     19889  0.0  0.0  12936   984 pts/2    S+   03:45   0:00 grep --color=auto dequeueBroker.php

まあ、消えてくれたんじゃないか。

じゃあ、浮かむ瀬 をバックグラウンドで起動してみよう。

# cd /home/★user/shogi/ukamuse_sdt4/bin
# ./apery &
[1] 20023
# ps aux | grep apery
root     20023  0.8 14.0 1325848 142852 pts/2  Tl   03:54   0:00 ./apery
root     20033  0.0  0.0  12936   984 pts/2    S+   03:55   0:00 grep --color=auto apery

[1]+  Stopped                 ./apery

これが分からない。 Stopped と書いてるのな。

# ps aux | grep apery
root     20023  0.1 14.0 1325848 142852 pts/2  Tl   03:54   0:00 ./apery
root     20055  0.0  0.0  12936   988 pts/2    S+   03:56   0:00 grep --color=auto apery

もう1回見てみると、./apery プロセスはまだ居るようだ。 Tl は 停止中で、プロセス生成中なのだろうか。なんのこっちゃ。

では、スーパー・ユーザーと、ログイン用一般アカウントの2つから ログアウトしてみる。

# logout
There are stopped jobs.
# logout
# logout

うーん?

再度ログイン

一般ユーザーでログインし、

$ sudo su -

から

# ps aux | grep apery
root     20182  0.0  0.0  12936   988 pts/2    S+   04:02   0:00 grep --color=auto apery

./apery はいなくなっている。ログアウトすると ジョブは止まるのだろうか。
じゃあ次。

「【 nohup 】 ログアウトした後もコマンドを実行し続ける」 (ITPro)
http://itpro.nikkeibp.co.jp/article/COLUMN/20060227/230850/

# cd /home/★user/shogi/ukamuse_sdt4/bin
# nohup ./apery &
[1] 20237
root@★:/home/★user/shogi/ukamuse_sdt4/bin# nohup: ignoring input and appending output to 'nohup.out'

なんだろう。

入力は無視されます、出力は nohup.out です、ということだろうか。
入力が無視されたら困るんじゃないか?

nohup php ./apery > apery.out.log 2> apery.err.log < /dev/null &

標準入力は 何にしたらいいんだ?
コンピューター将棋ソフトは 標準入力から コマンドを受け付けるので、それを閉じてしまうと 役に立たない。

それは あとで考えるとして、 nohup で実行したコマンドが、ログアウト後も残っているのか調べてみたい。

# ps aux | grep apery      root     20436  0.0  0.0  12936   984 pts/2    S+   04:15   0:00 grep --color=auto apery
[1]+  Done                    nohup ./apery

Done って何だろうか。 nohup が Done なのだろうか?

# ps aux | grep apery
root     20447  0.0  0.0  12936   988 pts/2    S+   04:16   0:00 grep --color=auto apery

ログアウトする間でもなく 消えてなくなった。
nohup は ./apery を実行しなかったのだろうか?

じゃあ、何が nohup でバックグラウンドで実行できるんだろうか?

「PHP while文と無限ループ」 (ゼロプロ ブログ〜0からのプログラミング学習〜)
http://zeropuro.com/blog/?p=248

loop28.php

<?php
while(true){
  sleep(1);
}
# php loop28.php &
[1] 20615
# ps aux | grep php
root      6091  0.0  0.0 311040   312 ?        Ss   Mar07   0:50 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  6094  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
www-data  6095  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
root     20224  0.0  1.3 232308 14048 ?        S    04:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     20615  0.1  1.3 230188 13304 pts/2    S    04:27   0:00 php loop28.php
root     20622  0.0  0.0  12936   988 pts/2    S+   04:28   0:00 grep --color=auto php

じゃあ、ログアウトしてみる。

# logout
# logout

さっきは3回で、今回は2回だ。何の違いがあるんだろう。

# ps aux | grep php
root      6091  0.0  0.0 311040   312 ?        Ss   Mar07   0:50 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  6094  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
www-data  6095  0.0  0.0 311040     0 ?        S    Mar07   0:00 php-fpm: pool www
root     20224  0.0  1.3 232308 14048 ?        S    04:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     20615  0.0  1.3 230188 13304 ?        S    04:27   0:00 php loop28.php
root     20743  0.0  0.0  12936   984 pts/3    S+   04:33   0:00 grep --color=auto php

今度は loop28.php がいるようだ。

だったら PHP と C#、C++ がプロセス間通信できるんなら 話しは早いんだが、PHP と C# はプロセス間通信が働かなかったのは 記事<その1>~<その3>のどこかに書いたのだった。

C++ の無限ループは常駐できるだろうか?

「時間・時刻処理について(4)」 (LinuxC)
http://linuxc.info/time/time6/

だめ、Windowsで動かない。

「Sleep for milliseconds」 (stack overflow)
http://stackoverflow.com/questions/4184468/sleep-for-milliseconds

じゃあ、こうか。

// Cpp_Tamesi28.cpp : Defines the entry point for the console application.
//

#include <chrono>
#include <thread>

#if WINDOWS
#include "stdafx.h"
#endif

int main(void)
{
    for (;;) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    return 0;
}

Visual Studio 2015 C++ で プリプロセッサ定義を コマンドライン引数に付けるにはどうやるんだ?

「/D (プリプロセッサの定義)」 (MSDN)
https://msdn.microsoft.com/ja-jp/library/hhzbb5c8.aspx

メインメニューの [Project] から、プロジェクトのプロパティーのページを開き、
[Configuration Properties] - [C/C++] - [Preprocessor] - [Preprocessor Definitions]
と進んで、

_DEBUG;_CONSOLE;%(PreprocessorDefinitions)

の末尾に WINDOWS とでも付けよう。

_DEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS

ダメだ。こうか。

_DEBUG;_CONSOLE;WINDOWS;%(PreprocessorDefinitions)

これもダメ。

あー! x86 になっている。 x64 に合わせよう。
これでOK。

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>


// TODO: reference additional headers your program requires here

の、

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#if WINDOWS
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#endif


// TODO: reference additional headers your program requires here

内容を

#if WINDOWS
#endif

で囲んだ。
実行すると、入力を受け付けないコマンド・プロンプト・ウィンドウが開いた。

Ubuntu にソースを持っていって、動くだろうか?
しかし Makefile の書き方が分からない。

# cd /home/★user/shogi
# mkdir cpp_service
# mkdir tamesi28.d
# nano tamesi28.cpp
#include <chrono>
#include <thread>

int main(void)
{
        for (;;) {
                std::this_thread::sleep_for(std::chrono::seconds(1));
        }
        return 0;
}

こんなんで いいんだろうか?
g++ でコンパイルするには?

「g++」 (C++)
http://kaworu.jpn.org/cpp/g++

ファイル名を付けるだけか。

# g++ tamesi28.cpp
In file included from /usr/include/c++/5/chrono:35:0,
                 from tamesi28.cpp:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
tamesi28.cpp: In function ‘int main()’:
tamesi28.cpp:7:8: error: ‘std::this_thread’ has not been declared
   std::this_thread::sleep_for(std::chrono::seconds(1));
        ^
tamesi28.cpp:7:36: error: ‘std::chrono’ has not been declared
   std::this_thread::sleep_for(std::chrono::seconds(1));
                                    ^

std::this_thread も、std::chrono も使えないのか。

浮かむ瀬 usi.cpp

            std::this_thread::sleep_for(std::chrono::seconds(5)); // 指定秒だけ待機し、進捗を表示する。

浮かむ瀬では使っているし、g++ でコンパイルできていると思うんだが。
Makefile に仕組みがあるんだろうか?

Makefile 冒頭の3行

COMPILER = g++
#COMPILER = mpicxx
CFLAGS   = -std=c++11 -fno-exceptions -fno-rtti -Wextra -Ofast -MMD -MP -fopenmp

このあたり、関係があるんじゃないだろうか?

# g++ -std=c++11 tamesi28.cpp

実行できたようだ。

# ls
a.out  tamesi28.cpp
# ./a.out

よし、無限ループしているようだ。

[Ctrl]+[C]

で抜ける。

# ./a.out &
[1] 21556

しまった、ファイル名が検索しづらい。

# ps aux | grep a.out
root     21556  0.0  0.0   4216   364 pts/3    S    05:22   0:00 ./a.out
root     21569  0.0  0.0  12936   988 pts/3    S+   05:22   0:00 grep --color=auto a.out

動いているようだ。
ログアウトしてみる。

# logout
$ logout

ログインしなおす。

# ps aux | grep a.out
root     21556  0.0  0.0   4216   364 ?        S    05:22   0:00 ./a.out
root     21658  0.0  0.0  12936   984 pts/4    S+   05:25   0:00 grep --color=auto a.out

ふつうに居るな……。

じゃあ、C++ はプロセス間通信できるのか調べてみよう。

「RabbitMQ Tutorials」 (RabbitMQ)
http://www.rabbitmq.com/getstarted.html

うーん? RabbitMQ に C++ の説明が無い?

「Clients & Developer Tools」 (RabbitMQ)
https://www.rabbitmq.com/devtools.html

4つあるようだが、

「AMQP-CPP」 (CopernicaMarketingSoftware/AMQP-CPP)
https://github.com/CopernicaMarketingSoftware/AMQP-CPP

サンプルが書いてあるこれを調べてみるか。

AMQP-CPP-master を FileZillaでコピー。これ、RabbitMQ じゃなくて、RabbitMQ 風にした AMQP だよな。

cd /home/★user/shogi/AMQP-CPP-master
cd AMQP-CPP-master
# nano Makefile

じゃあ、

# make pure
# ls
amqpcpp.h       include  Makefile   set_cxx_norm.cmake  tests
CMakeLists.txt  LICENSE  README.md  src

実行ファイルはどこに作られたのか? あるいはライブラリはどこだ。

# make
# make install
# make pure
make -C src pure
make[1]: Entering directory '/home/★user/shogi/AMQP-CPP-master/src'
make[1]: Nothing to be done for 'pure'.
make[1]: Leaving directory '/home/★user/shogi/AMQP-CPP-master/src'

うーん。 src フォルダーの中を見ればいいのか?

実行形式のファイルが1つだけある。
libamqpcpp.so.2.6.2

これをどうやって使うのか?

サンプル・プログラムはここだろうか?

「amqpcpp/examples/」 (akalend/amqpcpp)
https://github.com/akalend/amqpcpp/tree/master/examples

srcフォルダーの中にも Makefile があるな。

/home/★user/shogi/AMQP-CPP-master/src# make pure

lib.so. という実行形式ファイルができたようだが、なんのことやら。

root@★:/home/★user/shogi/AMQP-CPP-master# make pure
make -C src pure
make[1]: Entering directory '/home/★user/shogi/AMQP-CPP-master/src'
make[1]: Nothing to be done for 'pure'.
make[1]: Leaving directory '/home/★user/shogi/AMQP-CPP-master/src'
root@★:/home/★user/shogi/AMQP-CPP-master# make install
mkdir -p /usr/include/amqpcpp
mkdir -p /usr/lib
cp -f amqpcpp.h /usr/include
cp -f include/*.h /usr/include/amqpcpp
cp -f src/libamqpcpp.so.2.6.2 /usr/lib
cp -f src/libamqpcpp.a.2.6.2 /usr/lib
ln -r -s -f /usr/lib/libamqpcpp.so.2.6.2 /usr/lib/libamqpcpp.so.2.6
ln -r -s -f /usr/lib/libamqpcpp.so.2.6.2 /usr/lib/libamqpcpp.so
ln -r -s -f /usr/lib/libamqpcpp.a.2.6.2 /usr/lib/libamqpcpp.a

もう、使えるようにセットアップしてくれてるんじゃないか? /usr/lib に。
じゃあ、ソースを書いてみよう。

/home/★user/shogi/cpp_service# nano tamesi29a7_main.cpp

tamesi29a7_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

    // create a temporary queue
    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {

        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;

        // now we can close the connection
        connection.close();
    });

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}
# g++ tamesi29a7_main.cpp
tamesi29a7_main.cpp:1:16: fatal error: ev.h: No such file or directory
compilation terminated.

違うのか。

libev

「libevのev_asyncとは何に使用するものですか?」 (stack overflow)
http://ja.stackoverflow.com/questions/9066/libev%E3%81%AEev-async%E3%81%A8%E3%81%AF%E4%BD%95%E3%81%AB%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B%E3%82%82%E3%81%AE%E3%81%A7%E3%81%99%E3%81%8B

evライブラリーというのがあるのか。

「libevによる非同期プログラミング」(Qiita)
http://qiita.com/0xfffffff7/items/c5287067623fdbd2c3cb

じゃあ、インストールしてみよ。

# apt-get -y install libev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libev

そんなコマンドは無いのか……。

「How to install libev-dev on Ubuntu 14.04 (Trusty Tahr)」(HOW TO INSTALL)
https://www.howtoinstall.co/en/ubuntu/trusty/libev-dev

sudo apt-get update
sudo apt-get install libev-dev

入ったかな。

# g++ tamesi29a7_main.cpp

大量のエラーが出てきた。こうか?

# g++ -std=c++11 tamesi29a7_main.cpp
/tmp/ccMfSVKb.o: In function `main':
tamesi29a7_main.cpp:(.text+0x18f): undefined reference to `ev_default_loop'
tamesi29a7_main.cpp:(.text+0x1e3): undefined reference to `AMQP::TcpConnection::TcpConnection(AMQP::TcpHandler*, AMQP::Address const&)'
tamesi29a7_main.cpp:(.text+0x22d): undefined reference to `AMQP::exclusive'
tamesi29a7_main.cpp:(.text+0x276): undefined reference to `ev_run'
/tmp/ccMfSVKb.o: In function `AMQP::Field::~Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldD2Ev[_ZN4AMQP5FieldD5Ev]+0xd): undefined reference to `vtable for AMQP::Field'
/tmp/ccMfSVKb.o: In function `AMQP::Field::Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldC2Ev[_ZN4AMQP5FieldC5Ev]+0x9): undefined reference to `vtable for AMQP::Field'
/tmp/ccMfSVKb.o: In function `AMQP::Table::Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableC2Ev[_ZN4AMQP5TableC5Ev]+0x19): undefined reference to `vtable for AMQP::Table'
/tmp/ccMfSVKb.o: In function `AMQP::Table::~Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableD2Ev[_ZN4AMQP5TableD5Ev]+0xd): undefined reference to `vtable for AMQP::Table'
/tmp/ccMfSVKb.o: In function `AMQP::Channel::Channel(AMQP::Connection*)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x30): undefined reference to `AMQP::ChannelImpl::ChannelImpl()'
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x65): undefined reference to `AMQP::ChannelImpl::attach(AMQP::Connection*)'
/tmp/ccMfSVKb.o: In function `AMQP::Channel::~Channel()':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelD2Ev[_ZN4AMQP7ChannelD5Ev]+0x2c): undefined reference to `AMQP::ChannelImpl::close()'
/tmp/ccMfSVKb.o: In function `AMQP::Channel::declareQueue(int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7Channel12declareQueueEi[_ZN4AMQP7Channel12declareQueueEi]+0x68): undefined reference to `AMQP::ChannelImpl::declareQueue(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, AMQP::Table const&)'
/tmp/ccMfSVKb.o: In function `AMQP::Connection::~Connection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10ConnectionD2Ev[_ZN4AMQP10ConnectionD5Ev]+0x24): undefined reference to `AMQP::ConnectionImpl::~ConnectionImpl()'
/tmp/ccMfSVKb.o: In function `AMQP::Connection::close()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10Connection5closeEv[_ZN4AMQP10Connection5closeEv]+0x18): undefined reference to `AMQP::ConnectionImpl::close()'
/tmp/ccMfSVKb.o: In function `AMQP::TcpConnection::~TcpConnection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0xd): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x19): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x51): undefined reference to `AMQP::Watchable::~Watchable()'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::callback(ev_loop*, ev_io*, int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi[_ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi]+0x33): undefined reference to `AMQP::TcpConnection::process(int, int)'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::Watcher(ev_loop*, AMQP::TcpConnection*, int, int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7WatcherC2EP7ev_loopPNS_13TcpConnectionEii[_ZN4AMQP12LibEvHandler7WatcherC5EP7ev_loopPNS_13TcpConnectionEii]+0xbf): undefined reference to `ev_io_start'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::~Watcher()':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7WatcherD2Ev[_ZN4AMQP12LibEvHandler7WatcherD5Ev]+0x2f): undefined reference to `ev_io_stop'
/tmp/ccMfSVKb.o: In function `AMQP::LibEvHandler::Watcher::events(int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher6eventsEi[_ZN4AMQP12LibEvHandler7Watcher6eventsEi]+0x26): undefined reference to `ev_io_stop'
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher6eventsEi[_ZN4AMQP12LibEvHandler7Watcher6eventsEi]+0x5d): undefined reference to `ev_io_start'
collect2: error: ld returned 1 exit status

どうしたものか。

「c libev undefined reference to `ev_default_loop'」 (stack overflow)
http://stackoverflow.com/questions/13844309/c-libev-undefined-reference-to-ev-default-loop

「-lev」というオプションを付けたらいいのだろうか?

# g++ -std=c++11 tamesi29a7_main.cpp -lev
/tmp/ccbptlAj.o: In function `main':
tamesi29a7_main.cpp:(.text+0x1e3): undefined reference to `AMQP::TcpConnection::TcpConnection(AMQP::TcpHandler*, AMQP::Address const&)'
tamesi29a7_main.cpp:(.text+0x22d): undefined reference to `AMQP::exclusive'
/tmp/ccbptlAj.o: In function `AMQP::Field::~Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldD2Ev[_ZN4AMQP5FieldD5Ev]+0xd): undefined reference to `vtable for AMQP::Field'
/tmp/ccbptlAj.o: In function `AMQP::Field::Field()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5FieldC2Ev[_ZN4AMQP5FieldC5Ev]+0x9): undefined reference to `vtable for AMQP::Field'
/tmp/ccbptlAj.o: In function `AMQP::Table::Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableC2Ev[_ZN4AMQP5TableC5Ev]+0x19): undefined reference to `vtable for AMQP::Table'
/tmp/ccbptlAj.o: In function `AMQP::Table::~Table()':
tamesi29a7_main.cpp:(.text._ZN4AMQP5TableD2Ev[_ZN4AMQP5TableD5Ev]+0xd): undefined reference to `vtable for AMQP::Table'
/tmp/ccbptlAj.o: In function `AMQP::Channel::Channel(AMQP::Connection*)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x30): undefined reference to `AMQP::ChannelImpl::ChannelImpl()'
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelC2EPNS_10ConnectionE[_ZN4AMQP7ChannelC5EPNS_10ConnectionE]+0x65): undefined reference to `AMQP::ChannelImpl::attach(AMQP::Connection*)'
/tmp/ccbptlAj.o: In function `AMQP::Channel::~Channel()':
tamesi29a7_main.cpp:(.text._ZN4AMQP7ChannelD2Ev[_ZN4AMQP7ChannelD5Ev]+0x2c): undefined reference to `AMQP::ChannelImpl::close()'
/tmp/ccbptlAj.o: In function `AMQP::Channel::declareQueue(int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP7Channel12declareQueueEi[_ZN4AMQP7Channel12declareQueueEi]+0x68): undefined reference to `AMQP::ChannelImpl::declareQueue(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, AMQP::Table const&)'
/tmp/ccbptlAj.o: In function `AMQP::Connection::~Connection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10ConnectionD2Ev[_ZN4AMQP10ConnectionD5Ev]+0x24): undefined reference to `AMQP::ConnectionImpl::~ConnectionImpl()'
/tmp/ccbptlAj.o: In function `AMQP::Connection::close()':
tamesi29a7_main.cpp:(.text._ZN4AMQP10Connection5closeEv[_ZN4AMQP10Connection5closeEv]+0x18): undefined reference to `AMQP::ConnectionImpl::close()'
/tmp/ccbptlAj.o: In function `AMQP::TcpConnection::~TcpConnection()':
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0xd): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x19): undefined reference to `vtable for AMQP::TcpConnection'
tamesi29a7_main.cpp:(.text._ZN4AMQP13TcpConnectionD2Ev[_ZN4AMQP13TcpConnectionD5Ev]+0x51): undefined reference to `AMQP::Watchable::~Watchable()'
/tmp/ccbptlAj.o: In function `AMQP::LibEvHandler::Watcher::callback(ev_loop*, ev_io*, int)':
tamesi29a7_main.cpp:(.text._ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi[_ZN4AMQP12LibEvHandler7Watcher8callbackEP7ev_loopP5ev_ioi]+0x33): undefined reference to `AMQP::TcpConnection::process(int, int)'
collect2: error: ld returned 1 exit status

エラーは減っただろうか。

「Build against rabbitmq-c」(stack overflow)
http://stackoverflow.com/questions/12028112/build-against-rabbitmq-c

Git Hub のインストールの説明を読むと、ライブラリとリンクしてください、と書いているな。こうか。

# g++ -std=c++11 tamesi29a7_main.cpp -lev -lamqpcpp
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libamqpcpp.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

なんだろう?

「Undefined reference to pthread_create in Linux」(stack overflow)
http://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux

「-pthread」も付けたらいいんだろうか?

# g++ -std=c++11 tamesi29a7_main.cpp -lev -lamqpcpp -pthread

コンパイルは通ったようだ。実行ファイルの名前が a.out になってるが……。こうか。

# g++ -std=c++11 tamesi29a7_main.cpp -lev -lamqpcpp -pthread -o tamesi29a7_main.exe

よし、tamesi29a7_main.exe というファイルがでけた。

# ./tamesi29a7_main.exe
declared queue amq.gen-R8_7pGx8GdY5EmtcMddvzQ

なんのこっちゃ。

多分、キューの名前でも取得したんだろうか?

# rabbitmqctl list_queues
Listing queues ...
1111    0

うーむ。

「amqpcpp/examples/example_consume.cpp」(akalend/amqpcpp)
https://github.com/akalend/amqpcpp/blob/master/examples/example_consume.cpp

これを試してみよう。

tamesi29a8_example_consume.cpp

#include "AMQPcpp.h"

using namespace std;

int i=0;

int onCancel(AMQPMessage * message ) {
    cout << "cancel tag="<< message->getDeliveryTag() << endl;
    return 0;
}

int  onMessage( AMQPMessage * message  ) {
    uint32_t j = 0;
    char * data = message->getMessage(&j);
    if (data)
          cout << data << endl;

    i++;

    cout << "#" << i << " tag="<< message->getDeliveryTag() << " content-type:"<< message->getHeader("Content-type") ;
    cout << " encoding:"<< message->getHeader("Content-encoding")<< " mode="<<message->getHeader("Delivery-mode")<<endl;

    if (i > 10) {
        AMQPQueue * q = message->getQueue();
        q->Cancel( message->getConsumerTag() );
    }
    return 0;
};


int main () {


    try {
//      AMQP amqp("123123:akalend@localhost/private");

        AMQP amqp("123123:akalend@localhost:5673/private");

        AMQPQueue * qu2 = amqp.createQueue("q2");

        qu2->Declare();
        qu2->Bind( "e", "");

        qu2->setConsumerTag("tag_123");
        qu2->addEvent(AMQP_MESSAGE, onMessage );
        qu2->addEvent(AMQP_CANCEL, onCancel );

        qu2->Consume(AMQP_NOACK);//


    } catch (AMQPException e) {
        std::cout << e.getMessage() << std::endl;
    }

    return 0;

}
# g++ -std=c++11 tamesi29a8_example_consume.cpp -lev -lamqpcpp -pthread -o tamesi29a8_example_consume.exe
tamesi29a8_example_consume.cpp:1:21: fatal error: AMQPcpp.h: No such file or directory
compilation terminated.

なんの参考にもならないソースだったか。

# ./tamesi29a7_main.exe
declared queue amq.gen-0n2SmfNgiVabWrTBjd5F3w
# ./tamesi29a7_main.exe
declared queue amq.gen-MRpMMZ2dX3VRK-HVAJV62w
# ./tamesi29a7_main.exe
declared queue amq.gen-wCRRD2i98lE52gR8nHjaxw

この name は何なんだろな。

「FLAGS AND TABLES」(CopernicaMarketingSoftware/AMQP-CPP)
https://github.com/CopernicaMarketingSoftware/AMQP-CPP

説明を読んで改造してみる。

tamesi29a7_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;

        // now we can close the connection
        connection.close();
    }

    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}
# nano tamesi29a9_main.cpp
# g++ -std=c++11 tamesi29a9_main.cpp -lev -lamqpcpp -pthread -o tamesi29a9_main.exe
tamesi29a9_main.cpp: In lambda function:
tamesi29a9_main.cpp:37:9: error: ‘connection’ is not captured
         connection.close();
         ^
tamesi29a9_main.cpp:29:22: note: the lambda has no capture-default
     auto callback = [](const std::string &name, int msgcount, int consumercount) {
                      ^
tamesi29a9_main.cpp:14:25: note: ‘AMQP::TcpConnection connection’ declared here
     AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));
                         ^
tamesi29a9_main.cpp: In function ‘int main()’:
tamesi29a9_main.cpp:41:5: error: expected ‘,’ or ‘;’ before ‘channel’
     channel.declareQueue("1111").onSuccess(callback);
     ^

じゃあ、こう。

        // connection.close();
# g++ -std=c++11 tamesi29a9_main.cpp -lev -lamqpcpp -pthread -o tamesi29a9_main.exe
tamesi29a9_main.cpp: In function ‘int main()’:
tamesi29a9_main.cpp:41:5: error: expected ‘,’ or ‘;’ before ‘channel’
     channel.declareQueue("1111").onSuccess(callback);
     ^

セミコロン足りないのか、サンプルからコピーしたのに。じゃあ足す。

    };
# g++ -std=c++11 tamesi29a9_main.cpp -lev -lamqpcpp -pthread -o tamesi29a9_main.exe

コンパイルは通った。ソースはこんな感じ。

tamesi29a9_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;

        // now we can close the connection
        // connection.close();
    };

    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}
# ./tamesi29a9_main.exe
declared queue 1111

なんか待機している。 ためしさんシリーズ を使うか。

http://★.★.★.★/tamesi5.php

RabbitMQ で エンキューする ためしさん だ。

[x] Sent 'Flying Dragon!'

とくに変化はない。

^Z
[1]+  Stopped                 ./tamesi29a9_main.exe
# bg 1
[1]+ ./tamesi29a9_main.exe &

うーむ。

# jobs
[1]+  Running                 ./tamesi29a9_main.exe &

うーむ。ファミコンの さんまの名探偵 を遊んでいたときの気分に似ている。

ls
tamesi28.d  tamesi29a7_main.cpp  tamesi29a7_main.exe  tamesi29a8_example_consume.cpp  tamesi29a9_main.cpp  tamesi29a9_main.exe

何かが出力されている様子もない。

# rabbitmqctl list_queues
Listing queues ...
1111    0

そもそも キュー が空では。

# rabbitmqctl list_consumers
Listing consumers ...
1111    <rabbit@★> amq.ctag-ztANwsUVbqTvGhE7rnXh6w false   0       []
1111    <rabbit@★> amq.ctag-w-yJ9Fb8Ksb5HY0QX3iRAg false   0       []
1111    <rabbit@★> amq.ctag-xb6boiuc_Ee1girva-GdkQ false   0       []
1111    <rabbit@★> amq.ctag-jV7AUun3pc-FyFcK9M_Owg false   0       []
1111    <rabbit@★>  amq.ctag-Jdt5oPqW4mtCerlx8dUIOw false   0       []
1111    <rabbit@★> amq.ctag-55NHUikpOIFTxgxOEcipsg false   0       []

コンシューマーは いっぱいいるのにな。じゃあ、こいつらが取り合ってるなんてことがあるのだろうか?

# ps aux | grep rabbit
root     12122  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh /usr/sbin/rabbitmq-server
root     12129  0.0  0.0  55128     0 ?        S    Mar07   0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12130  0.0  0.0   4500     8 ?        Ss   Mar07   0:00 sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12131  0.0  0.0   4500     0 ?        S    Mar07   0:00 /bin/sh -e /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 12237  0.6  2.6 2712560 27192 ?       Sl   Mar07  27:01 /usr/lib/erlang/erts-8.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -B i -- -root /usr/lib/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/ebin -noshell -noinput -s rabbit boot -sname rabbit@★ -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/rabbit@★.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/rabbit@★-sasl.log"} -rabbit enabled_plugins_file "/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/plugins" -rabbit plugins_expand_dir "/var/lib/rabbitmq/mnesia/rabbit@★-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@★" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672
rabbitmq 12333  0.0  0.0   4356    72 ?        Ss   Mar07   0:18 erl_child_setup 1024
rabbitmq 12343  0.0  0.0  13676    36 ?        Ss   Mar07   0:00 inet_gethost 4
rabbitmq 12344  0.0  0.0  15796   228 ?        S    Mar07   0:01 inet_gethost 4
root     27067  0.0  0.0  12936   988 pts/4    S+   08:07   0:00 grep --color=auto rabbit
rabbitmq 27670  0.0  0.0  13848   252 ?        S    Mar06   0:11 /usr/lib/erlang/erts-8.1/bin/epmd -daemon

うーむ。

もしかして。

/home/★user/shogi/php_log# ls -an
total 8
drwxr-xr-x  2 0 0 4096 Mar  8 09:50 .
drwxr-xrwx 20 0 0 4096 Mar 10 06:04 ..
-rw-r--r--  1 0 0    0 Mar 10 08:05 dequeueBroker.err.log
-rw-r--r--  1 0 0    0 Mar 10 08:05 dequeueBroker.out.log

更新時刻が更新されている。
だが中身は空っぽだった。

# ps aux | grep dequeueBroker.php
root     20224  0.0  1.3 232308 14060 ?        S    04:05   0:01 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     21242  0.0  1.3 232308 14060 ?        S    05:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     22336  0.0  1.3 232308 14056 ?        S    06:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     25095  0.0  1.3 232308 14044 ?        S    07:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     26770  0.0  1.3 232308 14040 ?        S    08:05   0:00 /usr/bin/php /home/★user/shogi/php_service/dequeueBroker.php
root     27318  0.0  0.0  13068   988 pts/4    S+   08:19   0:00 grep --color=auto dequeueBroker.php

なんか 5分 に dequeueBroker.php が増えてないか。

こんなことができるのは Cron だろう。

# sudo crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
#0-59/30 * * * * /bin/bash /home/★user/shogi/bash_service/tamesi12.sh
5 * * * * /bin/bash /home/★user/shogi/bash_service/kicker_dequeueBroker.sh

ほら、いた。コメントアウトしとこ。
プロセスも落としとこ。

kill `ps aux | grep dequeueBroker.php | sed 's/[\t ]\+/\t/g' | cut -f 2`
# ps aux | grep dequeueBroker.php
root     27385  0.0  0.0  12936   988 pts/4    S+   08:23   0:00 grep --color=auto dequeueBroker.php

もう1回。今度は ためしさん14でエンキュー。

http://★.★.★.★/tamesi14.php

あっ、ためしさん14にバグがある。直そう。
と思ったら バグは無かったので 直さなかった。

# rabbitmqctl list_queues
Listing queues ...
1111    0

エンキューされてない。

chmod 666 dequeueBroker.err.log
chmod 666 dequeueBroker.out.log

RabbitMQ再起動

代わり無し。じゃあ RabbitMQ を再起動してみる。

# rabbitmqctl stop
Stopping and halting node 'rabbit@★' ...
[1]+  Done                    ./tamesi29a9_main.exe  (wd: /home/★user/shogi/cpp_service)
(wd now: /var/www/html)
# rabbitmq-server

              RabbitMQ 3.6.6. Copyright (C) 2007-2016 Pivotal Software, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /var/log/rabbitmq/rabbit@tk2-217-18401.log
  ######  ##        /var/log/rabbitmq/rabbit@tk2-217-18401-sasl.log
  ##########
              Starting broker...
 completed with 0 plugins.

[Ctrl]+[Z]

# bg 1

ログを見てみるか。

cd /var/log/rabbitmq

うーん。

前は PHPのエンキューは 動いていたんだが、今日は動かないようだ。

一応、ためしさん2、3、5 を叩いてみる。

# rabbitmqctl list_queues
Listing queues ...
1111    1

うーん??

じゃあ、ためしさん2 を叩いてみる。

変わらない。

じゃあ、ためしさん3 を叩いてみる。

変わらない。

じゃあ、ためしさん5 を叩いてみる。

# rabbitmqctl list_queues
Listing queues ...
1111    2

じゃあ、ためしさん14 を叩いてみる。

# rabbitmqctl list_queues
Listing queues ...
1111    3

ということは、RabbitMQ は動いてなかったのか。今はバックグラウンドで動いているが、じゃあ、ログアウトしてみる。

# logout
$ logout

再度ログイン。

# sudo su -
※パスワードを入れる
# jobs

空っぽだ。

# ps aux | grep rabbit
rabbitmq 27670  0.0  0.0  13848   252 ?        S    Mar06   0:12 /usr/lib/erlang/erts-8.1/bin/epmd -daemon
root     27924  0.0  0.0   4500   660 ?        S    08:33   0:00 /bin/sh /usr/sbin/rabbitmq-server
root     27931  0.0  0.1  55128  1820 ?        S    08:33   0:00 su rabbitmq -s /bin/sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 27932  0.0  0.0   4500   656 ?        Ss   08:33   0:00 sh -c /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 27933  0.0  0.0   4500   756 ?        S    08:33   0:00 /bin/sh -e /usr/lib/rabbitmq/bin/rabbitmq-server
rabbitmq 28038  0.9  5.8 2698680 59836 ?       Sl   08:33   0:09 /usr/lib/erlang/erts-8.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -B i -- -root /usr/lib/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/ebin -noshell -noinput -s rabbit boot -sname rabbit@tk2-217-18401 -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/rabbit@tk2-217-18401.log"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/rabbit@tk2-217-18401-sasl.log"} -rabbit enabled_plugins_file "/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.6/plugins" -rabbit plugins_expand_dir "/var/lib/rabbitmq/mnesia/rabbit@tk2-217-18401-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@tk2-217-18401" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672
rabbitmq 28135  0.0  0.0   4356   524 ?        Ss   08:33   0:00 erl_child_setup 1024
rabbitmq 28145  0.0  0.0  13676   468 ?        Ss   08:33   0:00 inet_gethost 4
rabbitmq 28146  0.0  0.0  15796   608 ?        S    08:33   0:00 inet_gethost 4
root     29169  0.0  0.0  12936   988 pts/0    S+   08:49   0:00 grep --color=auto rabbit

うーむ、見ても分からん。

ためしさん5 にアクセス。

# rabbitmqctl list_queues
Listing queues ...
1111    5

じゃあ、ジョブは見えないが、動いているのだろうか?
だったら .cpp の方はどうか?

# cd /home/★user/shogi/cpp_service
# ./tamesi29a9_main.exe
declared queue 1111

なーんにも変わらない。確かにキューの名前は取ってきているのかもしれないが、メッセージを取ってくるには どうすればいいのか?

書いてるのか?

「AMQP-CPP」(CopernicaMarketingSoftware/AMQP-CPP)
https://github.com/CopernicaMarketingSoftware/AMQP-CPP

ためしさん29a10

tamesi29a10_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "(^q^)declared queue " << name << std::endl;

        // now we can close the connection
        // connection.close();
    };
    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {

        std::cout << "(^q^)consume operation started" << std::endl;
    };

    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {

        std::cout << "(^q^)consume operation failed" << std::endl;
    }

    // callback operation when a message was received
    auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {

        std::cout << "(^q^)message received" << std::endl;

        // acknowledge the message
        channel.ack(deliveryTag);
    }

    // start consuming from the queue, and install the callbacks
    channel.consume("1111")
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}

こんなんでいいんだろうか?

# g++ -std=c++11 tamesi29a10_main.cpp -lev -lamqpcpp -pthread -o tamesi29a10_main.exe
tamesi29a10_main.cpp: In function ‘int main()’:
tamesi29a10_main.cpp:55:5: error: expected ‘,’ or ‘;’ before ‘auto’
     auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {
     ^
tamesi29a10_main.cpp:65:21: error: ‘messageCb’ was not declared in this scope
         .onReceived(messageCb)
                     ^

なんか サンプル・プログラムは セミコロンが抜けてるんじゃないかな。

「Semicolon is missing in sample program.」(CopernicaMarketingSoftware/AMQP-CPP)
https://github.com/CopernicaMarketingSoftware/AMQP-CPP/issues/119

送っといた。

よし、コンパイルも通った。

# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started
(^q^)message received
(^q^)message received
(^q^)message received
(^q^)message received
(^q^)message received

[Ctrl]+[C]で抜ける。

確かに 5匹の ためしさん が出てきたのは正解なんだが、メッセージを拾って欲しい。

# rabbitmqctl list_queues
Listing queues ...
1111    0

もう空っぽだ。

こう改造して どうか?

tamesi29a10_main.cpp

#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>

int main()
{
    // access to the event loop
    auto *loop = EV_DEFAULT;

    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(loop);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

//    // create a temporary queue
//    channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
//
//        // report the name of the temporary queue
//        std::cout << "declared queue " << name << std::endl;
//
//        // now we can close the connection
//        connection.close();
//    });
    // create a custom callback
    auto callback = [](const std::string &name, int msgcount, int consumercount) {

        // @todo add your own implementation

        // report the name of the temporary queue
        std::cout << "(^q^)declared queue " << name << std::endl;

        // now we can close the connection
        // connection.close();
    };
    // declare the queue, and install the callback that is called on success
    channel.declareQueue("1111").onSuccess(callback);

    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {

        // std::cout << "(^q^)consume operation started" << std::endl;
        std::cout << "(^q^)consume operation started [" << consumertag << "]" << std::endl;
    };

    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {

        // std::cout << "(^q^)consume operation failed" << std::endl;
        std::cout << "(^q^)consume operation failed [" << &message << "]" << std::endl;
    };

    // callback operation when a message was received
    auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {

        // std::cout << "(^q^)message received" << std::endl;
        std::cout << "(^q^)message received [" << &message << "]" << std::endl;

        // acknowledge the message
        channel.ack(deliveryTag);
    };

    // start consuming from the queue, and install the callbacks
    channel.consume("1111")
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);

    // run the loop
    ev_run(loop, 0);

    // done
    return 0;
}

ためしさん14 を使って、メッセージをエンキューしておく。

# rabbitmqctl list_queues
Listing queues ...
1111    6
# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-f9-pEPIwqTgyCHuhYm4wiA]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]
(^q^)message received [0xc87ea0]

ぬぎぎ! メッセージだけ なんとかなればいい。しかし異なるメッセージを送ったのに全部 0xc87ea0 とはどういうことだ。
何かのメモリ・アドレスか?

AMQP::Message

AMQP::Message は文字列じゃないんじゃないか。

/usr/include/amqpcpp.h

/usr ディレクトリの下を探していると、
/usr/include/amqpcpp.h というファイルがあった。

さらにディレクトリの下にヘッダファイルがあるようだ。

/usr/include/amqpcpp# ls
address.h     channel.h            copiedbuffer.h          deferredget.h    exception.h     libevent.h  monitor.h            stringfield.h    watchable.h
array.h       channelimpl.h        decimalfield.h          deferred.h       exchangetype.h  libev.h     numericfield.h       table.h
booleanset.h  classes.h            deferredcancel.h        deferredqueue.h  field.h         libuv.h     outbuffer.h          tcpchannel.h
buffer.h      connection.h         deferredconsumerbase.h  endian.h         fieldproxy.h    login.h     protocolexception.h  tcpconnection.h
bytebuffer.h  connectionhandler.h  deferredconsumer.h      entityimpl.h     flags.h         message.h   receivedframe.h      tcpdefines.h
callbacks.h   connectionimpl.h     deferreddelete.h        envelope.h       frame.h         metadata.h  stack_ptr.h          tcphandler.h

message.h というファイルが気になる。
著作表記を見ると、Copernica BV というところが作っているみたいだ。

const std::string &exchange() const
と、
const std::string &routingkey() const

ぐらいしか アクセスできそうなものが無いんだが。

        std::cout << "(^q^)message received exchange=[" << &message.exchange() << "] routingkey=[" << &message.routingkey() << "]" << std::endl;
# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-baH5AqjR3dg-pmSMobsugg]
(^q^)message received exchange=[0xcdf0d8] routingkey=[0xcdf0f8]
(^q^)message received exchange=[0xcdf0d8] routingkey=[0xcdf0f8]

違うのか。

Message クラスは Envelope クラスを継承しているので、Envelope も見てみよう。

    /**
     *  Access to the full message data
     *  @return buffer
     */
    const char *body() const
    {
        return _body;
    }

こんなのがある。

        std::cout << "(^q^)message received body=[" << message.body() << "]" << std::endl;

あと、頭のアンパサンドは要らんのでは?

# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-FYCpWCbtuoRvXl4XoRReHA]
(^q^)message received body=[ClickSitanDaze▒YCpWCbtuoRvXl4XoRReHA]
(^q^)message received body=[Dragon▒]
(^q^)message received body=[Tiger▒]

よしきた道中。なんか 謎の文字が後ろについているが……。

    /**
     *  Size of the body
     *  @return uint64_t
     */
    uint64_t bodySize() const
    {
        return _bodySize;
    }

C言語でいうところの、文字列のサイズかだぜ?
どうやって使うのか。

「convert a char* to std::string」(stack overflow)
http://stackoverflow.com/questions/1195675/convert-a-char-to-stdstring

        std::string myString(message.body(), message.bodySize());
        std::cout << "(^q^)message received myString=[" << myString << "]" << std::endl;
# ./tamesi29a10_main.exe
(^q^)declared queue 1111
(^q^)consume operation started [amq.ctag-M2QG32xTPEkdEn-Swcl2Sw]
(^q^)message received myString=[Tiger]

いけた。

いったん 別記事にまとめよう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1