没有不值得去解决的问题,也没有不值得去学习的技术!

Using Queues in Laravel 6 Modules

Call the API. After the job is dispatched to the queue, you can see the listening status in the command line

1. Use the following Artisan command to generate a new queue job for the specified module. See Figure 1.

Use the following Artisan command to generate a new queue job for the specified module
Figure 1
PHP
PS E:\wwwroot\object> php artisan module:make-job ThemeInstallation ThemeStoreDB
Created : E:/wwwroot/object/Modules/ThemeStoreDB/Jobs/ThemeInstallation.php
PS E:\wwwroot\object>

2. Dispatch the job, pushing it onto the queue.

PHP
use Modules\ThemeStoreDB\Jobs\ThemeInstallation as ThemeInstallationJob;

// 推送任务到队列
ThemeInstallationJob::dispatch($themeInstallationTask);

3. Edit /Modules/ThemeStoreDB/Jobs/ThemeInstallation.php. The processing example in the queue primarily updates the value of the step field in the theme_installation_task table to 2.

PHP
<?php
 
namespace Modules\ThemeStoreDB\Jobs;
 
use Modules\ThemeStoreDB\Entities\ThemeInstallationTask;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
 
class ThemeInstallation implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    protected $themeInstallationTask;
 
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(ThemeInstallationTask $themeInstallationTask)
    {
        // 队列任务构造器中接收了 Eloquent 模型,将会只序列化模型的 ID
        $this->themeInstallationTask = $themeInstallationTask;
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->themeInstallationTask->step = ThemeInstallationTask::STEP_DOWNLOAD_ZIP;
        $this->themeInstallationTask->save();
    }
}
 
 

4. Start the queue system from the command line. Once started, the queue enters a listening state:

Plaintext
PS E:\wwwroot\object> php artisan queue:listen

5. Call the API. After the job is dispatched to the queue, you can see the listening status in the command line. See Figure 2.

Call the API. After the job is dispatched to the queue, you can see the listening status in the command line
Figure 2
Plaintext
PS E:\wwwroot\object> php artisan queue:listen
[2022-05-18 14:12:39][0brUqtQG1q1J8njW3uyUnrXE16ubcCXs] Processing: Modules\ThemeStoreDB\Jobs\ThemeInstallation
[2022-05-18 14:12:39][0brUqtQG1q1J8njW3uyUnrXE16ubcCXs] Processed:  Modules\ThemeStoreDB\Jobs\ThemeInstallation


6. Verify that the value of the step field in the theme_installation_task table has been updated to 2. The created_at and updated_at values are 2022-05-18 14:12:37 and 2022-05-18 14:12:39, respectively. The update time is 2 seconds later than the creation time, which matches expectations. See Figure 3.

Verify that the value of the step field in the theme_installation_task table has been updated to 2. The created_at and updated_at values are 2022-05-18 14:12:37 and 2022-05-18 14:12:39, respectively. The update time is 2 seconds later than the creation time, which matches expectations
Figure 3

7. When the handle method throws an exception, the queue job fails and the log information is inserted into the failed_jobs table. The expectation is to synchronously insert the exception’s message into the message field of the theme_installation_task table. See Figure 4.

When the handle method throws an exception, the queue job fails and the log information is inserted into the failed_jobs table. The expectation is to synchronously insert the exception's message into the message field of the theme_installation_task table
Figure 4
Plaintext
PS E:\wwwroot\object> php artisan queue:listen
[2022-05-18 15:49:45][XFUmrUWBn2muST8AXmRXzjVLUr72UUde] Processing: Modules\ThemeStoreDB\Jobs\ThemeInstallation
[2022-05-18 15:49:45][XFUmrUWBn2muST8AXmRXzjVLUr72UUde] Failed:     Modules\ThemeStoreDB\Jobs\ThemeInstallation


8. Define a failed method in the job class. The Exception that caused the job to fail will be passed to the failed method.

PHP
<?php
 
namespace Modules\ThemeStoreDB\Jobs;
 
use Modules\ThemeStoreDB\Entities\ThemeInstallationTask;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
 
class ThemeInstallation implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    /**
     * 任务可以尝试的最大次数。
     *
     * @var int
     */
    public $tries = 1;
 
    /**
     * 任务可以执行的最大秒数 (超时时间)。
     *
     * @var int
     */
    public $timeout = 600;
 
    protected $themeInstallationTask;
 
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(ThemeInstallationTask $themeInstallationTask)
    {
        // 队列任务构造器中接收了 Eloquent 模型,将会只序列化模型的 ID
        $this->themeInstallationTask = $themeInstallationTask;
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        checkhere();
        $this->themeInstallationTask->step = ThemeInstallationTask::STEP_DOWNLOAD_ZIP;
        $this->themeInstallationTask->save();
    }
 
    /**
     * 任务失败的处理过程
     *
     * @param  Exception  $exception
     * @return void
     */
    public function failed(Exception $exception)
    {
        $this->themeInstallationTask->message = $exception->getMessage();
        $this->themeInstallationTask->save();
    }
}
 
 

9. The exception’s message is synchronously inserted into the message field of the theme_installation_task table. This matches expectations. See Figure 5.

The exception's message is synchronously inserted into the message field of the theme_installation_task table. This matches expectations
Figure 5

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系