{"id":1404,"date":"2025-06-02T13:06:18","date_gmt":"2025-06-02T13:06:18","guid":{"rendered":"https:\/\/robinluo.top\/?p=1404"},"modified":"2025-06-02T13:09:16","modified_gmt":"2025-06-02T13:09:16","slug":"laravel-%e5%bc%82%e6%ad%a5%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=1404","title":{"rendered":"laravel \u5f02\u6b65\u4efb\u52a1"},"content":{"rendered":"\n<p>\u5728 Laravel \u4e2d\uff0c\u4f7f\u7528&nbsp;<code>php artisan queue<\/code>&nbsp;\u521b\u5efa\u5e76\u6267\u884c\u5f02\u6b65\u4efb\u52a1\u7684\u5b8c\u6574\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>1. \u914d\u7f6e\u961f\u5217\u9a71\u52a8<\/strong><\/h2>\n\n\n\n<p>\u5728&nbsp;<code>.env<\/code>&nbsp;\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u961f\u5217\u9a71\u52a8\uff08\u5982&nbsp;<code>database<\/code>\u3001<code>redis<\/code>\u3001<code>sqs<\/code>&nbsp;\u7b49\uff09\uff1aenv\u590d\u5236\u4e0b\u8f7d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">QUEUE_CONNECTION=database<\/pre>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u4f7f\u7528&nbsp;<code>database<\/code>&nbsp;\u9a71\u52a8\uff0c\u9700\u5148\u521b\u5efa&nbsp;<code>jobs<\/code>&nbsp;\u8868\uff1abash\u590d\u5236\u4e0b\u8f7d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan queue:table\nphp artisan migrate<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>2. \u521b\u5efa\u5f02\u6b65\u4efb\u52a1\uff08Job\uff09<\/strong><\/h2>\n\n\n\n<p>\u4f7f\u7528 Artisan \u751f\u6210\u4e00\u4e2a Job \u7c7b\uff1abash\u590d\u5236\u4e0b\u8f7d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan make:job ProcessPodcast<\/pre>\n\n\n\n<h2><strong>3. \u5206\u53d1\u4efb\u52a1\uff08Dispatch Job\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728\u63a7\u5236\u5668\u6216 CLI \u547d\u4ee4\u4e2d\u8c03\u7528 Job\uff1aphp\u590d\u5236\u4e0b\u8f7d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">use App\\Jobs\\ProcessPodcast;\n\n\/\/ \u5206\u53d1\u4efb\u52a1\u5230\u961f\u5217\nProcessPodcast::dispatch($podcastData);\n\n\/\/ \u5ef6\u8fdf\u6267\u884c\uff085\u79d2\u540e\u8fd0\u884c\uff09\nProcessPodcast::dispatch($podcastData)->delay(now()->addSeconds(5));\n\n\/\/ \u6307\u5b9a\u961f\u5217\uff08\u5982 \"high\" \u4f18\u5148\u7ea7\u961f\u5217\uff09\nProcessPodcast::dispatch($podcastData)->onQueue('high');\n\n\/\/ \u4f20\u8f93\u53c2\u6570\u4f8b\u5b50\nImportSCEyeScreeningBatRoaster::<em>dispatch<\/em>($task->id,$EyeScreeningBatID,$path,Admin::user());<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>4. \u542f\u52a8\u961f\u5217 Worker<\/strong><\/h2>\n\n\n\n<p>\u8fd0\u884c\u961f\u5217\u5904\u7406\u5668\uff1abash\u590d\u5236\u4e0b\u8f7d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan queue:work<\/pre>\n\n\n\n<p><strong>\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u9009\u9879<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>--queue=high,default<\/code><\/td><td>\u4f18\u5148\u5904\u7406&nbsp;<code>high<\/code>&nbsp;\u961f\u5217<\/td><\/tr><tr><td><code>--tries=3<\/code><\/td><td>\u5931\u8d25\u4efb\u52a1\u91cd\u8bd5\u6b21\u6570<\/td><\/tr><tr><td><code>--timeout=60<\/code><\/td><td>\u4efb\u52a1\u8d85\u65f6\u65f6\u95f4\uff08\u79d2\uff09<\/td><\/tr><tr><td><code>--daemon<\/code><\/td><td>\u5b88\u62a4\u8fdb\u7a0b\u6a21\u5f0f\uff08\u751f\u4ea7\u73af\u5883\u63a8\u8350\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>5. \u4efb\u52a1\u6267\u884c\u6d41\u7a0b<\/strong><\/h2>\n\n\n\n<ol><li><strong>\u4efb\u52a1\u5165\u961f<\/strong><ul><li><code>dispatch()<\/code>&nbsp;\u5c06\u4efb\u52a1\u5e8f\u5217\u5316\u540e\u5b58\u5165&nbsp;<code>jobs<\/code>&nbsp;\u8868\uff08<code>database<\/code>&nbsp;\u9a71\u52a8\uff09\u3002<\/li><\/ul><\/li><li><strong>Worker \u76d1\u542c\u961f\u5217<\/strong><ul><li><code>queue:work<\/code>&nbsp;\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u53cd\u5e8f\u5217\u5316\u3002<\/li><\/ul><\/li><li><strong>\u6267\u884c&nbsp;<code>handle()<\/code>&nbsp;\u65b9\u6cd5<\/strong><ul><li>\u8fd0\u884c Job \u7c7b\u4e2d\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/li><\/ul><\/li><li><strong>\u4efb\u52a1\u5b8c\u6210\/\u5931\u8d25<\/strong><ul><li>\u6210\u529f\uff1a\u4efb\u52a1\u4ece\u961f\u5217\u5220\u9664\u3002<\/li><li>\u5931\u8d25\uff1a\u4efb\u52a1\u8fdb\u5165&nbsp;<code>failed_jobs<\/code>&nbsp;\u8868\uff08\u9700\u914d\u7f6e\uff09\u3002<\/li><\/ul><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Laravel \u4e2d\uff0c\u4f7f\u7528&nbsp;php artisan queue&nbsp;\u521b\u5efa\u5e76\u6267\u884c\u5f02\u6b65\u4efb\u52a1\u7684\u5b8c\u6574\u6d41\u7a0b\u5982\u4e0b\uff1a 1. \u914d\u7f6e\u961f\u5217\u9a71\u52a8 \u5728&nbsp;.env&nbsp;\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u961f\u5217\u9a71\u52a8\uff08\u5982&nbsp;database\u3001redis\u3001sqs&nbsp;\u7b49\uff09\uff1aenv\u590d\u5236\u4e0b\u8f7d QUEUE_CONNECTION=database \u5982\u679c\u9700\u8981\u4f7f\u7528&nbsp;database&nbsp;\u9a71\u52a8\uff0c\u9700\u5148\u521b\u5efa&nbsp;jobs&nbsp;\u8868\uff1abash\u590d\u5236\u4e0b\u8f7d php artisan queue:table php artisan migrate 2. \u521b\u5efa\u5f02\u6b65\u4efb\u52a1\uff08Job\uff09 \u4f7f\u7528 Artisan \u751f\u6210\u4e00\u4e2a Job \u7c7b\uff1abash\u590d\u5236\u4e0b\u8f7d php artisan make:job ProcessPodcast 3. \u5206\u53d1\u4efb\u52a1\uff08Dispatch Job\uff09 \u5728\u63a7\u5236\u5668\u6216 CLI \u547d\u4ee4\u4e2d\u8c03\u7528 Job\uff1aphp\u590d\u5236\u4e0b\u8f7d use App\\Jobs\\ProcessPodcast; \/\/ \u5206\u53d1\u4efb\u52a1\u5230\u961f\u5217 ProcessPodcast::dispatch($podcastData); \/\/ \u5ef6\u8fdf\u6267\u884c\uff085\u79d2\u540e\u8fd0\u884c\uff09 ProcessPodcast::dispatch($podcastData)->delay(now()->addSeconds(5)); \/\/ \u6307\u5b9a\u961f\u5217\uff08\u5982 &#8220;high&#8221; \u4f18\u5148\u7ea7\u961f\u5217\uff09 ProcessPodcast::dispatch($podcastData)->onQueue(&#8216;high&#8217;); \/\/ \u4f20\u8f93\u53c2\u6570\u4f8b\u5b50 ImportSCEyeScreeningBatRoaster::dispatch($task->id,$EyeScreeningBatID,$path,Admin::user()); 4. \u542f\u52a8\u961f\u5217 Worker \u8fd0\u884c\u961f\u5217\u5904\u7406\u5668\uff1abash\u590d\u5236\u4e0b\u8f7d php [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[10],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1404"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1404"}],"version-history":[{"count":2,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1404\/revisions"}],"predecessor-version":[{"id":1406,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1404\/revisions\/1406"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}