SyncImageFileJob.php 1.5 KB
<?php

namespace App\Jobs;

use App\Models\File\ErrorFile;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SyncImageFileJob 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($data)
    {
        $this->param = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $code = $this->synchronizationFile($this->param['path'].'/'.$this->param['name']);
        echo date('Y-m-d H:i:s') . ' | ' . $code . PHP_EOL;
        if(200 != (int)$code){
            $errorFileModel = new ErrorFile();
            $errorFileModel->add(['path'=>$this->param['path'].'/'.$this->param['name']]);
        }
        return true;
    }

    public function synchronizationFile($path_name){
        //同步到大文件
//        $file_path = config('filesystems.disks.cos')['cdn1'].$path_name;
//        $directoryPath = pathinfo($path_name, PATHINFO_DIRNAME);
        $cmd = 'curl -k -F "file_path='.$path_name.'" -F "save_path=/www/wwwroot/cos'.$path_name.'" https://v6-file.globalso.com/upload.php';
        echo date('Y-m-d H:i:s') . ' | ' . $cmd . PHP_EOL;
        return shell_exec($cmd);
    }
}