Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Queues´¦Öóͷ£Ò첽ʹÃü£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel queues´¦Öóͷ£Ò첽ʹÃü£¿
LaravelÊÇÒ»ÖÖÊ¢ÐеÄPHP¿ò¼Ü£¬ÆäÇ¿Ê¢µÄ¹¦Ð§Ê¹Æä³ÉΪWebÓ¦ÓóÌÐò¿ª·¢µÄ×î¼ÑÑ¡Ôñ¡£Ê¹ÓÃLaravel£¬Äú¿ÉÒÔ¿ìËÙÇáËɵؽ¨ÉèÖØ´óµÄWebÓ¦ÓóÌÐò¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛLaravel QueuesµÄʹÓã¬ÕâÊÇÒ»¸öÓÐÖúÓÚ´¦Öóͷ£Ò첽ʹÃüµÄ¹¦Ð§¡£
Ò첽ʹÃüµÄ¿´·¨
µ±Óû§ÔÚWebÓ¦ÓóÌÐòÖÐÖ´ÐÐij¸öʹÃüʱ£¬ÐèÒªÆÚ´ýʹÃüÍê³Éºó²Å»ª»ñµÃЧ¹û¡£Õâ¾ÍÊÇͬ²½Ê¹Ãü¡£¿ÉÊÇ£¬ÈôÊÇÎÒÃÇÏëÒªÔÚÓû§²»ÐèÒªÆÚ´ýʹÃüÍê³ÉµÄÇéÐÎϾÙÐд¦Öóͷ££¬¸ÃÔõô°ìÄØ£¿Õâ¾ÍÊÇÒ첽ʹÃü£¡
Ò첽ʹÃü²»»áÓ°ÏìÓû§µÄʵʱÏìÓ¦ÐÔÄÜ£¬ÓÉÓÚËüÃDz»ÆÚ´ýʹÃüÍê³É²Å·µ»ØЧ¹û¡£Ïà·´£¬ÕâЩʹÃü±»·ÅÈëÐÐÁÐÖУ¬²¢ÔÚºǫ́Öð¸ö´¦Öóͷ£¡£ÀýÈ磬Óû§ÏòЧÀÍÆ÷ÉÏ´«Îļþ¡£ÈôÊÇÓÉÓÚÎļþÌ«´ó¶øµ¼ÖÂÉÏ´«Ê±¼ä¹ý³¤£¬Óû§¿ÉÄÜ»á±äµÃ²»ÄÍÐIJ¢×÷·ÏÉÏ´«¡£´Ëʱ£¬Ê¹ÓÃÒ첽ʹÃü½«ÓÐÖúÓÚ×èÖ¹Óû§Á÷ʧ¡£
Laravel QueuesÊÇLaravel¿ò¼ÜµÄÒ»²¿·Ö£¬ËüÓÐÖúÓÚ´¦Öóͷ£Ò첽ʹÃü¡£
½¨ÉèLaravel Queues
ÔÚLaravelÖÐʹÓÃÐÐÁеĵÚÒ»²½ÊÇÈõçÄÔÖªµÀÓ¦¸Ã´¦Öóͷ£ÄĸöÐÐÁС£ÔÚQueue.phpÎļþÖУ¬Äú¿ÉÒÔ½ç˵ÐÐÁв¢Ö¸¶¨ÐÐÁÐʹÓÃÄÄÖÖÇý¶¯³ÌÐò¡£Çý¶¯³ÌÐò¿ÉÒÔÊÇÍâµØÇý¶¯³ÌÐò¡¢Êý¾Ý¿âÇý¶¯³ÌÐò¡¢RedisÇý¶¯³ÌÐò¡¢BeanstalkÇý¶¯³ÌÐòµÈ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
'connection' => env('QUEUE_DRIVER', 'sync'), 'driver' => env('QUEUE_DRIVER', 'sync'), 'queue' => [ 'default' => [ 'connection' => 'default', 'queue' => 'default', 'retry_after' => 90, 'backoff' => [ 'strategy' => 'exponential', 'max_attempts' => 3, 'delay' => 1000, ], ], ],
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëʾÀýÅú×¢£¬ÐÐÁÐĬÈÏʹÓÃ’sync’Çý¶¯³ÌÐò¡£¿ÉÊÇ£¬Äú¿ÉÒÔÔÚ.envÎļþÖнç˵²î±ðµÄÇý¶¯³ÌÐò¡£
Ìí¼ÓÐÐÁÐ
ÏÖÔÚ£¬ÎÒÃÇÒѾ½ç˵ÁËÐÐÁкÍËüµÄÇý¶¯³ÌÐò£¬ÐèÒª½«Ê¹ÃüÌí¼Óµ½ÐÐÁÐÖÐÒÔ¾ÙÐд¦Öóͷ£¡£ÔÚLaravel¿ò¼ÜÖУ¬Ê¹ÓÃdispatch()ÒªÁ콫ʹÃüÌí¼Óµ½ÐÐÁÐÖС£ÀýÈ磬ÎÒÃÇʹÓÃÏÂÃæµÄ´úÂ뽫ʹÃüÌí¼Óµ½ÐÐÁÐÖУº
ProcessPodcasts::dispatch($podcast);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ProcessPodcastsÊÇÎÒÃÇÒªÖ´ÐеÄʹÃü£¬$ podcastÊÇת´ï¸øʹÃüµÄ²ÎÊý¡£
½¨ÉèʹÃü
ÏÖÔÚ£¬ÈÃÎÒÃÇʹÓÃLaravel½¨ÉèÒ»¸öÒ첽ʹÃü¡£Îª´Ë£¬ÎÒÃǽ«ÔÚapp/JobsĿ¼Öн¨ÉèÒ»¸öеÄJob.phpÎļþ¡£ÒÔÏÂÊÇʾÀý´úÂ룺
<?php namespace AppJobs; use AppMailSendWelcomeEmail; use AppUser; use IlluminateBusQueueable; use IlluminateContractsQueueShouldQueue; use IlluminateFoundationBusDispatchable; use IlluminateQueueInteractsWithQueue; use IlluminateQueueSerializesModels; use IlluminateSupportFacadesMail; class SendWelcomeEmailJob implements ShouldQueue { // Traits used by the class use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $user; /** * Create a new job instance. * * @param User $user */ public function __construct(User $user) { $this->user = $user; } /** * Execute the job. * * @return void */ public function handle() { Mail::to($this->user->email)->send(new SendWelcomeEmail($this->user)); } }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽨ÉèÁËÒ»¸öÃûΪSendWelcomeEmailJobµÄÒ첽ʹÃü¡£¸ÃʹÃüÏòÐÂÓû§·¢ËÍÓʼþ¡£ÓÉÓÚ¸ÃʹÃüÔÚÐÐÁÐÖУ¬Òò´ËÓû§²»±ØÆÚ´ýµç×ÓÓʼþµÄ·¢ËÍÍê³É¡£
Ö´ÐÐÐÐÁÐ
ͨ¹ýÒÔÉϲÙ×÷£¬ÎÒÃÇÒѾ½¨ÉèÁËÒ»¸öʹÃü²¢½«ÆäÌí¼Óµ½ÐÐÁÐÖС£¿ÉÊÇ£¬ÎÒÃÇÐèÒªÖ´ÐÐÐÐÁÐÖеÄʹÃü¡£Óм¸ÖÖÒªÁì¿ÉÒÔÖ´ÐÐÐÐÁУ¬ÒÔÏÂÊÇʾÀý´úÂ룺
php artisan queue:work
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃLaravel CLIÖеÄqueue:workÏÂÁîÖ´ÐÐÐÐÁС£´ËÏÂÁһÁ¬Ö´ÐÐÖ±µ½Ã»ÓÐʹÃü¿ÉÒÔ´¦Öóͷ£ÎªÖ¹¡£
ÉÐÓÐÆäËûÒªÁì¿ÉÒÔÖ´ÐÐÐÐÁУ¬ÀýÈçÔÚsupervisorµÄ×ÊÖúÏÂÖ´ÐÐÐÐÁС£supervisorÊÇÒ»¸öÀú³ÌÖÎÀí¹¤¾ß£¬ÔÊÐíÎÒÃÇÔÚºǫ́ÖÎÀíLaravelÓ¦ÓóÌÐò¡£ÒªÔÚsupervisor×ÊÖúÏÂÖ´ÐÐÐÐÁУ¬Çëƾ֤ÒÔÏ°취²Ù×÷£º
×°ÖÃsupervisor£¨Çë²Î¿¼supervisorµÄʹÓã©¡£
½¨ÉèÒ»¸öÐÂÎļþqueue-worker.confλÓÚ/etc/supervisor/conf.dĿ¼ÖС£
ÊäÈëÒÔÏ´úÂ룺
[program:queue-worker] process_name=%(program_name)s_%(process_num)02d command=php /path/to/artisan queue:work autostart=true autorestart=true user=user numprocs=1 redirect_stderr=true stdout_logfile=/path/to/your/logs/queue-worker.log
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëΪsupervisor½¨ÉèÁËÒ»¸öÐÐÁÐÊÂÇé³ÌÐò¡£µ±supervisorÆô¶¯Ê±£¬´Ë³ÌÐò½«×îÏÈ´¦Öóͷ£ÐÐÁУ¬²¢ÔÚºǫִ́ÐС£
½áÂÛ
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇѧϰÁËÔõÑùʹÓÃLaravel Queues¾ÙÐÐÒ첽ʹÃü´¦Öóͷ£¡£ÎÒÃÇÏàʶÁËQueueµÄ¿´·¨£¬½ç˵ÁËеÄÐÐÁкÍÇý¶¯³ÌÐò¡£ÎÒÃÇ»¹½¨ÉèÁËÒ»¸öÒ첽ʹÃü£¬²¢ÏàʶÁËÔõÑùʹÓÃLaravel QueuesÖ´ÐÐËü¡£×ÜÌå¶øÑÔ£¬Laravel QueuesÊÇLaravel¿ª·¢ÖкÜÊÇÓÐÓõÄ×é¼þ£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵش¦Öóͷ£Ò첽ʹÃü¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Queues´¦Öóͷ£Ò첽ʹÃü£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡