laravelÐÐÁÐʧ°ÜÊÇʲôÇéÐÎ
ÔÚʹÓà laravel ÐÐÁÐʱ£¬ÎÒÃÇ¿ÉÄÜ»áÓöµ½Ê¹ÃüÖ´ÐÐʧ°ÜµÄÇéÐΡ£Õâʱ¼ä£¬laravel ÌṩÁËÒ»¸öºÜºÃµÄ½â¾ö¼Æ»®£¬×ÝÈ»ÓÃʧÁÓÐÐÁС£
ÎÒÃÇÔÚÖ´ÐÐÐÐÁÐʹÃüʱ£¬¿ÉÒÔ½«Ê§°ÜµÄʹÃüÍÆÈëÒ»¸öרÃÅÓÃÀ´´¦Öóͷ£Ê§°ÜʹÃüµÄÐÐÁÐÖУ¬È»ºó°´ÆÚ¼ì²éÕâ¸öÐÐÁУ¬²¢ÖØÐÂÖ´ÐÐÆäÖеÄʧ°ÜʹÃü¡£ÕâÑùÒ»À´£¬ÎÒÃǾͿÉÒÔ×èÖ¹ÐÐÁÐʹÃüÒòһЩСÎÊÌâ¶øÖÕÖ¹£¬¼ÌÐøΪÎÒÃÇЧÀÍ¡£
Laravel µÄʧÁÓÐÐÁÐÌṩÁ˶àÖÖÉèÖÃÒÔ¼°¿ÉÀ©Õ¹µÄ½Ó¿Ú£¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇóÀ´¾ÙÐÐÉèÖá£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÉèÖÃÎļþ config/queue.php ÖÐÉèÖÃʧ°ÜµÄʹÃüÓ¦¸Ã½øÈëµÄÐÐÁС£¸ÃÉèÖÃÏîµÄ¼üΪ failed£¬ÆäֵΪһ¸öÊý×飬ÆäÖаüÀ¨ driver ºÍ queue Á½¸öÉèÖÃÏî¡£driver ÉèÖÃÏîÌåÏÖÎÒÃÇÏ£ÍûʹÓÃÄĸöʧ°ÜÇý¶¯£¬Laravel ĬÈÏÌṩÁË database ºÍ redis Á½¸öʧ°ÜÇý¶¯¡£¶ø queue ÔòÌåÏÖʧ°ÜʹÃü½«½øÈëÄĸöÐÐÁС£
'failed' => [ 'driver' => 'database', 'queue' => 'failed', ],
µÇ¼ºó¸´ÖÆ
ÈôÊÇÎÒÃÇÏ£ÍûʹÓÃÆäËûµÄʧ°ÜÇý¶¯£¬ÎÒÃÇ¿ÉÒÔͨ¹ý×¢²á×Ô½ç˵ʧ°ÜÇý¶¯²¢ÔÚ driver ÉèÖÃÏîÖÐÒýÓÃËüÀ´ÊµÏÖ¡£
×¢²á×Ô½ç˵Çý¶¯µÄ´úÂëÈçÏÂËùʾ£º
Queue::failing(function ($connection, $job, $data) { // ×Ô½ç˵´¦Öóͷ£Âß¼ });
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½ç˵´¦Öóͷ£Ê§°ÜʹÃüµÄÏêϸÂß¼¡£ÎÒÃÇ¿ÉÒÔÖ±½Ó½«Ê§°ÜʹÃüÔÙ´ÎÍÆÈëÐÐÁÐÖУ¬ÕâÑùÔÚÏÂÒ»´ÎÐÐÁд¦Öóͷ£Ê±¸ÃʹÃü¾Í»áÔٴα»Ö´ÐС£»òÕߣ¬ÎÒÃÇÒ²¿ÉÒÔ½«Ê§°ÜʹÃüµÄһЩÐÅÏ¢´æ´¢ÏÂÀ´ÒÔ±¸ºóÐø¼ì²éÒÔ¼°´¦Öóͷ£¡£
¹ØÓÚ½«Ê§°ÜʹÃüÍÆÈëÐÐÁеĴ¦Öóͷ£·½·¨£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
Queue::failing(function ($connection, $job, $data) { $queue = $job->getQueue(); $payload = $job->payload(); Queue::pushRaw($payload, $queue); });
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂ뽫ʧ°ÜʹÃüÖØÐÂÍƵ½ÁËÔʼÐÐÁÐÖУ¬ÆÚ´ýÏÂÒ»´Î±»Ö´ÐС£
¶ø¹ØÓÚ½«Ê§°ÜʹÃüÐÅÏ¢´æ´¢µÄ´¦Öóͷ£·½·¨£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
Queue::failing(function ($connection, $job, $data) { // ½«Ê§°ÜʹÃüÐÅÏ¢´æ´¢µ½Êý¾Ý¿âÖÐ DB::table('failed_jobs')->insert([ 'connection' => $connection, 'queue' => $job->getQueue(), 'payload' => $job->getRawBody(), 'exception' => $data['exception'], 'failed_at' => now(), ]); });
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂ뽫ʧ°ÜʹÃüµÄÐÅÏ¢´æ´¢µ½Êý¾Ý¿â±í failed_jobs ÖУ¬ÒÔ±ãÎÒÃÇÉÔºó¾ÙÐмì²éÒÔ¼°´¦Öóͷ£¡£
³ýÁËÒÔÉÏÁ½ÖÖ´¦Öóͷ£·½·¨£¬Laravel »¹ÌṩÁ˸ü¶àµÄ´¦Öóͷ£·½·¨¹©ÎÒÃÇÑ¡Ôñ¡£ÎÒÃÇ¿ÉÒÔÉó²é Laravel µÄÎĵµÒÔ¼°Ô´´úÂëÒÔÏàʶ¸ü¶àÏêÇé¡£
×îºó£¬ÎÒÃÇÐèÒª°´ÆÚ¼ì²éʧÁÓÐÐÁв¢ÖØÊÔÆäÖеÄʹÃü¡£Laravel ĬÈÏÌṩÁË queue:retry ÏÂÁîÀ´¾ÙÐÐʹÃüÖØÊÔ£¬¸ÃÏÂÁî½ÓÊÜÒ»¸ö¿ÉÑ¡²ÎÊý –queue£¬ÌåÏÖÎÒÃÇÒªÖØÊÔµÄʹÃüÐÐÁС£ÈôÊDz»Ö¸¶¨¸Ã²ÎÊý£¬ÔòĬÈÏÖØÊÔËùÓÐÐÐÁС£
php artisan queue:retry 5 --queue=my-queue
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁ»á´ÓʧÁÓÐÐÁÐÖÐµÄ my-queue ÐÐÁÐÖÐÖØÐÂÖ´ÐÐÇ° 5 ¸öʧ°ÜʹÃü¡£
³ý´ËÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔʹÓòÙ×÷ϵͳµÄ׼ʱʹÃü¹¤¾ß£¨Èç crontab£©À´ÖÜÆÚÐÔµØÖ´ÐÐ queue:retry ÏÂÁÒÔ°´ÆÚ´¦Öóͷ£Ê§°ÜʹÃü¡£
×ÜÖ®£¬Laravel µÄʧÁÓÐÐÁÐÌṩÁËÒ»¸öºÜÊǺÃÓõĽâ¾ö¼Æ»®À´´¦Öóͷ£ÐÐÁÐʹÃüµÄʧ°ÜÇéÐΡ£ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇó¾ÙÐÐÉèÖ㬲¢¶Ôʧ°ÜʹÃü¾ÙÐжàÖÖ²î±ðµÄ´¦Öóͷ£·½·¨¡£Í¬Ê±£¬ÎÒÃÇ»¹ÐèÒª°´ÆÚ¼ì²éʧÁÓÐÐÁв¢ÖØÊÔÆäÖеÄʹÃü£¬ÒÔÈ·±£ÐÐÁÐʹÃüÄܹ»Õý³£µØÔËÐС£
Ï£Íû±¾ÎĶԸ÷ÈËÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇlaravelÐÐÁÐʧ°ÜÊÇʲôÇéÐεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡