ThinkPHP6ÖÐÔõÑùʹÓÃÐÐÁÐʵÏÖÒ첽ʹÃü£¿
Ëæ×Å»¥ÁªÍøÓ¦ÓúÍÐÅϢϵͳµÄÒ»Ö±Éú³¤£¬Ðí¶àÓªÒµ¹ØÓÚ´¦ÀíÖØ´óÂß¼»òÐÔÄÜÒªÇó½Ï¸ßµÄ²Ù×÷¾ÍÐèҪʹÓõ½Ò첽ʹÃü¡£È»¶ø£¬¹Å°åµÄͬ²½´¦Àí·½·¨»á¶ÔϵͳÐÔÄÜ´øÀ´½Ï´óµÄѹÁ¦ºÍ¸ººÉ£¬ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃÐÂÎÅÐÐÁÐÀ´ÊµÏÖÒ첽ʹÃü´¦Àí¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6¿ò¼ÜÖÐʹÓÃÐÐÁÐʵÏÖÒ첽ʹÃü¡£
Ò»¡¢×°ÖÃÉèÖÃ
1.1 ÏÂÔØ×°ÖÃÀ©Õ¹
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃQueue×é¼þÀ´ÊµÏÖÐÐÁд¦Àí¡£Í¨¹ýÔÚcomposer.jsonÎļþÖÐÌí¼ÓÒÀÀµÏîÀ´¾ÙÐÐ×°Öã¬ÈçÏÂËùʾ£º
composer require topthink/think-queue
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1.2 ÉèÖÃÎļþ
×°ÖÃÀֳɺó£¬ÎÒÃÇÐèÒªÔÚÏîÄ¿ÖÐÌí¼ÓÉèÖÃÎļþ£¬²¢¾ÙÐÐÏà¹ØÉèÖá£ÔÚconfigĿ¼ÏÂÐÂÔöqueue.phpÎļþ£¬ÔÚÆäÖÐÌí¼ÓÒÔÏÂÉèÖÃÏ
return [ 'default' => env('queue.driver', 'sync'), 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'redis' => [ 'driver' => 'redis', 'queue' => 'default', 'connection' => 'default', 'retry_after' => 90, 'block_for' => null, ], ], ];
µÇ¼ºó¸´ÖÆ
ÕâÀïÖ÷ÒªÊÇÉèÖÃÐÐÁеÄĬÈÏÇý¶¯ºÍÅþÁ¬·½·¨¡£ÆäÖУ¬ÎÒÃÇÖ§³ÖÁ½ÖÖÇý¶¯·½·¨£ºÍ¬²½(sync)ºÍRedis(redis)¡£RedisÊÇÒ»¸öÂþÑÜʽµÄÄÚ´æÊý¾Ý¿â£¬Ëü¿ÉÒÔÖ§³ÖÖÖÖÖÊý¾Ý½á¹¹£¬ÓÈÆäÊǼüÖµ¶Ô¡¢ÁÐ±í¡¢ÜöÝÍ¡¢¹þÏ£µÈÊý¾Ý½á¹¹¡£ÔÚThinkPHP6µÄQueue×é¼þÖУ¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃRedisÇý¶¯À´×÷ΪÐÂÎÅÐÐÁеĴ洢·½·¨¡£
ÁíÍ⣬ÎÒÃÇ»¹¿ÉÒÔ¶ÔÐÐö¾ÙÐÐһЩÆäËûÉèÖ㬺ñÈÐÐÁÐÃû×Ö(queue)¡¢ÅþÁ¬Ãû×Ö(connection)¡¢ÖØÊÔʱ¼ä(retry_after)µÈ¡£
ÒÔÉÏÉèÖÃÏîÒ²¿ÉÒÔÔÚÓ¦ÓÃÉèÖÃÎļþ£¨config/app.php£©»òÕßÇéÐÎÉèÖÃÎļþÖоÙÐÐÉèÖá£
¶þ¡¢½¨ÉèÐÐÁÐʹÃü
ÔÚThinkPHP 6µÄÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔʹÓù¤³§Ä£Ê½À´½¨ÉèÐÐÁÐʹÃü£¬Í¬Ê±¿ÉÒÔͨ¹ý¼ÌÐøJobÀàÀ´ÊµÏÖÏêϸµÄʹÃüÂß¼¡£
2.1 ½¨É蹤³§
ÎÒÃÇ¿ÉÒÔÔÚapp/jobĿ¼Ï½¨ÉèJob.phpÎļþ£¬²¢½ç˵һ¸öÐÂÎÅÐÐÁеŤ³§À࣬ÔÚÆäÖÐʵÏÖ´¦ÀíÏêϸÐÐÁÐÐÂÎŵÄÒªÁìhandle¡£ÏêϸʵÏÖÈçÏ£º
namespace appjob; use thinkqueueJob; class MyJob { public function handle(Job $job, $data) { //... ÏêϸʹÃü´¦ÀíÂß¼ //... Ö´ÐÐÀֳɣ¬É¾³ý¸ÃÐÂÎÅ $job->delete(); } }
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃǽç˵ÁËÒ»¸öMyJobÀ࣬ÆäÖÐhandleÒªÁìÈÏÕæÏêϸµÄÐÐÁÐÐÂÎÅÂß¼µÄ´¦Àí¡£µ±Ö´ÐÐÀֳɺó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýŲÓÃ$job->delete()ÒªÁìÀ´É¾³ýÕâÌõÐÐÁÐÐÂÎÅ¡£
2.2 ½¨ÉèʹÃü
ÎÒÃÇ¿ÉÒÔͨ¹ý¼ÌÐøJobÀ࣬À´½¨ÉèÎÒÃÇÐèÒª´¦ÀíµÄʹÃü¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öSendEmailÀ࣬ʹÓÃÕâ¸öʹÃüÀ´·¢ËÍÓʼþ¡£
namespace appjob; use thinkqueueJob; class SendEmail extends Job { public function handle() { // ...ÏêϸµÄÓʼþ·¢ËÍÂß¼ // ...ʹÃüÖ´ÐÐÍê³É£¬É¾³ý¸ÃÐÂÎÅ $this->delete(); } }
µÇ¼ºó¸´ÖÆ
ÔÚhandleÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔ±àдÏêϸµÄÓʼþ·¢ËÍÂß¼¡£Í¬Ê±£¬ÎÒÃÇÒ²¿ÉÒÔÔÚ×îºóŲÓÃdeleteÒªÁìÀ´É¾³ýÒѾִÐÐÀֳɵÄÐÐÁÐÐÂÎÅ¡£
Èý¡¢½«Ê¹ÃüÌí¼Óµ½ÐÐÁÐÖÐ
µ±ÎÒÃǽ¨ÉèºÃÐÐÁÐʹÃüºó£¬ÎÒÃÇÐèÒª½«ÆäÌí¼Óµ½ÐÂÎÅÐÐÁÐÖУ¬ÒÔ±ãºóÐøÒì²½´¦Àí¡£ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÐÐÁÐЧÀÍproviderÀ´Ìí¼ÓʹÃü¡£
app('queue')->push(new SendEmail());
µÇ¼ºó¸´ÖÆ
´Ë´¦£¬ÎÒÃÇͨ¹ýŲÓÃ$app[‘queue’]£¬À´»ñÈ¡ÐÐÁÐЧÀÍʵÀý£¬Í¨¹ýpushÒªÁ콫SendEmailʹÃüÌí¼Óµ½ÐÐÁÐÖС£
ËÄ¡¢Ê¹Ãü¼à¿ØºÍÖ´ÐÐ
ÔÚʹÃüÌí¼Óµ½ÐÐÁÐÖкó£¬ÎÒÃÇÐèÒªÄܹ»ÊµÊ±ÏàʶʹÃü״̬²¢ÊµÊ±´¦Àí¡£Õë¶ÔÕâ¸öÐèÇó£¬ÎÒÃÇ¿ÉÒÔʹÓÃThinkPHP6µÄArtisan Console¹¤¾ß¡£ËüÊÇ»ùÓÚSymfony Console×é¼þµÄÀ©Õ¹£¬Ö§³ÖÎÒÃÇͨ¹ý¿ØÖÆִ̨ÐÐһЩÌض¨µÄÏÂÁî¡£
4.1 Æô¶¯ÐÐÁмàÌý
ÎÒÃÇ¿ÉÒÔÆô¶¯¿ØÖÆ̨£¬Ö±½ÓÔÚÏÂÁîÐÐÖ´ÐÐÒÔÏÂÏÂÁ
php think queue:work --daemon --queue default
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬–queueÖ¸¶¨ÐÐÁеÄÃû³Æ£¬¿ÉÒÔ×Ô½ç˵£¬–daemonÌåÏÖÔÚºǫ́ÔËÐС£
Õâ¸öÏÂÁîÖ´Ðкó¾Í»áÆô¶¯ÐÐÁмàÌý£¬½«ÐÐÁÐÖеÄÐÂÎÅÖðÒ»¾ÙÐд¦Àí¡£
4.2 ¼à¿ØʹÃüÖ´ÐÐ״̬
Ö´ÐÐÐÐÁеÄÀú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔʹÓüàÊÓÆ÷À´Éó²éÐÐÁеÄÖ´ÐÐÇéÐΡ£ÔÚÏÂÁîÐÐÖ´ÐÐÒÔÏÂÏÂÁ
php think queue:listen --queue default --tries=3
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬–queueÖ¸¶¨ÐÐÁеÄÃû³Æ£¬–triesÖ¸¶¨Ê¹ÃüÖØÊÔ´ÎÊý¡£
Ö´Ðкó»áÊä³öÄ¿½ñÐÂÎÅÐÐÁеÄ״̬ºÍÏêϸִÐÐÇéÐΡ£ÎÒÃÇ¿ÉÒÔƾ֤Êä³öµÄÐÅÏ¢£¬ÊµÊ±¶ÔʹÃüµÄ״̬¾ÙÐмà¿ØºÍ´¦Àí¡£
Îå¡¢×ܽá
ͨ¹ýʹÓÃÐÐÁÐʵÏÖÒ첽ʹÃü£¬ÎÒÃÇ¿ÉÒÔÓÐÓõÄÌáÉýϵͳµÄÐÔÄܺÍÎȹÌÐÔ¡£±¾ÎÄÖ÷ÒªÏÈÈÝÁËÔÚThinkPHP6ÖÐÔõÑùʹÓÃÐÐÁÐʵÏÖÒ첽ʹÃü£¬²¢ÇÒ¶ÔÐÐÁеÄÉèÖá¢Ê¹ÃüµÄ½¨Éè¼°Ìí¼Ó¡¢ÐÐÁеļà¿ØºÍÖ´ÐоÙÐÐÁËÏêϸµÄ˵Ã÷¡£Ï£ÍûÄܹ»¶Ô¸÷ÈËÔÚÏÖʵӦÓÃÖд¦ÀíÒ첽ʹÃüʱÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʹÓÃÐÐÁÐʵÏÖÒ첽ʹÃü£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡