lists_to_list_hot.php
1.9 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
<?php
include_once "../vendor/autoload.php";
/**
* 把lists表的is_hots字段为1的记录插入到list_hot表中 并删除list表中的这些记录
*/
// 进程管理器
$pm = new \Swoole\Process\Manager();
// 启动业务进程
$pm->addBatch(10,function (\Swoole\Process\Pool $pool, int $worker_id){
if($worker_id == 0){
// 一个月以前的数据
$one_month_ago = strtotime(date('Y-m-d', strtotime('-1 month')));
// 查询lists lists_hot
$id = redis()->rPop('lists_to_lists_hot');
if(!$id) {
$id = 389884523;
}else{
$id = redis()->rPush('lists_to_lists_hot', $id);
}
$limit = 100;
while (true){
// echo "正在查询 id {$id}\n";
$data = db()->all("select `id` from lists where id > {$id} and `is_hots` = 1 and `udate` < {$one_month_ago} limit {$limit};");
if($data){
foreach ($data as $item) {
$id = $item['id'];
redis()->rPush('lists_to_lists_hot', $item['id']);
}
}else{
sleep(5);
}
}
}else{
// 迁移数据
while (true){
$id = redis()->lPop('lists_to_lists_hot');
if($id){
$data = db()->first("select * from lists where `id` = {$id} limit 1;");
if($data){
try {
db()->throw()->insert('lists_back', $data);
} catch (\Exception $e) {
@file_put_contents('lists_to_lists_hot.data.error.log', json_encode($data)."\n", FILE_APPEND);
}
db()->delete('lists', ['id' => $id]);
echo $id . " ok \n";
}
}else{
sleep(1);
}
}
}
});
$pm->start();