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

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.
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
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:
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.

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.

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.

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
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.

