Qiita Laravel #2 Advent Calendar 2019 17æ¥ç®ã®è¨äºã§ãã
ããã«ã¡ã¯ãM&Aã¯ã©ã¦ãã®æ´¥å´ã§ãã
ä»æ¥ã¯ãAWS Elastic Beanstalk Workerç°å¢ã§Laravelãåããã¦ããã¥ã¼å¦çãå®æå®è¡å¦çã®å°ç¨ç°å¢ãæ§ç¯ãã話ããã¾ãã
æåã¯Qiitaã«æ稿ããäºå®ã§è¨äºãæ¸ãã¦ãããã§ãããèªç¤¾ã®ç°å¢ã«éã£ã話ãå¤ãã£ãã®ã§ãèªç¤¾ããã°ã§å ¬éãã¾ããã
è¨äºã®æå¾ã«ã¯ãæ¬çªç°å¢ã§ããããã¡ãã£ã
話ãããã¾ãã®ã§ãLaravel ã§ã¯ã¼ã«ã¼ç°å¢ã®æ§ç¯ãæ¤è¨ããã¦ããæ¹ã¯ãã§ãã¯ãã¦ããã ãããã¨æãã¾ãã
å¼ç¤¾ãéç¨ãã¦ããM&Aã¯ã©ã¦ãã¨ããã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ã¯ãAWS Elastic Beanstalkã®ã¦ã§ããµã¼ãã¼ç°å¢ã§æ§æããã¦ãã¾ãã
AWS Elastic Beanstalkã¯ããæ軽ã«Webã¢ããªã±ã¼ã·ã§ã³æ§ç¯ã§ãããã¤ã³ãã©ã«æãåããªãã¹ã¿ã¼ãã¢ããä¼æ¥ã«ã¯ã¨ã¦ããããããAWSã®ãµã¼ãã¹ã§ãã åé·åããªã¼ãã¹ã±ã¼ãªã³ã°ãAWSã®Webã³ã³ã½ã¼ã«ä¸ããç°¡åã«æ§ç¯ã»è¨å®ãããã¨ãã§ãã¾ãã ï¼å¼ç¤¾ã®ã¤ã³ãã©ãæå¼·ã«ãã¦ã¿ããã¨ã³ã¸ãã¢ã®æ¹ããå¾ ã¡ãã¦ã¾ãðââï¸ï¼
ãµã¼ãæ§æ
å°å ¥åã®æ§æ
å°å ¥å¾ã®æ§æ
ã¯ã¼ã«ã¼ç°å¢ãå°å ¥ãã以åã¯ããã¥ã¼å¦çãå®æå®è¡ã®ã¿ã¹ã¯ãªã©ã®ããã¯ã°ã©ã¦ã³ãå¦çãã¦ã§ããµã¼ãç°å¢ã§å®è¡ãã¦ãã¾ããã
ããã¯ã°ã©ã¦ã³ãå¦çã®ããªã¬ã¼ã¯ãCloudWatch Events
ã使ã£ã¦ãã¾ãã
CloudWatch Events
ããLambda
ãå®è¡ããHTTPã«ãã£ã¦Laravel製ã®ã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ã®ç¹å®URLã«ã¢ã¯ã»ã¹ãã¦ãã¾ãã
ã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ã¯ãç¹å®URLã«ã¢ã¯ã»ã¹ãããæã«ããã¯ã°ã©ã¦ã³ãå¦çãéå§ãã¾ãã
å³ã§ã¯ãããã¯ã°ã©ã¦ã³ãå¦çã®ä»£è¡¨çãªä¾ã¨ãã¦ãAmazon SQS
ãããã¥ã¼ãåãåã£ã¦å¦çãè¡ããAmazon SNS
ãå¼ã³åºãã¦ã¦ã¼ã¶ã«ã¡ã¼ã«ãéä¿¡ããã±ã¼ã¹ã表ãã¦ãã¾ãã
æ§æä¸ã®åé¡
ã¦ã§ããµã¼ãç°å¢ã§ããã¯ã°ã©ã¦ã³ãå¦çããæ§æã«ã¯ããã¤ãã®åé¡ãããã¾ããã
- ã»ãã¥ãªãã£ã«ã¤ãã¦
- ã¿ã¹ã¯å®è¡ã®ããã®URLãå¤é¨ã«é²åºãã¦ãããURLãæµåºãã¢ã¯ã»ã¹ãããã¨ä¸æ£ã«å¦çãå®è¡ããã¦ãã¾ããªã¹ã¯ããã
- æ§è½ã¤ãã¦
- ãã¥ã¼å¦çã¯1åã«ä¸åã100åã®ãã¥ã¼ãåå¾ãã¦å¦çããããã«å®è£
ãã¦ããããããã¥ã¼ãæ°ç¾åç©ã¾ããã¨å¦çãè©°ã¾ã£ã¦ãã¾ã
- ä¾ãã°ã2000ééãå ´åãã¡ã¼ã«éä¿¡ã«20åããã£ã¦ãã¾ãããã®éä»ã®ã¡ã¼ã«ãéãããªããªã©
- â»ããã¯å®è£ ä¸ã®åé¡ãªã®ã§ä¸æãä½ãã°ãã£ã¨å¹ççã«ãªããããã³ã¨å®è£ ãããã¨ããã¨ãã§ããã ãºã«ã·ã¤
- ãã¥ã¼å¦çã¯1åã«ä¸åã100åã®ãã¥ã¼ãåå¾ãã¦å¦çããããã«å®è£
ãã¦ããããããã¥ã¼ãæ°ç¾åç©ã¾ããã¨å¦çãè©°ã¾ã£ã¦ãã¾ã
- è² è·ã«ã¤ãã¦
- å°æ¥çã«å©ç¨è ãå¢ãã¦ããã¨ãã¦ã§ããµã¼ãã«ãããããã¯ã°ã©ã¦ã³ãå¦çã«ããè² è·ãç¡è¦ã§ããªããªã
ãããã®åé¡ã¯ã¯ã¼ã«ã¼ç°å¢ã使ããªãã¦ãæ§ã ãªå·¥å¤«ã§æ¹åã§ãã¾ããã ã¯ã¼ã«ã¼ç°å¢ã«ã¯ãããã®åé¡ãä¸åº¦ã«è§£æ±ºã§ããã¨ããã¡ãªããããã£ãã®ã§æ¡ç¨ãã¾ããã
AWS Elastic Beanstalk ã¯ã¼ã«ã¼ç°å¢ã¨ã¯
Beanstalkã«ã¯ãï¼ã¤ã®ç°å¢æ
ãããã¾ãã
1ã¤ã¯ãé常ã®ã¦ã§ããµã¼ãã«ç¨ããããã¦ã§ããµã¼ãã¼ç°å¢
ã§ãã
ãã1ã¤ããã¯ã¼ã«ã¼ç°å¢
ã§ãã
ã¯ã¼ã«ã¼ç°å¢ã¯ãã¦ã§ããµã¼ãç°å¢ã¨éããSQSãã¼ã¢ã³ðã¨ãããAmazon SQSãããã¥ã¼ãèªã¿åãããã¼ã«ã«ã®ãã¼ã80ã«HTTP POSTãªã¯ã¨ã¹ããéã£ã¦ããããã¼ã¢ã³ããã»ã¹
ãåã¤ã³ã¹ã¿ã³ã¹å
ã«å¸¸é§ãã¦ãã¾ãã
èªåã®ã¢ããªã±ã¼ã·ã§ã³ã§SQSãã¼ã¢ã³ããPOSTããããã¼ã¿ãå¦çããããã«å®è£
ããã°ãããã¯ã°ã©ã¦ã³ãå¦çç¨ã®ãµã¼ãã®å®æã«ãªãã¾ãã
(https://docs.aws.amazon.comããå¼ç¨)
https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/using-features-managing-env-tiers.html
ã¯ã¼ã«ã¼ç°å¢ã®å°å ¥
ã¯ã¼ã«ã¼ç°å¢ã®å°å
¥ã«ã¯ãSQSã®ãã¥ã¼ãåãåãã¨ã³ããã¤ã³ããä½ãå¿
è¦ãããã¾ãã
ãã®ã¨ã³ããã¤ã³ãã®ä½æã«ã¯ãdusterio/laravel-aws-worker
ã¨ããããã±ã¼ã¸ãå©ç¨ãã¾ããã
ã¤ã³ã¹ãã¼ã«ããã ãã§/worker/queue
ã¨ããã¨ã³ããã¤ã³ããä½æããã¾ãã
ãã¡ããã¡ã便å©ã§ãã
https://github.com/dusterio/laravel-aws-worker
ãã¨ã¯ãã¯ã¼ã«ã¼ç°å¢ãä½ã£ã¦å種è¨å®ãããã°OK!
å°å ¥çµæ
ã¯ã¼ã«ã¼ç°å¢ã®å°å ¥ã«ãã£ã¦ãå½åæ±ãã¦ããåé¡ã¯è§£æ±ºããã¾ããã
- ã»ãã¥ãªãã£ã«ã¤ãã¦
- ã¯ã¼ã«ã¼ç°å¢ã¯ããã©ã«ãã§ã¤ã³ã¿ã¼ãããããã¢ã¯ã»ã¹ã§ããªã
- æ§è½ã«ã¤ãã¦
- ãã¥ã¼ã並åã«æ大100åå¦çã§ããã®ã§ããªã½ã¼ã¹ã®å©ç¨å¹çããããªãå¦çãæ©ããªã£ã
- 並åæ°ãé«ãã¦ãSNSã§è©°ã¾ã£ã¦ãã¾ãã®ã§ãå¾ã ã«ä¸¦åæ°ãå¢ããã¦ãã
- ä»ã®æï¼ä¸¦åã§éç¨ãã¦ãããã¯ã¼ã«ã¼ç°å¢å°å ¥åããã¡ã¼ã«ã®ä¸æ¬éä¿¡ã4.8åãæ©ããªã£ã
- è² è·ã«ã¤ãã¦
- ããã¯ã°ã©ã¦ã³ãå´ã®è² è·ã¯ãã¦ã§ãã¢ããªã±ã¼ã·ã§ã³å´ã«å ¨ãããããªããªã£ã
æ¬çªç°å¢ã§ããããã¡ããã¾ãã
ã¯ã¼ã«ã¼ç°å¢å°å ¥ã«ããã£ã¦ãæ¬çªç°å¢ã§ã¦ã¼ã¶ã¼ã«è¿·æãããã¦ãã¾ããããªä¸å ·åãçºçãã¦ãã¾ãã¾ããã Laravelã§ã¯ã¼ã«ã¼ç°å¢ãå°å ¥ããæ¹ã¯ãåãéã¡ãç¯ããªãããã«åå注æãã¦æ¬²ããã§ãð¢
çºçããç¾è±¡
ã¯ã¼ã«ã¼ç°å¢ããéä¿¡ãããã¡ã¼ã«ã«ããã¦ãã¡ã¼ã«å ã®ãã¿ã³åä½ããªãã¨ã¦ã¼ã¶ããé£çµ¡ãããã¾ããã
ãªãã¨ãã¡ã¼ã«ã«è¡¨ç¤ºããããã¿ã³ã®ãªã³ã¯å
ãhttp://localhost/
ããå§ã¾ãURLã«ãªã£ã¦ãã¾ã£ã¦ããã®ã§ãã
åå
Laravelã®ããã©ã«ãã®è¨å®ã§ã¯ãèªãµã¤ãã®URLçæã®æã«ããªã¯ã¨ã¹ãæã®ãã¡ã¤ã³åãå©ç¨ããããã«ãªã£ã¦ãã¾ãã
ã¦ã§ããµã¼ãç°å¢ã§ã¯ãã¦ã¼ã¶ããmacloud.jp
ã¨ãããã¡ã¤ã³ã§ã¢ã¯ã»ã¹ãããã®ã§ãèªãµã¤ãã®URLãçæããã¨macloud.jp
ã¨ãããã¡ã¤ã³ã®URLãçæããã¾ãã
ä¸æ¹ãã¯ã¼ã«ã¼ç°å¢ã§ã¯ãSQSãã¼ã¢ã³ãLaravelã«å¯¾ãã¦localhost
ã§ã¢ã¯ã»ã¹ãè¡ãã¾ãã
ãã®ãããLaravelã¯èªãµã¤ãã®URLãhttp://localhost~
ã¨ãã¦çæãã¦ãã¾ã£ãã®ã§ãã
対ç
app/Providers/AppServiceProvider.php
ã®boot()
ã¡ã½ããã«ã
ç°å¢å¤æ°ã§æå®ããã«ã¼ãURLã使ç¨ããããã«è¿½è¨ãã¾ããã
public function boot() { { if (env('REGISTER_WORKER_ROUTES', true)) { URL::forceRootUrl(config('app.url')); } // ç¥
åç
大ããªæ©è½ã®éçºã®æã«ã¯ãã¨ã³ã¸ãã¢ã¨ãããã¯ãããã¼ã¸ã£ã¼ãéã¾ã£ã¦ãæ©è½ããã°ã¬ã¼ãã®ç¢ºèªä¼ãå¿ ãè¡ãããã«ãã¦ãã¾ãã ããããä»åã®ã¤ã³ãã©ã®å¤æ´ã¯ãã³ã¼ãã®å¤æ´ãå°ãªããã¡ã¼ã«ãéãããã°å¤§ä¸å¤«ã ããã¨ãã¼ã å ¨å¡ãæã£ã¦ãããããæ å½è ã¬ãã«ã§ã®ç¢ºèªã«çã¾ã£ã¦ãã¾ããã
ãã¼ã¹URLãç°ãªãã¨ããä¸å ·åã¯ãäºåã«ãã®å¯è½æ§ã«æ°ã¥ãã®ãé£ããããã¹ãã³ã¼ããæ¸ãã¦ãããã¨ã¯å°é£ã§ãã®ã§ãã©ããã¦ã人éã®æã«ãããã§ãã¯ãå¿ è¦ã«ãªãã¾ãã
ä»å¾ã¯ãã¤ã³ãã©ã®å¤æ´ã«ããã¦ã確èªä¼ãå®æ½ããå¨è¾ºã®æ©è½ãå«ãã¦ã®ç¢ºèªãå¾¹åºãã¦ããã¾ãï¼
ã¾ã¨ã
- ã¯ã¼ã«ã¼ç°å¢ã¯ããã¥ã¼å¦çãå®æå®è¡ãªã©ã®ããã¯ã°ã©ã¦ã³ãå¦çãè¡ãããã®ã«ä¾¿å©
dusterio/laravel-aws-worker
ã§ç°¡åã«å°å ¥ã§ãã- ãã¼ã¹URLã«æ°ãã¤ãã¦
æå¾ã«
æã ã¯ãä¸ç·ã«ã¤ã³ãã©ãæ¹åããã¦ãããã¨ã³ã¸ãã¢ãéæåéä¸ã§ãã www.wantedly.com