|
|
|
1
|
+<?php
|
|
|
|
2
|
+
|
|
|
|
3
|
+namespace App\Console\Commands\Sync;
|
|
|
|
4
|
+
|
|
|
|
5
|
+
|
|
|
|
6
|
+use App\Exceptions\InquiryFilterException;
|
|
|
|
7
|
+use App\Models\Project\Project;
|
|
|
|
8
|
+use App\Models\SyncSubmitTask\SyncSubmitTask as SyncSubmitTaskModel;
|
|
|
|
9
|
+use App\Services\SyncSubmitTaskService;
|
|
|
|
10
|
+use Illuminate\Console\Command;
|
|
|
|
11
|
+use Illuminate\Support\Facades\Cache;
|
|
|
|
12
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
13
|
+use Illuminate\Support\Facades\Redis;
|
|
|
|
14
|
+use Illuminate\Support\Facades\Schema;
|
|
|
|
15
|
+use Illuminate\Support\Str;
|
|
|
|
16
|
+
|
|
|
|
17
|
+/**
|
|
|
|
18
|
+ *
|
|
|
|
19
|
+ * Class SyncSubmitTask
|
|
|
|
20
|
+ * @package App\Console\Commands
|
|
|
|
21
|
+ * @author zbj
|
|
|
|
22
|
+ * @date 2023/11/28
|
|
|
|
23
|
+ */
|
|
|
|
24
|
+class SyncSubmitTaskDistribution extends Command
|
|
|
|
25
|
+{
|
|
|
|
26
|
+
|
|
|
|
27
|
+ protected $signature = 'sync_submit_task_distribution';
|
|
|
|
28
|
+ protected $description = '询盘、访问异步任务分发';
|
|
|
|
29
|
+
|
|
|
|
30
|
+ public function handle()
|
|
|
|
31
|
+ {
|
|
|
|
32
|
+ while (true) {
|
|
|
|
33
|
+ $len = Redis::llen('sync_submit_task');
|
|
|
|
34
|
+ if (!$len) {
|
|
|
|
35
|
+ $max_id = SyncSubmitTaskModel::orderBy('id', 'desc')->value('id');
|
|
|
|
36
|
+ if ($max_id > 2000000) {
|
|
|
|
37
|
+ $this->backup();
|
|
|
|
38
|
+ } else {
|
|
|
|
39
|
+ $tasks = SyncSubmitTaskModel::where('status', 0)->limit(100)->get();
|
|
|
|
40
|
+ foreach ($tasks as $task) {
|
|
|
|
41
|
+ $task->status = 2;
|
|
|
|
42
|
+ $task->save();
|
|
|
|
43
|
+ Redis::lpush('sync_submit_task', $task->id);
|
|
|
|
44
|
+ }
|
|
|
|
45
|
+ }
|
|
|
|
46
|
+ }
|
|
|
|
47
|
+ sleep(3);
|
|
|
|
48
|
+ }
|
|
|
|
49
|
+ }
|
|
|
|
50
|
+
|
|
|
|
51
|
+ /**
|
|
|
|
52
|
+ * 输出处理日志
|
|
|
|
53
|
+ */
|
|
|
|
54
|
+ public function output($message): bool
|
|
|
|
55
|
+ {
|
|
|
|
56
|
+ echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
|
|
|
|
57
|
+ return true;
|
|
|
|
58
|
+ }
|
|
|
|
59
|
+
|
|
|
|
60
|
+ /**
|
|
|
|
61
|
+ * 备份数据
|
|
|
|
62
|
+ * @author zbj
|
|
|
|
63
|
+ * @date 2024/1/23
|
|
|
|
64
|
+ */
|
|
|
|
65
|
+ public function backup()
|
|
|
|
66
|
+ {
|
|
|
|
67
|
+ DB::beginTransaction();
|
|
|
|
68
|
+ try {
|
|
|
|
69
|
+ $table = (new SyncSubmitTaskModel())->getTable();
|
|
|
|
70
|
+ $new_table = $table . '_backup_' . date('Ymd');
|
|
|
|
71
|
+
|
|
|
|
72
|
+ //重命名当前表
|
|
|
|
73
|
+ Schema::rename($table, $new_table);
|
|
|
|
74
|
+ //克隆表数据
|
|
|
|
75
|
+ DB::statement('CREATE TABLE ' . $table . ' LIKE ' . $new_table);
|
|
|
|
76
|
+
|
|
|
|
77
|
+ //未入队的写到新表
|
|
|
|
78
|
+ $list = DB::table($new_table)->where('status', 0)->get();
|
|
|
|
79
|
+ $data = [];
|
|
|
|
80
|
+ foreach ($list as $task) {
|
|
|
|
81
|
+ $data[] = [
|
|
|
|
82
|
+ 'type' => $task->type,
|
|
|
|
83
|
+ 'data' => json_encode($task->data),
|
|
|
|
84
|
+ 'created_at' => $task['created_at'],
|
|
|
|
85
|
+ 'updated_at' => $task['updated_at'],
|
|
|
|
86
|
+ ];
|
|
|
|
87
|
+ }
|
|
|
|
88
|
+ $data && SyncSubmitTaskModel::insert($data);
|
|
|
|
89
|
+
|
|
|
|
90
|
+ DB::commit();
|
|
|
|
91
|
+
|
|
|
|
92
|
+ $this->output('数据备份成功');
|
|
|
|
93
|
+ } catch (\Exception $e) {
|
|
|
|
94
|
+ $this->output('数据备份失败' . $e->getMessage());
|
|
|
|
95
|
+ DB::rollBack();
|
|
|
|
96
|
+ }
|
|
|
|
97
|
+ return $new_table ?? '';
|
|
|
|
98
|
+ }
|
|
|
|
99
|
+} |