作者 lyh

gx

<?php
namespace App\Jobs;
use App\Events\CopyImageFile;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class CopyImageFileJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3; // 可配置任务重试次数
protected $param;
/**
* Create a new job instance.
*
* @param CopyImageFile $event
* @return void
*/
public function __construct(CopyImageFile $event)
{
$this->param = $event->data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$imageModel = new ImageModel();
//获取当前项目的所有图片
$imageList = $imageModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos','size']);
if(!empty($imageList)){
$amazonS3Service = new AmazonS3Service();
foreach ($imageList as $k => $v){
$amazonS3Service->syncImageFiles(getImageUrl($v['path']));
$imageModel->edit(['is_cos'=>0],['id'=>$v['id']]);
}
}
$fileModel = new FileModel();
$fileList = $fileModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos']);
if(!empty($fileList)){
$amazonS3Service = new AmazonS3Service();
foreach ($fileList as $k => $v){
$amazonS3Service->syncImageFiles(getImageUrl($v['path']));
$fileList->edit(['is_cos'=>0],['id'=>$v['id']]);
}
}
}
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Listeners;
use App\Events\CopyImageFile;
use App\Events\UpdateHtml;
use App\Jobs\CopyImageFileJob;
use App\Jobs\updateHtmlJob;
use App\Models\File\File as FileModel;
use App\Models\File\Image as ImageModel;
... ... @@ -12,39 +13,16 @@ use Illuminate\Contracts\Queue\ShouldQueue;
class CopyImageFileListener implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param UpdateHtml $event
* @return void
*/
public function handle(CopyImageFile $event)
{
$this->param = $event->data;
$imageModel = new ImageModel();
//获取当前项目的所有图片
$imageList = $imageModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos','size']);
if(!empty($imageList)){
$amazonS3Service = new AmazonS3Service();
foreach ($imageList as $k => $v){
$amazonS3Service->syncImageFiles(getImageUrl($v['path']));
$imageModel->edit(['is_cos'=>0],['id'=>$v['id']]);
}
}
$fileModel = new FileModel();
$fileList = $fileModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos']);
if(!empty($fileList)){
$amazonS3Service = new AmazonS3Service();
foreach ($fileList as $k => $v){
$amazonS3Service->syncImageFiles(getImageUrl($v['path']));
$fileList->edit(['is_cos'=>0],['id'=>$v['id']]);
}
}
return true;
// 将任务推送到队列,异步执行
dispatch(new CopyImageFileJob($event));
}
}
... ...