SyncFile.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @remark :
* @name :SyncFile.php
* @author :lyh
* @method :post
* @time :2024/4/17 10:05
*/
namespace App\Console\Commands\SyncFilesImage;
use App\Models\File\ErrorFile;
use App\Models\Geo\GeoQuestion;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class SyncFile extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_file';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步图片与文件';
public function handle(){
while (true){
$task_id = $this->getTaskId();
echo "task_id:$task_id\n";
if(empty($task_id)){
sleep(100);
continue;
}
$errorFileModel = new ErrorFile();
$info = $errorFileModel->read(['id'=>$task_id],['id','path']);
if(strpos($info['path'], '/181/') !== false ){
$code = $this->synchronizationFiles($info['path']);
}else{
$code = $this->synchronizationFile($info['path']);
}
if(200 == (int)$code){
echo date('Y-m-d H:i:s') . '编辑的path为:'. $info['path'] .',主键id:'. $info['id'] . PHP_EOL;
$errorFileModel->edit(['status'=>2],['id'=>$info['id']]);
}else{
$errorFileModel->edit(['status'=>0],['id'=>$info['id']]);
}
echo date('Y-m-d H:i:s') . '编辑的end为:' .$code . PHP_EOL;
}
}
public function getTaskId(){
$key = 'sync_file_task';
$task_id = Redis::rpop($key);
if(empty($task_id)){
$lock_key = 'sync_file_lock';
$lock_ttl = 60; // 锁时间大于当前 锁功能执行时间
// 尝试获取锁,非阻塞方式
$lock = Redis::set($lock_key, 1, 'EX', $lock_ttl, 'NX');
if (empty($lock)){
return $task_id;
}
$errorFileModel = new ErrorFile();
$lists = $errorFileModel->list(['status'=>0],'id',['id','path'],'asc',2000);//未同步成功的图片及文件
foreach ($lists as $v){
Redis::lpush($key, $v['id']);
$errorFileModel->edit(['status'=>1],['id'=>$v['id']]);
}
$task_id = Redis::rpop($key);
}
return $task_id;
}
/**
* @remark :指定同步文件到獨立177服務器
* @name :synchronizationFile
* @author :lyh
* @method :post
* @time :2024/4/8 11:10
*/
public function synchronizationFile($path_name){
//同步到大文件
$cmd = 'curl -k -F "file_path='.$path_name.'" -F "save_path=/www/wwwroot/cos'.$path_name.'" https://v6-file.globalso.com/upload.php';
return shell_exec($cmd);
}
public function synchronizationFiles($path_name){
//同步到大文件
$file_path = config('filesystems.disks.s3')['cdn'].$path_name;
$directoryPath = pathinfo($path_name, PATHINFO_DIRNAME);
$cmd = 'curl -k -F "file_path='.$file_path.'" -F "save_path=/www/wwwroot/cos'.$directoryPath.'" https://v6-file.globalso.com/upload.php';
return shell_exec($cmd);
}
}