Laravelã®Message Queueãææ¡ãã¦ã¿ãï¼åæã¨ãã¦ã®Message Queueãã®ãã®
ã¡ããã¨ãLaravelã®Message Queueãã«ã¤ãã¦èª¿ã¹ãç¨äºããã£ãã®ã§ã
調ã¹ç©ã®åå¿é²ç¨ã«æ¸ãã¦ããã®ã§ãééãã¨ããããããããªãã®ã§
ã»ä½¿ãæã¯æ°ãã¤ãã¦ãã ãã
ã»ééãã«æ°ä»ãããã³ã¡ã³ããªã©ã§çªã£è¾¼ãã§ããã ããã°ãã¨æãã¾ãã
ã¨ããè¨ã訳ãæ¸ãã¦ãã(ç¬
ãã¦ã
ã¾ããMessage Queueãã£ã¦ã®ããããã¯ãã·ã¹ãã ã®ä¸è¬çãªæ¦å¿µãã¨ãã¦åå¨ãã¾ãã
大æã¯ãmessnd() / msgrcv() ã¨ã mq_send() / mq_receive() ã¨ãã使ã£ã¦ã¾ãããããããã»ã¹ééä¿¡ã¨å¼ã°ãããã®ã®ä¸ã¤ã§ãã¾ããã®ã¾ãã¾ãã¡ãã»ã¼ã¸ãã¥ã¼ãã£ã¦å¼ç§°ã ã£ãã¨è¨æ¶ãã¦ããã¾ãã
messnd() ã¨ã mq_send() ã¨ãã§enqueueãmsgrcv()ã¨ãmq_receive()ã§dequeueãããåºæ¬çã«ã¯ããã¥ã¼æ§é ãæã¤ãã¼ã¿æ§é ãã£ã¦ã ãã§ããããã¥ã¼æ§é ã¯FIFOã£ã¦è¨ã£ã¦ããã£ã¦ä¸ãã¯ãçç¥ãã¦ããããï¼
ãªã®ã§æ¬è³ªçã«ã¯ããªã«ããã(é常ã¯æåå)ãenqueueã§ç©ãã§ã(ã ããã)ç©ãã é çªã«dequeueã§åãåºããæ©è½ã§ãã
ã§ã¾ããããããä½ã«ä½¿ã£ã¦ããããã®ã§ããããç©ãé度ã«ã ã©ããã£ãããããããªã¿ã¹ã¯ããæã
"ãã«ã£ã¨"ç©ã¾ãããããã®ããã¡ã¾ã¡ã¾ã¨å¦çãã¦ããããããªãã¦æã«ããã使ããã¾ãã¦ã
Message Queueã®ããã®ããç©ãã®ã¯ã©ããªã«ã ã©ã®ããç©ã¿æ¹ããã¦ããå¦çã¯åºæ¬çã«"ä¸å®ã®ãã¼ã¹ã§çä»ãã"ãã£ã¦æã«ããã¾ãããªã®ã§ãã¤ã³ãã©ãªã½ã¼ã¹ãããç¨åº¦è¨ç®ãããããã§ããªã
ãã®ããé¡ããããã¨ãå®éã«å¦çããããå¥ã
ã«åãã®ããéåæããªãã¦è¨ãæ¹ããããããã¾ãããããªã®ã§Message Queueã¯ãéåæãã§ãããã¾ãã
ãªããç©ãããä»äºãã¯ãããããæåå(string)ã§ããã°ãã©ã¼ãããã¯ãã好ã¿ããªã®ã§ãé©å®å¥½ããªé»æãã©ã¼ãããããç¨æãã ããããããããããã¼ã¹ãããããã¡ã¤ã³åºæè¨èª(DSL)ããã£ããè¨è¨ããããPHPã¨ãã ã¨ãé
åãjsonãªãserializeãªããã£ã¦ã®ãæããªããã¨ã¯æãã¾ãã
ç©ããä»äºã®äºãã¿ã¹ã¯ã¨ãjobã¨ãè¨ã£ã¦ãããããäºããããã§ãjob queueãªãã¦å¼ç§°ãè³ã«ãã¾ããããtask queueã£ã¦å¼ç§°ãããã¾ãããjob queueããã¯èããªãâ¦â¦ãããããªãã
ãã§ã
ãéåæã§ãä»äºãå¦çãããã£ã¦æèã§Message Queueãèªãå ´åã
ãç©ã¿ä¸ããå
ã¯ã©ãï¼ããç©ãã ãä»äºã誰ãã©ããªé¢¨ã«å¦çãã¦æ¶åãã¦ããã®ï¼ãã£ã¦ããããéè¦ã«ãªãã¾ãã
ã®ã§ããã¿ç ãã¦ã
ã¾ããç©ã¿ä¸ããå
ã¯ï¼ãã£ã¦ã®ããã£ã¦ã
ä¾ãã°å
ã«ããããmessnd() ã¨ã mq_send()ãã ã¨ãããã¯ãOSã§å®è£
ããã¦ããããã»ã¹ééä¿¡(IPC)ãã§ãã¾ããããã«ã¦ã§ã¢ã¨ãããã¾ãæå¾
ã§ããªãã£ãé ãã«ã¯é«é »åº¦ã§ä½¿ããã¦ãããã®ã§ãããã¾ã*1ã
ã§ã¾ãä¸æ°ã«æé軸ããç¾å¨ãã«æã¡ä¸ããã¨â¦â¦ããã¾ããRDBã§èªåå®è£
ãã¨ãããããªã®ã§ãããAWS SQSã¨ããApache Kafkaã¨ããããã¯ãé«å§ã§ãã¼ã¿ãããè¾¼ãã§ããããªãã«é害èæ§ããã£ã¦ãããããã®é度ã§"ããç¨åº¦é¸æãã¦"ãã¼ã¿ãèªã¿åºãããæ©æ§ã§ããã°ãããè¾¼ãå
ã¨ãã¦ã¯ååã§ãããã¾ãâ¦â¦ã®ã§ãã¨ã¯è¦ä»¶ã«åããã¦ã
ã次ã«ãã©ããã£ã¦ä»äºãæ¶åããã®ï¼ãã«ã¤ãã¦ã¯ãããããã¾ãããããã§ãçµããã£ã¦ã話ã«ãªãã®ã§ã
åºæ¬çã«ã¯ãã¡ãã»ã¼ã¸ãããéã¯ãã°ã«ã°ã«ã¨ã¶ãåãã¦å¦çããç¶ããããããçµããã£ã¦ã®ãåºæ¬ã«ãªãã¾ãã
ç©ã¿ä¸ãããä»äºã®ä¾é ¼æãã©ã¼ãããã«ã¤ãã¦ã¯ã¾ãããéæã«ãã
ããããããæ¸ããããå¾ã§ã¡ã³ããã³ã¹ããããâ¦â¦ãªãã¦ãã¨ãèãã¦ããã¨ãããããã¨ã
ãã§ã¾ãããããã¨æ¬¡ã«èå¯ããªãã¨ãããªãã®ã以ä¸ã®äºè±¡ã§ãã
ã»ãä»äºæ¶åå´ã延ã
ã¨ãããã»ã¹ã24æé365æ¥åãã¦ããã¨ãã¦ãã¡ã¢ãªãªã¼ã¯ã¨ã大ä¸å¤«ï¼
ã»åãããä»äºæ¶åå´ãããä»äºããªããæã£ã¦ããããã¯ã©ããã¦ãã®ï¼
ã¾ãã¡ã¢ãªãªã¼ã¯ã«ã¤ãã¦ã¯ãããã¡ãããæ®æ®µPHPã使ã£ã¦ããã®ã§ããã®å ´å端çã«ãããã¾ã§ã®ä¿¡é ¼æ§ã¯ãªããã¨èãã¦ããã®ã§(身ãèããªã)ã
ããããã¨ã¾ããä»çµã¿ã¨ãã¦ã¯ãä¸å®æéãã¾ãã¯ä¸å®åæ°åããããä¸åº¦ãã®ãããããã»ã¹ãè½ã¨ããã£ã¦å®è£
ããããããã¾ãã
ã¤ã¾ã
while($message = dequeue()) { ãä»äº }
ã§ã¯ãªãã¦
while($message = dequeue()) { // åæ°ç¢ºèª if (ä¸å®åæ° <= ++$ä½æ¥ã«ã¦ã³ã¿) { çµäºå¦çãã¦ããã»ã¹ãexit; } // else ãä»äº }
ã£ã¦ãªæãã«ãã¦ããã¾ã(åæ°å¶éã®å ´å)ã
ãããªãã¨ãæ»ãã ã次ã®ãããã誰ãèµ·ããã®ï¼ãã£ã¦ã®ããããã§ãããã¡ããã¨æ£ã®ä¸ã«pushã
ã次ã
ãããä»äºããªããæã£ã¦ããããã¯ã©ããã¦ãã®ï¼ãã«ã¤ãã¦ã¯ã2種é¡ã®è¦è§£ããä¸å¿ããã¾ãã¦ã
ã»ãä»äºããªãæã¯ãsleepã¨ãã§å¾
ã¤
ã»ãä»äºããªãæã¯ãä¸ç«¯exitã§çµäºãã
ã¨ãªãã¾ã(ã¬ã¢ã±ã¼ã¹çã«å¥ã©ã¤ã³ãåå¨ãã¾ãããé¢åãªãã§çç¥)ã
ã§ã¾ããããã¡ããã®å ´åã¯ãsleepã§å¾ ã£ã¦ãã¡ã¢ãªã¨ãé£ãããªããã¨ãªãã®ã§ãåºæ¬ãè½ã¡ã/è½ã¨ããäºãå¤ãã§ãã
â¦â¦ã¨ãªãã¨ããä»äºããªãã¨è½ã¡ãã®ã§ãã¤ã¾ãã¯ããä»äºãããããã¦ããããä»äºããªãã¦ããè½ã¡ããã¨ã«ãªãã¾ãã
ãªã®ã§ãã¡ã¢ãªãªã¼ã¯ã§æ£ã«ä¸ãã¦ãããæ»ãã ã次ã®ãããã誰ãèµ·ããã®ï¼ãã£ã¦ãé¡ãpopããã¦ã話ãmergeãã¦ããã¾ãã
ãé¡ã¯ããä»äºæ¶åãããã®ããã»ã¹ãããã«ãã¦(å度)ç«ã¡ä¸ããããã
å人çã«ã¯ããã¯ãcronã§æ¯åèµ·å ï¼ ã»ããã©ã§å¤éå¶å¾¡ã1æããªããã¨ã
éã§ã¯ããã¾ãããè²ã
ã¨æ¥½ã§ããããã
ããæ¹ã¯ç°¡åã§ã以ä¸ã®éãã§ãã
ã»ãä»äºæ¶åãããã¯ãæ¯åcronã§èµ·åããã(1ã¤ãããããªãããnååæã«èµ·åãããããªããããã®è¾ºã¯å¤é度ã®è¨å®ã«åããã¦ã好ã¿ã§)
ã»ãä»äºæ¶åãããã¯ã»ããã©ã§å¤éèµ·åã管çããã¦ããã®ã§ãæ¯ååããã¦ããä½ã£ãããã»ã¹ã¯å³æçµäºãã¨ãªã
ãâ ãªã®ã§ãã»ããã©ã®è¨å®ã¯ãå¾
ã¤ããããªãã¦ãããã«è½ã¡ããã«ãã
ã£ã¦ãããããªããã¨ã
ããããã¨ã¾ããcronè¨å®ãããããã§ã³ã³ããã¼ã«ãåºæ¥ãã®ã§ãæéããªãã¦æ¥½ã§ãããã¾ãã
cronã§åããã®ãã1ã¤ã®ããããã¨ãã«ãã¦ãããã§ãä½å¤éåæã«èµ·åãããããè¨å®ãããæ¸ãã¦ããããããã¨ããã½ã¼ã¹ã³ã¼ã管çã®ç¯å²ã§å
¨é¨è³ãããã§ãããããã
ãªã次ã«åºã¦ããã®ã
ã»ã³ã¼ãä¿®æ£ããã®ãå³æã«åæ ããããã ãã©ãã©ããããããã®ï¼
ã£ã¦ã®ããã£ã¦(ãã°ã¨ã)ã
ã¾ãçé¢ç®ã«èããã¨ãã·ã°ãã«ã¨ãå®è£
ããããï¼ https://www.php.net/manual/ja/function.pcntl-signal.php ãã£ã¦ãªãã®ã§ãããPHPã§ãããã¾ã§ããããï¼ãã£ã¦ã®ãããªãããªãã®ã§(å¿
è¦ãªããããã©ããã¡ããã¯)(ãPHPã§ã·ã°ãã«ãã¯ãå¾ã§Blogã«æ¸ãã¦ããã¾ãããããã)ã
ããã¡ãã£ã¨ãã£ããããã¨ãã¯
ã»graceful ã®æ©è½ãä½ã£ã¦ãã
ãâ 大ä½ããã¡ã¤ã«ãç½®ãã¦ããã¦ããã®ãã¡ã¤ã«ãåå¨ããã¨ãã¯å¦çã®loopã®æå¾ã§exitããããããã®ãã£ããå®è£
ãå¤ãã§ã
ã£ã¦ã®ãååºã«
ã»æ°ããã³ã¼ãããããã¤
ã»gracefulãã¡ã¤ã«ãscpã¨ãrsyncã¨ãã§ç½®ã
ã»ã¡ããã¨å¾
ã¤(1åãããã°ãå
¨ã¦ã®ããã»ã¹ãgracefulè¸ãã§ä¸åº¦exitããã§ãããçé¢ç®ã«ããããªãpsã§ããã»ã¹èµ·åæå»ã確èª)
ã»gracefulãã¡ã¤ã«ãsshã¨ãrsyncã¨ãã§åé¤
ã£ã¦ããã¨ãã¾ããçä»ãã¾ãã
ããããµã¼ããä½åä½ç¾ãããã¨é¢åãªã®ã§ããã2ï½3å°ã¾ã§ãããããªããããªæé (ã®åèªåå)ãããã§ããçµæ§ãããã®ã§ã¯ãªãããªããã¨ã
(大ä½èªååã§ãããã§ãå°æ°å¢ãã¦ãã¶ã£ã¡ãããããªã«å°ãã¾ãã)
ãã¨ãã¹ã±ã¼ã«ã¢ã¦ãã§ãããããµã¼ããè¤æ°å°ãããæã¨ãã¯ã
ãã®ããæ¹ã¯åºæ¬ãéåæããªã®ã§ãä¾ãã°å¤ã¨ã®éä¿¡ã®é½åããã£ã¦ãå
¨ä½ã¨ãã¦ãããç¬éã«10å¦çã¾ã§ã«æ¼ãããããå ´åãããµã¼ã1å°è¾ºãã®å¤éèµ·åæ°ããã10 / ãµã¼ãå°æ°ãã£ã¦ãã¦ããã°ãæ®éã«ã³ã³ããã¼ã«å¯è½ã§ãã
å¤åããã®ãµã¼ãã¯aã¨bã®ãããããã®ãµã¼ãã¯cã¨dã®ããããã£ã¦ããããããå
¨ä½çã«èãæãåºããããã«ããã»ãããã¡ã³ãã¨ãã楽ã§ãã1å°è½ã¡ã¦ããæ§è½ããã£ããè½ã¡ããã ãã§ãç大ãªè¢«å®³ã¨ããåºãªããªãã¾ããã
â¦â¦ãªããä»ã«ãè²ã ãã£ãæ°ãããã®ã§ãããä»åã®æ¬é¡ã¯ãLaravelã§ã®å®è£ 確èªããªã®ã§ãå説ã¯ãããããã§ã
*1:ããPHPã«ã https://www.php.net/manual/ja/function.msg-send.php ã¨ãããããããã使ããã¦ããªãã®ãï¼ãã¨åãããã¨ããã©ï½ãªãã ããããï¼ãã¨ãããªãã¨ã