lists_to_list_hot.php 1.9 KB
<?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_hot', $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();