正在显示
2 个修改的文件
包含
64 行增加
和
26 行删除
app/Jobs/CopyImageFileJob.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Jobs; | ||
| 4 | + | ||
| 5 | +use App\Events\CopyImageFile; | ||
| 6 | +use Illuminate\Bus\Queueable; | ||
| 7 | +use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
| 8 | +use Illuminate\Contracts\Queue\ShouldQueue; | ||
| 9 | +use Illuminate\Foundation\Bus\Dispatchable; | ||
| 10 | +use Illuminate\Queue\InteractsWithQueue; | ||
| 11 | +use Illuminate\Queue\SerializesModels; | ||
| 12 | + | ||
| 13 | +class CopyImageFileJob implements ShouldQueue | ||
| 14 | +{ | ||
| 15 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
| 16 | + | ||
| 17 | + public $tries = 3; // 可配置任务重试次数 | ||
| 18 | + | ||
| 19 | + protected $param; | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * Create a new job instance. | ||
| 23 | + * | ||
| 24 | + * @param CopyImageFile $event | ||
| 25 | + * @return void | ||
| 26 | + */ | ||
| 27 | + public function __construct(CopyImageFile $event) | ||
| 28 | + { | ||
| 29 | + $this->param = $event->data; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * Execute the job. | ||
| 34 | + * | ||
| 35 | + * @return void | ||
| 36 | + */ | ||
| 37 | + public function handle() | ||
| 38 | + { | ||
| 39 | + $imageModel = new ImageModel(); | ||
| 40 | + //获取当前项目的所有图片 | ||
| 41 | + $imageList = $imageModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos','size']); | ||
| 42 | + if(!empty($imageList)){ | ||
| 43 | + $amazonS3Service = new AmazonS3Service(); | ||
| 44 | + foreach ($imageList as $k => $v){ | ||
| 45 | + $amazonS3Service->syncImageFiles(getImageUrl($v['path'])); | ||
| 46 | + $imageModel->edit(['is_cos'=>0],['id'=>$v['id']]); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + $fileModel = new FileModel(); | ||
| 51 | + $fileList = $fileModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos']); | ||
| 52 | + if(!empty($fileList)){ | ||
| 53 | + $amazonS3Service = new AmazonS3Service(); | ||
| 54 | + foreach ($fileList as $k => $v){ | ||
| 55 | + $amazonS3Service->syncImageFiles(getImageUrl($v['path'])); | ||
| 56 | + $fileList->edit(['is_cos'=>0],['id'=>$v['id']]); | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | +} |
| @@ -4,6 +4,7 @@ namespace App\Listeners; | @@ -4,6 +4,7 @@ namespace App\Listeners; | ||
| 4 | 4 | ||
| 5 | use App\Events\CopyImageFile; | 5 | use App\Events\CopyImageFile; |
| 6 | use App\Events\UpdateHtml; | 6 | use App\Events\UpdateHtml; |
| 7 | +use App\Jobs\CopyImageFileJob; | ||
| 7 | use App\Jobs\updateHtmlJob; | 8 | use App\Jobs\updateHtmlJob; |
| 8 | use App\Models\File\File as FileModel; | 9 | use App\Models\File\File as FileModel; |
| 9 | use App\Models\File\Image as ImageModel; | 10 | use App\Models\File\Image as ImageModel; |
| @@ -12,39 +13,16 @@ use Illuminate\Contracts\Queue\ShouldQueue; | @@ -12,39 +13,16 @@ use Illuminate\Contracts\Queue\ShouldQueue; | ||
| 12 | 13 | ||
| 13 | class CopyImageFileListener implements ShouldQueue | 14 | class CopyImageFileListener implements ShouldQueue |
| 14 | { | 15 | { |
| 16 | + use InteractsWithQueue, SerializesModels; | ||
| 15 | public function __construct() | 17 | public function __construct() |
| 16 | { | 18 | { |
| 17 | // | 19 | // |
| 18 | } | 20 | } |
| 19 | 21 | ||
| 20 | - /** | ||
| 21 | - * Handle the event. | ||
| 22 | - * | ||
| 23 | - * @param UpdateHtml $event | ||
| 24 | - * @return void | ||
| 25 | - */ | ||
| 26 | public function handle(CopyImageFile $event) | 22 | public function handle(CopyImageFile $event) |
| 27 | { | 23 | { |
| 28 | $this->param = $event->data; | 24 | $this->param = $event->data; |
| 29 | - $imageModel = new ImageModel(); | ||
| 30 | - //获取当前项目的所有图片 | ||
| 31 | - $imageList = $imageModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos','size']); | ||
| 32 | - if(!empty($imageList)){ | ||
| 33 | - $amazonS3Service = new AmazonS3Service(); | ||
| 34 | - foreach ($imageList as $k => $v){ | ||
| 35 | - $amazonS3Service->syncImageFiles(getImageUrl($v['path'])); | ||
| 36 | - $imageModel->edit(['is_cos'=>0],['id'=>$v['id']]); | ||
| 37 | - } | ||
| 38 | - } | ||
| 39 | - $fileModel = new FileModel(); | ||
| 40 | - $fileList = $fileModel->list(['project_id'=>$this->param['project_id'],'is_cos'=>1],'id',['id','path','is_cos']); | ||
| 41 | - if(!empty($fileList)){ | ||
| 42 | - $amazonS3Service = new AmazonS3Service(); | ||
| 43 | - foreach ($fileList as $k => $v){ | ||
| 44 | - $amazonS3Service->syncImageFiles(getImageUrl($v['path'])); | ||
| 45 | - $fileList->edit(['is_cos'=>0],['id'=>$v['id']]); | ||
| 46 | - } | ||
| 47 | - } | ||
| 48 | - return true; | 25 | + // 将任务推送到队列,异步执行 |
| 26 | + dispatch(new CopyImageFileJob($event)); | ||
| 49 | } | 27 | } |
| 50 | } | 28 | } |
-
请 注册 或 登录 后发表评论