ããã«ã¡ã¯ãRubyåãããããã¡ããã¢ããæä¼ããã¦ãã@willnetã¨ããã¾ãã
ã¡ããã¢ã§ã¯PHPããRubyã«ç§»è¡ããã«ããããåç´ã«è¨èªãç½®ãæããã ãã§ã¯ãªããè¨èªä»¥å¤ã®ä»çµã¿ã«ã¤ãã¦ãé©å®è¦ç´ããè¡ã£ã¦ãã¾ããä»åã¯ãã®ãã¡ã¸ã§ãã¹ã±ã¸ã¥ã¼ã©ãè¦ç´ãã件ã«ã¤ãã¦æ¸ãã¦ããã¾ãã
è¨èªãç½®ãæãã話ã¯ãã¡ããåèã«ãã¦ãã ããã
ããããã¸ã§ãã¹ã±ã¸ã¥ã¼ã©ã£ã¦ãªã«
ãæ¯æ¥1æã«ãªã£ããåæ¥ã®ã¢ã¯ã»ã¹ãã°ãéè¨ãã¦çµ±è¨ãã¼ã¿ã¨ãã¦ã¾ã¨ããããªã©ã¨ãã£ãå®æçã«å®è¡ããã¸ã§ããç»é²ããããã®ãã®ã§ãã
ã¦ã§ããµã¼ãã¹ãä½ãã¨ãã®ã¸ã§ãã¹ã±ã¸ã¥ã¼ã©ã¨ãã£ãããã£ã±ãcronã§ããããã¡ããã¢ã§ãããã¾ã§cronãæ´»ç¨ãã¦ãã¾ãããããããµã¼ãã¹ãå°ãããã¡ã¯cronã§ãããã»ã©åé¡ãªãã®ã§ããããµã¼ãã¹ãè²ã¤ã«ã¤ãã ãã ãåé¡ãé¡ã«ãªã£ã¦ãã¾ãã
cronã®åé¡ã£ã¦ãªã«
以ä¸åæãã¾ã
ã¹ã±ã¸ã¥ã¼ã«ããã¼ã¸ã§ã³ç®¡çã§ããªã
crontab -e
ãªã©ã§ç´æ¥crontabãã¡ã¤ã«ãç·¨éããå ´åãå¤æ´ã®å±¥æ´ãæ®ãã¾ãããã¤ã¾ã以åã®ãã¼ã¸ã§ã³ã«åãæ»ããããéå»ã®å±¥æ´ãæ¯ãè¿ã£ãããããã¨ãã§ãã¾ããã
ä½è«ã§ããcrontab -e
ãæã¨ãã¨ãã¦eã®é£ã®rãæ¼ãã¦ãã¾ããcrontab -r
ã§crontabã®è¨å®ãæ¶å»ãã¦ãã¾ã£ãçµé¨ã®ãã人ã¯ãã¼ã ã«ä¸äººãããããããããªãã§ããããã
ééããããè¨æ³
crontabã®è¨æ³ã¯ä¸è¦ãã¦ãããã¥ãããè¨å®ãã¹ããããããã®ã«ãªã£ã¦ãã¾ãã
ä¾ãã°æ¬¡ã®ãããªcronãè¨å®ããã¨ãã¾ãã
5 2 1 * * /your/batch/command
ããã¯æ¯æ1æ¥ã®2æ5åã«/your/batch/command
ãå®è¡ããcronã¸ã§ãã§ãããã±ã£ã¨è¦ãã ãã§ç解ããã®ã¯é£ããã¯ãªãã§ããããããã¨ã¯*/10
ã¨ã1-5,10-20
ã¨ãã®ç¹æ®è¨å·ã使ã£ãã¨ãããæ¬å½ã«ãã®æ¸ãæ¹ã§æ³å®éãã®æéã«å®è¡ã§ããã®ãä¸å®ã«ãªãã¾ãã
cronãµã¼ããåæ£ã§ããªã
ã¦ã§ããµã¼ãã¹ã§è² è·ãé«ã¾ã£ãã¨ãã«ã¯ãä»®ã«ã¢ããªã±ã¼ã·ã§ã³ãµã¼ããããã«ããã¯ã§ããã°åããµã¼ãã追å (ã¹ã±ã¼ã«ã¢ã¦ã)ãããã¨ã§è² è·ãåæ£ãããã¨ãã§ãã¾ãããããcronãµã¼ãã§åããã¨ãããã¨è¤æ°ã®ãµã¼ãã§åãã¸ã§ããåæã«å®è¡ããããã¨ã«ãªã£ã¦ãã¾ãã¾ãããã®ãã大æµã®ç°å¢ã«ããã¦cronãµã¼ãã¯1å°ã ãã§éç¨ããã¦ããã¯ãã§ãã
éçºãé²ã¿cronã¸ã§ããå¢ãã¦ããã¨ãæé帯ã«ãã£ã¦ã¯è¤æ°ã®ã¸ã§ãã1å°ã®cronãµã¼ãã§ä¸¦åã«å®è¡ããã¦è² è·ãç°å¸¸ã«é«ã¾ããäºæ³ããªãæåãé害ã«ã¤ãªãã£ã¦ããã¾ãã
ãããã°ãã¥ãã
cronã®ç°å¢ã¨ãæ®æ®µä½¿ã£ã¦ããã·ã§ã«ã®ç°å¢ã¨ã®éãã§ã¸ã§ãã失æãããã¨ã¯ããããã¾ãããç¹ã«ããã示åãããããªåºåã¯ãªããã¨ãå¤ãã§ãããã®ãããã®æã®çµé¨ããªãã¨ãæå ã§å®è¡ããã¨åãã®ã«ãªããcronã ã¨åããªãï¼ãªãï¼ï¼ãã®ããã«ãããã¾ãã
ãã°ã追ãã¥ãã
cronãå®è¡ããéã®åºåã¯ã¡ã¼ã«ã§éä¿¡ããã¾ããã2017å¹´ã®ç¾å¨ãã®æ©è½ã使ã£ã¦ããæ¹ã¯ãã¾ãããªãã®ã§ã¯ãªãã§ãããããæ®æ®µã¯èªåã§ãã°ãé å¼µã£ã¦åºåãããã®ãä¿æãã¦ãããã¨ã©ã¼ãèµ·ããã¨ãã«ã¯Airbrakeãªã©ã®ã¨ã©ã¼ç®¡çã·ã¹ãã ã¨é£æºãã¦éç¥ããã°ãæ¼ã£ã¦åå ã究æããâ¦ã¨ããã±ã¼ã¹ãå¤ããã§ãã
ã¸ã§ããã¨ã«æåï¼å¤±æã®ã¹ãã¼ã¿ã¹ããã°ãã¾ã¨ã¾ã£ã¦ããã¨ä¾¿å©ã§ãããããã¾ã§èªåã§å®è£ ããã®ã¯å¤§å¤ã§ããã
ã©ããã£ã¦åé¡ã解決ããã
cronã®åé¡ã«ã¤ãã¦ããããæ¸ãã¾ããããã©ã®ããã«ããããããã®åé¡ã解決ã§ããã®ã§ãããããcronãã®ãã®ãæ¹åãããæ¹åã¨ãcronããããæ¹åã§èãã¦ã¿ã¾ãã
whenever 㧠cron ãæ¹åããã
ããã¾ã§æããcronã®åé¡ã®ãã¡ããã¤ãã¯wheneverã¨ããgemãå©ç¨ãããã¨ã§æ¹åå¯è½ã§ãã使ã£ã¦ããæ¹ãå¤ãã®ã§ã¯ãªãã§ããããã
wheneverã使ãã¨æ¬¡ã®ãããªè¨æ³ã§ã¸ã§ãããã¡ã¤ã«ã«å®ç¾©ããcrontabç¨ã®è¨æ³ã«å¤æãã¦ç»é²ãããã¨ãã§ãã¾ãã
every 1.day, :at => '4:30 am' do runner "MyModel.task_to_run_at_four_thirty_in_the_morning" end
ãã®ãã¡ã¤ã«ããã¼ã¸ã§ã³ç®¡çãã¦ããããããã¤æã«èªåã§crontabãæ´æ°ããããã«ãã¾ããããã§ããã¼ã¸ã§ã³ç®¡çã®åé¡ã¨ãè¨æ³ã®åé¡ã解決ã§ãã¾ããã
ããããä»ã®åé¡ã¯å¤ãããæ®ã£ã¦ãã¾ãã
cron ãããã
調ã¹ãã¨ãcronãç½®ãæãããã¨ããããã¸ã§ã¯ãã¯ãããããããã¨ããããã¾ãã
- Jobber
- Rocket Job
- sidekiq-scheduler
- sidekiq-cron
- Sidekiq (enterpriseç)
- crono
- resque-scheduler
- clockwork
ä¸è¨ã¯ãªãã¹ãRubyã§ä½ããããã¼ã«ã®ä¸ããé¸ã³ã¾ãããã¤ã¾ãRubyã«ãã ãããªããã°ãã£ã¨ããããã®é¸æè¢ãããããã§ããæ©ã¿ã¾ããï¼ï¼ã
å ¨ã¦ã®åé¡ãå®å ¨ã«è§£æ±ºãããã¼ã«ã¯åå¨ããªãã£ãã®ã§ã次ã®ãããªè¦³ç¹ã§ãã¼ã«ãé¸å®ãã¾ããã
- é©åº¦ã«åé¡ã解決ãã¦ãããã¨
- Ruby ã使ã£ãããã¸ã§ã¯ãã§ãããã¨
- ã¡ã³ããã³ã¹ãç¶ãã¦ãããã¨
- ææªã¡ã³ããã³ã¹ãåæ»ãã¦ãèªåãã¡ã§ãªãã¨ãã§ãããã¨
- å°å ¥ã®ãããã
çµæã¨ãã¦ãsidekiq-cronãæ¡ç¨ãããã¨ã«ãã¾ããã
sidekiq-cron
sidekiq-cron ã¯ããã¯ã°ã©ã¦ã³ãã¸ã§ããæ±ãgemã§ããsidekiqãæ¡å¼µããcronçãªæ©è½ã追å ãã¦ãããgemã§ãã
主ãªã¡ãªããã¨ãã¦ã¯ã
- sidekiqã«ç¸ä¹ãããå½¢ã§å©ç¨ã§ããã®ã§å°å ¥ã楽
- ã¸ã§ã失ææã«ãªãã©ã¤ããããã¨ãã§ãã
- sidekiqã®ã¯ã¼ã«ã¼ãå¢ãããã¨ã§è² è·åæ£ã§ãã
- yamlã§ã¹ã±ã¸ã¥ã¼ãªã³ã°ãå®ç¾©ããã®ã§ã¹ã±ã¸ã¥ã¼ã«ã®ãã¼ã¸ã§ã³ç®¡çãã§ãã
- sidekiqãweb uiãç¨æãã¦ããã®ã§ãã¸ã§ãã®ç¶æ ã確èªã§ãã*1
ãªã©ãããã¾ããæ £ãã¦ããsidekiqããã®ã¾ã¾ä½¿ãæè¦ã§ãããã®ãããã§ããã
æéãæå®ããè¨æ³ã¯crontabã¨åãã ã£ããã詳細ãªãã°åºåã¯ç¬èªã§é å¼µãå¿ è¦ãããã¾ãããcronåä½ã®æããã¯ã ãã¶åé²ã§ããæ°ããã¾ãã
å°æ¥çã«ã©ãããã
sidekiq-cronã¯æ¬çªå°å ¥ããã¦ãããä»ã®æ®µéã§ã¯ç¹ã«åé¡ã¯ãªãã®ã§ãããé ãã¦ãæ°å¹´ãããä¹ãæããæ¤è¨ããå¿ è¦ãããããã§ãã
ä¾ãã°è¤æ°ã®Railsã¢ããªã管çããããã«ãªã£ãå ´åã«ãè¤æ°ã¢ããªã横æããã¸ã§ãã¹ã±ã¸ã¥ã¼ã©ã管çãããã¨ããè¦æã«ã¯sidekiq-cronã§ã¯å¿ãããã¾ãããã¾ããã¸ã§ãã®æ°ãæ°åãç¾ã«ãªã£ãå ´åã«ã¸ã§ããã©ããã£ã¦ç®¡çããããæ©ã¾ããã¨ããã§ããããã°ã®åºåãå¼±ããã¨ãåé¡ã¨ãªãã±ã¼ã¹ãä»å¾åºã¦ããã§ãããã
ã¨ããããªæã«æ¨å¹´OSSåãããkuroko2ã軽ã触ã£ã¦ã¿ãã¨ããããªããªãè¯ãããã§ãã*2ã次åä¹ãæããæ¤è¨ããéã®æåãªåè£ã¨ãªãããã§ãã
kuroko2ã®è©³ç´°ã¯ä»¥ä¸ã®ãªã³ã¯ãåèã«ã©ããã
- ã¯ãã¯ãããã®ã¸ã§ã管çã·ã¹ãã kuroko2 ã®ç´¹ä» - ã¯ãã¯ãããéçºè ããã°
- OSS ã«ãªã£ã Kuroko2 ãã©ããããæ©ãå°å ¥ããã®ã§ç´¹ä»ãããããã° â ã¿ããªã®ã¦ã§ãã£ã³ã°ã¨ã³ã¸ãã¢ãªã³ã°ããã°
- OSSã®ã¸ã§ã管çã·ã¹ãã Kuroko2ã使ã£ã¦ã¿ã ï½ Developers.IO
ã¾ã¨ã
éçºãã¦ããã¢ããªã±ã¼ã·ã§ã³ã®è¦æ¨¡ã«ãã£ã¦é©åãªãã¼ã«ã¯å¤ãã£ã¦ãã¾ããå°è¦æ¨¡ãªã¢ããªã±ã¼ã·ã§ã³ã®ã¸ã§ãã¹ã±ã¸ã¥ã¼ã©ã§ããã°cronã¯ã¾ã£ããåé¡ãªãã¨æãã¾ãããã¢ããªã±ã¼ã·ã§ã³ãæé·ãã¦ããã«ã¤ãã¦ããé©ãããã¼ã«ã«ä¹ãæãã¦ããå¿ è¦ãããã§ãããããã®ã¨ã³ããªã次ã®ãã¼ã«é¸å®ã®åèã«ãªãã°å¹¸ãã§ãã
*1:sidekiqã®ä»æ§ä¸ãWebUIä¸ã«ãã°ã¯ã»ã¼æ®ããªãã®ã§ãsidekiq-failuresãå©ç¨ãã¦å¤±æããã¸ã§ãã®ã¨ã©ã¼ãé²è¦§ã§ããããã«ãã¦ãã¾ãã