作者 邓超

预热脚本移入

@@ -6,28 +6,62 @@ include_once "../vendor/autoload.php"; @@ -6,28 +6,62 @@ include_once "../vendor/autoload.php";
6 * 把lists表的is_hots字段为1的记录插入到list_hot表中 并删除list表中的这些记录 6 * 把lists表的is_hots字段为1的记录插入到list_hot表中 并删除list表中的这些记录
7 */ 7 */
8 8
9 -// 一个月以前的数据  
10 -$one_month_ago = strtotime(date('Y-m-d', strtotime('-1 month'))); 9 +// 进程管理器
  10 +$pm = new \Swoole\Process\Manager();
  11 +
  12 +// 启动业务进程
  13 +$pm->addBatch(10,function (\Swoole\Process\Pool $pool, int $worker_id){
  14 + if($worker_id == 0){
  15 + // 一个月以前的数据
  16 + $one_month_ago = strtotime(date('Y-m-d', strtotime('-1 month')));
11 17
12 // 查询lists lists_hot 18 // 查询lists lists_hot
13 -$id = 389884523;  
14 -while (true){  
15 - $data = db()->first("select * from lists where id > {$id} and `is_hots` = 1 and `udate` < {$one_month_ago} limit 1;"); 19 + $id = redis()->rPop('lists_to_lists_hot');
  20 + if(!$id) {
  21 + $id = 389884523;
  22 + }else{
  23 + $id = redis()->rPush('lists_to_lists_hot', $id);
  24 + }
  25 + $limit = 100;
  26 + while (true){
  27 +// echo "正在查询 id {$id}\n";
  28 + $data = db()->all("select `id` from lists where id > {$id} and `is_hots` = 1 and `udate` < {$one_month_ago} limit {$limit};");
  29 + if($data){
  30 + foreach ($data as $item) {
  31 + $id = $item['id'];
  32 + redis()->rPush('lists_to_lists_hot', $item['id']);
  33 + }
  34 + }else{
  35 + sleep(5);
  36 + }
  37 + }
  38 + }else{
  39 +
  40 + // 迁移数据
  41 + while (true){
  42 + $id = redis()->lPop('lists_to_lists_hot');
  43 + if($id){
  44 + $data = db()->first("select * from lists where `id` = {$id} limit 1;");
16 if($data){ 45 if($data){
17 - $id = $data['id'];  
18 try { 46 try {
19 db()->throw()->insert('lists_hot', $data); 47 db()->throw()->insert('lists_hot', $data);
20 } catch (\Exception $e) { 48 } catch (\Exception $e) {
21 @file_put_contents('lists_to_lists_hot.data.error.log', json_encode($data)."\n", FILE_APPEND); 49 @file_put_contents('lists_to_lists_hot.data.error.log', json_encode($data)."\n", FILE_APPEND);
22 } 50 }
23 - db()->delete('lists', ['id' => $data['id']]); 51 + db()->delete('lists', ['id' => $id]);
  52 +
24 echo $id . " ok \n"; 53 echo $id . " ok \n";
25 -// break; 54 + }
26 }else{ 55 }else{
27 - sleep(5); 56 + sleep(1);
  57 + }
  58 + }
  59 +
  60 +
28 } 61 }
29 -} 62 +});
30 63
  64 +$pm->start();
31 65
32 66
33 67