laravelÖÐÐÐÁжà¾ÃÏûºÄÒ»´Î
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ÆäÖеÄÐÐÁÐϵͳ¿ÉÒÔÈÃÎÒÃÇÀû±ãµØ´¦Öóͷ£Ò»Ð©Ò첽ʹÃü£¬Èç·¢ËÍÓʼþ¡¢ÌìÉú±¨±íµÈ¡£¶ø¹ØÓÚÐÐÁеÄÏûºÄƵÂÊ£¬ÕâÊÇÒ»¸öºÜÏÖʵµÄÎÊÌ⣬ҲÊÇÐí¶à¿ª·¢ÕßÌåÌùµÄÎÊÌâ¡£
Ê×ÏÈ£¬Ã÷È·LaravelÐÐÁеĻùÀ´Ô´ÀíÊǺÜÖ÷ÒªµÄ¡£LaravelÐÐÁÐϵͳÊÇ»ùÓÚÐÂÎÅÐÐÁеģ¬Ëüͨ¹ý½«ÐèÒª´¦Öóͷ£µÄʹÃü·ÅÈëÐÐÁÐÖУ¬ÔÙͨ¹ýÒì²½Àú³ÌÐÐÖ¹ÖÃÕâЩʹÃü¡£Õâ¸öÀú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÐÐÁеġ°ÅþÁ¬Æ÷¡±£¬À´Ñ¡ÔñʹÃüµÄÐÂÎÅÐÐÁÐЧÀÍ£¬ÀýÈçRedis¡¢RabbitMQµÈ¡£
ÄÇô£¬ÐÐÁеÄÏûºÄƵÂÊÊÇÔõÑù¿ØÖƵÄÄØ£¿
ĬÈÏÇéÐÎÏ£¬LaravelÐÐÁÐÔÚÖ´ÐÐphp artisan queue:workÏÂÁîʱ£¬½«Ò»Ö±µØÂÖѯÐÐÁÐÖеÄʹÃü²¢Ö´ÐС£Õâ¸öÂÖѯʱ¼ä¾àÀ룬LaravelĬÈÏÊÇ3Ãë¡£
ÏÖʵÉÏ£¬Õâ¸öÂÖѯʱ¼ä¾àÀë¿ÉÒÔͨ¹ýÉèÖÃÐÐÁÐÊÂÇéÀú³ÌµÄÐÝÃßʱ¼äÀ´¿ØÖÆ¡£Õâ¸öÐÝÃßʱ¼ä¿ÉÒÔÔÚÐÐÁеÄÓ¦ÓóÌÐòÉèÖÃÎļþÖÐÉèÖã¬ÀýÈçÔÚ.envÖÐÉèÖãº
QUEUE_TIMEOUT=30
µÇ¼ºó¸´ÖÆ
¶øÕâ¸öʱ¼ä¾àÀ룬ÔÚLaravelÖб»³ÆΪ¡°ÐÐÁеij¬Ê±Ê±¼ä¡±¡£
ÕâÒâζ×Å£¬ÔÚÿһ´ÎµÄʹÃü´¦Öóͷ£¿¢Êºó£¬ÐÐÁÐÊÂÇéÀú³Ì½«»áÐÝÃß30Ã룬ȻºóÔٴμì²éÐÐÁÐÖÐÊÇ·ñÓдý´¦Öóͷ£µÄʹÃü¡£
ÏêϸÀ´Ëµ£¬ÈôÊÇÎÒÃÇÔÚLaravelÖнç˵ÁËÈçϵÄÐÐÁÐʹÃü£º
<?php namespace AppJobs; use IlluminateBusQueueable; use IlluminateContractsQueueShouldQueue; use IlluminateFoundationBusDispatchable; use IlluminateQueueInteractsWithQueue; use IlluminateQueueSerializesModels; class ProcessPodcast implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct() { // } /** * Execute the job. * * @return void */ public function handle() { // } }
µÇ¼ºó¸´ÖÆ
ĬÈÏÇéÐÎÏ£¬Õâ¸öÐÐÁÐʹÃü½«»áÔÚLaravelÐÐÁÐÖÐÿ3ÃëÖÓÖ´ÐÐÒ»´Î£¬Ö±µ½Ê¹Ãü´¦Öóͷ£Íê±Ï¡£
ÈôÊÇÎÒÃÇÐèÒªÐÞתҵÁеÄÏûºÄƵÂÊ£¬¿ÉÒÔÔÚ½¨ÉèÐÐÁÐʹÃüµÄʱ¼ä£¬×ª´ïÒ»¸öÑÓ³Ùʱ¼ä²ÎÊý£º
ProcessPodcast::dispatch()->delay(now()->addMinutes(5));
µÇ¼ºó¸´ÖÆ
ÕâÑù×öµÄЧ¹ûÊÇ£¬Õâ¸öÐÐÁÐʹÃü»áÔÚ5·ÖÖÓºó¼ÓÈëµ½ÐÐÁÐÖУ¬¶ø²»ÊÇÁ¬Ã¦×îÏÈÖ´ÐС£
ÁíÍ⣬ÔÚʹÓÃÐÐÁд¦Öóͷ£Ê¹Ãüʱ£¬Ò²ÐèҪעÖØһЩÊÂÏÀýÈç´¦Öóͷ£Ê§°ÜµÄÇéÐΡ¢ÐÐÁеÄÐÐÁйæÄ£µÈµÈ¡£ÕâЩÎÊÌⶼÐèÒªÎÒÃÇ¿ª·¢ÕßÈ¥ÖðÒ»½â¾ö¡£
×ÜÖ®£¬LaravelÐÐÁÐÊÇÒ»¸öÀû±ã¸ßЧµÄÒ첽ʹÃü´¦Öóͷ£¹¤¾ß£¬¹ØÓÚËüµÄÏûºÄƵÂÊ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖó¬Ê±Ê±¼ä¡¢ÑÓ³Ùʱ¼äµÈÊÖ¶ÎÀ´¾ÙÐпØÖÆ¡£Í¬Ê±£¬ÔÚÏÖʵӦÓÃÖУ¬Ò²Ðè×¢ÖØÐÐÁд¦Öóͷ£Ê§°ÜµÈÎÊÌâ¡£
ÒÔÉϾÍÊÇlaravelÖÐÐÐÁжà¾ÃÏûºÄÒ»´ÎµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡