×ðÁú¿­Ê±¹ÙÍøµÇ¼

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´¦ÀíÒ첽ʹÃü£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ