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ÖÐÐÐÁжà¾ÃÏûºÄÒ»´ÎµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡