[{"data":1,"prerenderedAt":622},["ShallowReactive",2],{"article:\u002Fblog\u002Fsetup-queue-job-for-laravel-8":3},{"article":4,"surroundings":616},{"id":5,"title":6,"body":7,"description":606,"extension":607,"meta":608,"navigation":108,"path":609,"published":610,"seo":611,"sitemap":612,"stem":613,"tag":614,"__hash__":615},"blog\u002Fblog\u002Fsetup-queue-job-for-laravel-8.md","Setup Queue Job for Laravel 8",{"type":8,"value":9,"toc":604},"minimark",[10,14,25,45,55,62,69,88,327,334,349,356,363,527,535,546,549,597,600],[11,12,13],"p",{},"This tutorial teaches how to setup a queue job system in Laravel.",[11,15,16,17,24],{},"We will use the example from the ",[18,19,23],"a",{"href":20,"rel":21},"https:\u002F\u002Fseancrx.com\u002Fblog\u002Fexception-reporting-email-for-laravel-8",[22],"nofollow","last blog article"," to demonstrate.",[11,26,27,28,32,33,36,37,40,41,44],{},"First, we save the queue job in the database. In your ",[29,30,31],"code",{},".env.example"," and ",[29,34,35],{},".env"," file, change the ",[29,38,39],{},"QUEUE_CONNECTION"," into ",[29,42,43],{},"database",".",[46,47,52],"pre",{"className":48,"code":50,"language":51},[49],"language-text","QUEUE_CONNECTION=database\n","text",[29,53,50],{"__ignoreMap":54},"",[11,56,57,58,61],{},"Next, run the command ",[29,59,60],{},"php artisan queue:table"," to generate the necessary migration file.",[11,63,64,65,68],{},"Then, run the command ",[29,66,67],{},"php artisan migrate"," to migrate the database.",[11,70,71,72,75,76,79,80,83,84,87],{},"We need to create a job for sending the exception email. Run the command ",[29,73,74],{},"php artisan make:job SendExceptionEmail",". The ",[29,77,78],{},"SendExceptionEmail.php"," file will be created in ",[29,81,82],{},"App\\Jobs\\"," folder. In this file, add an ",[29,85,86],{},"$message"," private variable, set the variable in the constructor and send the email in the handler function. The file should look like this:",[46,89,93],{"className":90,"code":91,"language":92,"meta":54,"style":54},"language-PHP shiki shiki-themes github-light github-dark","\u003C?php\n\nnamespace App\\Jobs;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Contracts\\Queue\\ShouldBeUnique;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Foundation\\Bus\\Dispatchable;\nuse Illuminate\\Queue\\InteractsWithQueue;\nuse Illuminate\\Queue\\SerializesModels;\nuse Mail;\nuse App\\Mail\\ExceptionOccurred;\n\nclass SendExceptionEmail implements ShouldQueue\n{\n    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;\n\n    private $message;\n\n    \u002F**\n     * Create a new job instance.\n     *\n     * @return void\n     *\u002F\n    public function __construct($message)\n    {\n        $this->message = $message;\n    }\n\n    \u002F**\n     * Execute the job.\n     *\n     * @return void\n     *\u002F\n    public function handle()\n    {\n        $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n        Mail::to($to)->send(new ExceptionOccurred($this->message));\n    }\n}\n","PHP",[29,94,95,103,110,116,121,127,133,139,145,151,157,163,169,174,180,186,192,197,203,208,214,220,226,232,238,244,250,256,262,267,272,278,283,288,293,299,304,310,316,321],{"__ignoreMap":54},[96,97,100],"span",{"class":98,"line":99},"line",1,[96,101,102],{},"\u003C?php\n",[96,104,106],{"class":98,"line":105},2,[96,107,109],{"emptyLinePlaceholder":108},true,"\n",[96,111,113],{"class":98,"line":112},3,[96,114,115],{},"namespace App\\Jobs;\n",[96,117,119],{"class":98,"line":118},4,[96,120,109],{"emptyLinePlaceholder":108},[96,122,124],{"class":98,"line":123},5,[96,125,126],{},"use Illuminate\\Bus\\Queueable;\n",[96,128,130],{"class":98,"line":129},6,[96,131,132],{},"use Illuminate\\Contracts\\Queue\\ShouldBeUnique;\n",[96,134,136],{"class":98,"line":135},7,[96,137,138],{},"use Illuminate\\Contracts\\Queue\\ShouldQueue;\n",[96,140,142],{"class":98,"line":141},8,[96,143,144],{},"use Illuminate\\Foundation\\Bus\\Dispatchable;\n",[96,146,148],{"class":98,"line":147},9,[96,149,150],{},"use Illuminate\\Queue\\InteractsWithQueue;\n",[96,152,154],{"class":98,"line":153},10,[96,155,156],{},"use Illuminate\\Queue\\SerializesModels;\n",[96,158,160],{"class":98,"line":159},11,[96,161,162],{},"use Mail;\n",[96,164,166],{"class":98,"line":165},12,[96,167,168],{},"use App\\Mail\\ExceptionOccurred;\n",[96,170,172],{"class":98,"line":171},13,[96,173,109],{"emptyLinePlaceholder":108},[96,175,177],{"class":98,"line":176},14,[96,178,179],{},"class SendExceptionEmail implements ShouldQueue\n",[96,181,183],{"class":98,"line":182},15,[96,184,185],{},"{\n",[96,187,189],{"class":98,"line":188},16,[96,190,191],{},"    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;\n",[96,193,195],{"class":98,"line":194},17,[96,196,109],{"emptyLinePlaceholder":108},[96,198,200],{"class":98,"line":199},18,[96,201,202],{},"    private $message;\n",[96,204,206],{"class":98,"line":205},19,[96,207,109],{"emptyLinePlaceholder":108},[96,209,211],{"class":98,"line":210},20,[96,212,213],{},"    \u002F**\n",[96,215,217],{"class":98,"line":216},21,[96,218,219],{},"     * Create a new job instance.\n",[96,221,223],{"class":98,"line":222},22,[96,224,225],{},"     *\n",[96,227,229],{"class":98,"line":228},23,[96,230,231],{},"     * @return void\n",[96,233,235],{"class":98,"line":234},24,[96,236,237],{},"     *\u002F\n",[96,239,241],{"class":98,"line":240},25,[96,242,243],{},"    public function __construct($message)\n",[96,245,247],{"class":98,"line":246},26,[96,248,249],{},"    {\n",[96,251,253],{"class":98,"line":252},27,[96,254,255],{},"        $this->message = $message;\n",[96,257,259],{"class":98,"line":258},28,[96,260,261],{},"    }\n",[96,263,265],{"class":98,"line":264},29,[96,266,109],{"emptyLinePlaceholder":108},[96,268,270],{"class":98,"line":269},30,[96,271,213],{},[96,273,275],{"class":98,"line":274},31,[96,276,277],{},"     * Execute the job.\n",[96,279,281],{"class":98,"line":280},32,[96,282,225],{},[96,284,286],{"class":98,"line":285},33,[96,287,231],{},[96,289,291],{"class":98,"line":290},34,[96,292,237],{},[96,294,296],{"class":98,"line":295},35,[96,297,298],{},"    public function handle()\n",[96,300,302],{"class":98,"line":301},36,[96,303,249],{},[96,305,307],{"class":98,"line":306},37,[96,308,309],{},"        $to = explode(',', env('ERROR_REPORTING_EMAIL', ''));\n",[96,311,313],{"class":98,"line":312},38,[96,314,315],{},"        Mail::to($to)->send(new ExceptionOccurred($this->message));\n",[96,317,319],{"class":98,"line":318},39,[96,320,261],{},[96,322,324],{"class":98,"line":323},40,[96,325,326],{},"}\n",[11,328,329,330,333],{},"To test if it is working, run the command ",[29,331,332],{},"php artisan queue:work",". You will need to make some exception to see it in action.",[11,335,336,337,339,340,345,346,44],{},"However, in production, running ",[29,338,332],{}," is not the recommended action. The queue worker will stop if you logout from the terminal or if there is some error causing the process being killed. The ",[18,341,344],{"href":342,"rel":343},"https:\u002F\u002Flaravel.com\u002Fdocs\u002F8.x\u002Fqueues#supervisor-configuration",[22],"recommended method"," is running it using ",[29,347,348],{},"Supervisor",[11,350,351,352,355],{},"To run the queue worker using supervisor, first you have to install it by running ",[29,353,354],{},"sudo apt-get install supervisor",", assuming you are using Debian based Linux environment.",[11,357,358,359,362],{},"Then, create a configuration file in the supervisor directory using ",[29,360,361],{},"sudo nano \u002Fetc\u002Fsupervisor\u002Fconf.d\u002Flaravel-worker.conf",". In the Nano editor, fill in the following:",[46,364,368],{"className":365,"code":366,"language":367,"meta":54,"style":54},"language-BASH shiki shiki-themes github-light github-dark","[program:laravel-worker]\nprocess_name=%(program_name)s_%(process_num)02d\ncommand=php \u002Fvar\u002Fwww\u002Fhtml\u002Fyourapp\u002Fartisan queue:work database --sleep=3 --tries=3 --max-time=3600\nautostart=true\nautorestart=true\nstopasgroup=true\nkillasgroup=true\nuser=forge\nnumprocs=8\nredirect_stderr=true\nstdout_logfile=\u002Fvar\u002Fwww\u002Fhtml\u002Fyourapp\u002Fworker.log\nstopwaitsecs=3600\n","BASH",[29,369,370,376,412,441,451,460,469,478,488,498,507,517],{"__ignoreMap":54},[96,371,372],{"class":98,"line":99},[96,373,375],{"class":374},"sVt8B","[program:laravel-worker]\n",[96,377,378,381,385,389,392,396,399,402,404,407,409],{"class":98,"line":105},[96,379,380],{"class":374},"process_name",[96,382,384],{"class":383},"szBVR","=",[96,386,388],{"class":387},"sZZnC","%",[96,390,391],{"class":374},"(",[96,393,395],{"class":394},"sScJk","program_name",[96,397,398],{"class":374},")",[96,400,401],{"class":387},"s_%",[96,403,391],{"class":374},[96,405,406],{"class":394},"process_num",[96,408,398],{"class":374},[96,410,411],{"class":387},"02d\n",[96,413,414,417,419,422,425,428,431,435,438],{"class":98,"line":112},[96,415,416],{"class":374},"command",[96,418,384],{"class":383},[96,420,421],{"class":387},"php",[96,423,424],{"class":394}," \u002Fvar\u002Fwww\u002Fhtml\u002Fyourapp\u002Fartisan",[96,426,427],{"class":387}," queue:work",[96,429,430],{"class":387}," database",[96,432,434],{"class":433},"sj4cs"," --sleep=3",[96,436,437],{"class":433}," --tries=3",[96,439,440],{"class":433}," --max-time=3600\n",[96,442,443,446,448],{"class":98,"line":118},[96,444,445],{"class":374},"autostart",[96,447,384],{"class":383},[96,449,450],{"class":387},"true\n",[96,452,453,456,458],{"class":98,"line":123},[96,454,455],{"class":374},"autorestart",[96,457,384],{"class":383},[96,459,450],{"class":387},[96,461,462,465,467],{"class":98,"line":129},[96,463,464],{"class":374},"stopasgroup",[96,466,384],{"class":383},[96,468,450],{"class":387},[96,470,471,474,476],{"class":98,"line":135},[96,472,473],{"class":374},"killasgroup",[96,475,384],{"class":383},[96,477,450],{"class":387},[96,479,480,483,485],{"class":98,"line":141},[96,481,482],{"class":374},"user",[96,484,384],{"class":383},[96,486,487],{"class":387},"forge\n",[96,489,490,493,495],{"class":98,"line":147},[96,491,492],{"class":374},"numprocs",[96,494,384],{"class":383},[96,496,497],{"class":387},"8\n",[96,499,500,503,505],{"class":98,"line":153},[96,501,502],{"class":374},"redirect_stderr",[96,504,384],{"class":383},[96,506,450],{"class":387},[96,508,509,512,514],{"class":98,"line":159},[96,510,511],{"class":374},"stdout_logfile",[96,513,384],{"class":383},[96,515,516],{"class":387},"\u002Fvar\u002Fwww\u002Fhtml\u002Fyourapp\u002Fworker.log\n",[96,518,519,522,524],{"class":98,"line":165},[96,520,521],{"class":374},"stopwaitsecs",[96,523,384],{"class":383},[96,525,526],{"class":387},"3600\n",[11,528,529,530,32,532,534],{},"In the configuration file, change the path in ",[29,531,416],{},[29,533,511],{}," according to your server.",[11,536,537,538,541,542,545],{},"To save it using Nano editor, use ",[29,539,540],{},"control + o",", then ",[29,543,544],{},"control + x"," to exit.",[11,547,548],{},"To start the supervisor for our queue worker, run these command accordingly:",[46,550,552],{"className":365,"code":551,"language":367,"meta":54,"style":54},"sudo supervisorctl reread\n\nsudo supervisorctl update\n\nsudo supervisorctl start laravel-worker:*\n",[29,553,554,565,569,578,582],{"__ignoreMap":54},[96,555,556,559,562],{"class":98,"line":99},[96,557,558],{"class":394},"sudo",[96,560,561],{"class":387}," supervisorctl",[96,563,564],{"class":387}," reread\n",[96,566,567],{"class":98,"line":105},[96,568,109],{"emptyLinePlaceholder":108},[96,570,571,573,575],{"class":98,"line":112},[96,572,558],{"class":394},[96,574,561],{"class":387},[96,576,577],{"class":387}," update\n",[96,579,580],{"class":98,"line":118},[96,581,109],{"emptyLinePlaceholder":108},[96,583,584,586,588,591,594],{"class":98,"line":123},[96,585,558],{"class":394},[96,587,561],{"class":387},[96,589,590],{"class":387}," start",[96,592,593],{"class":387}," laravel-worker:",[96,595,596],{"class":433},"*\n",[11,598,599],{},"There you have it. Now, your Laravel app should be sending the exception email using a queue job without obstructing the user experience.",[601,602,603],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":54,"searchDepth":105,"depth":105,"links":605},[],"Last time we configure our Laravel app to send email when an error exception is encountered. Let's enhance it by configuring the email sending into a queue job.","md",{},"\u002Fblog\u002Fsetup-queue-job-for-laravel-8","2021-06-12",{"title":6,"description":606},{"loc":609},"blog\u002Fsetup-queue-job-for-laravel-8","Software","vVx1yHiksqwgWxPgHYsADB2a-lZ9_DPrFPr4ENKRtDY",[617,618],null,{"title":619,"path":620,"stem":621,"children":-1},"Exception Reporting Email for Laravel 8","\u002Fblog\u002Fexception-reporting-email-for-laravel-8","blog\u002Fexception-reporting-email-for-laravel-8",1793045552943]