SyncSubmitTaskDistribution.php
2.7 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
<?php
namespace App\Console\Commands\Sync;
use App\Exceptions\InquiryFilterException;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask as SyncSubmitTaskModel;
use App\Services\SyncSubmitTaskService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
/**
*
* Class SyncSubmitTask
* @package App\Console\Commands
* @author zbj
* @date 2023/11/28
*/
class SyncSubmitTaskDistribution extends Command
{
protected $signature = 'sync_submit_task_distribution';
protected $description = '询盘、访问异步任务分发';
public function handle()
{
while (true) {
$len = Redis::llen('sync_submit_task');
if (!$len) {
$max_id = SyncSubmitTaskModel::orderBy('id', 'desc')->value('id');
if ($max_id > 2000000) {
$this->backup();
} else {
$tasks = SyncSubmitTaskModel::where('status', 0)->limit(100)->get();
foreach ($tasks as $task) {
$task->status = 3;
$task->save();
Redis::lpush('sync_submit_task', $task->id);
}
}
}
}
}
/**
* 输出处理日志
*/
public function output($message): bool
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
/**
* 备份数据
* @author zbj
* @date 2024/1/23
*/
public function backup()
{
DB::beginTransaction();
try {
$table = (new SyncSubmitTaskModel())->getTable();
$new_table = $table . '_backup_' . date('Ymd');
//重命名当前表
Schema::rename($table, $new_table);
//克隆表数据
DB::statement('CREATE TABLE ' . $table . ' LIKE ' . $new_table);
//未入队的写到新表
$list = DB::table($new_table)->where('status', 0)->get();
$data = [];
foreach ($list as $task) {
$data[] = [
'type' => $task->type,
'data' => $task->data,
'created_at' => $task->created_at,
'updated_at' => $task->updated_at,
];
}
$data && SyncSubmitTaskModel::insert($data);
DB::commit();
$this->output('数据备份成功');
} catch (\Exception $e) {
$this->output('数据备份失败' . $e->getMessage());
DB::rollBack();
}
return $new_table ?? '';
}
}