Use queues in Laravel 6 modules
1. Use the following Artisan command to generate a new queue task for the specified module. as shown in 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. Task distribution, push tasks to 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 is mainly to change the value of the field step of the table Theme_Installation_Task 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 on the command line, and the queue will enter the listening state after the startup is complete:
PS E:\wwwroot\object> php artisan queue:listen
5. Invoke the API. After the task is distributed to the queue, you can see the listening status on the command line. as shown in 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. Check the field step of the data table Theme_Installation_Task has been changed to 2. And created_at and updated_at are: 2022-05-18 14:12:37, 2022-05-18 14:12:39. The modification time is 2 seconds later than the creation time. in line with expectations. as shown in Figure 3
7. When the handle method throws an exception, the queue task execution fails, and the log information is inserted into the table failed_jobs. It is expected to insert the exception Message synchronously into the field Message of the table Theme_Installation_Task. as shown in 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 the failed method in the task class. The Exception that caused the task 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 abnormal message is synchronized into the field message of the table Theme_Installation_Task. in line with expectations. as shown in Figure 5




